Skip to content

Commit

Permalink
Support Prism as a Ruby parser
Browse files Browse the repository at this point in the history
Follow up rubocop/rubocop-ast#277

There are no changes to the implementation, but the required dependency version of
RuboCop AST will be updated. It would be more rational than adding new dependency on
`gem 'rubocop-ast', '>= 1.31.1'` in the Gemfile just to bump up the minor version.

RuboCop Minitest now ensures that `RuboCop::AST::ProcessedSource` used in tests is aware of
`parser_engine` parameter.

Tests for RuboCop AST with Prism as the backend can be run as follows:

```console
bundle exec rake prism_test
```

The above is the shortcut alias for:

```console
PARSER_ENGINE=parser_prism bundle exec rake test
```

RuboCop works on Ruby versions 2.7+, but since Prism only targets parsing for Ruby 3.3+,
`internal_investigation` Rake task will not be executed. This task is only run with the Parser gem,
which can parse Ruby versions 2.0+.
  • Loading branch information
koic committed Mar 4, 2024
1 parent 2448f36 commit 43b6ddb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ jobs:
- name: internal_investigation
run: bundle exec rake internal_investigation

prism:
runs-on: ubuntu-latest
name: Prism
steps:
- uses: actions/checkout@v4
- name: set up Ruby
uses: ruby/setup-ruby@v1
with:
# Specify the minimum Ruby version 2.7 required for Prism to run.
ruby-version: 2.7
bundler-cache: true
- name: test
env:
PARSER_ENGINE: parser_prism
run: bundle exec rake prism_test

documentation_checks:
runs-on: ubuntu-latest
name: Check documentation syntax
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gemspec
gem 'bump', require: false
gem 'minitest', '~> 5.11'
gem 'minitest-proveit'
gem 'prism'
gem 'rake'
gem 'rubocop', github: 'rubocop/rubocop'
gem 'rubocop-performance', '~> 1.18.0'
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ else
end
end

desc 'Run tests with Prism'
task :prism_test do
sh('PARSER_ENGINE=parser_prism bundle exec rake test')
end

desc 'Run RuboCop over itself'
RuboCop::RakeTask.new(:internal_investigation)

task default: %i[documentation_syntax_check test internal_investigation]
task default: %i[documentation_syntax_check test prism_test internal_investigation]

desc 'Generate a new cop template'
task :new_cop, [:cop] do |_task, args|
Expand Down
1 change: 1 addition & 0 deletions changelog/change_support_prism.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#304](https://github.com/rubocop/rubocop-minitest/pull/304): Require RuboCop AST >= 1.31.1 to support Prism as a Ruby Parser. ([@koic][])
9 changes: 7 additions & 2 deletions lib/rubocop/minitest/assert_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def parse_source!(source, file = nil)
file = file.path
end

processed_source = RuboCop::ProcessedSource.new(source, ruby_version, file)
processed_source = RuboCop::ProcessedSource.new(source, ruby_version, file, parser_engine: parser_engine)

# Follow up https://github.com/rubocop/rubocop/pull/10987.
# When support for RuboCop 1.37.1 ends, this condition can be removed.
Expand Down Expand Up @@ -216,7 +216,12 @@ def registry
end

def ruby_version
RuboCop::TargetRuby::DEFAULT_VERSION
# Prism supports parsing Ruby 3.3+.
ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
end

def parser_engine
ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym
end
end
# rubocop:enable Metrics/ModuleLength
Expand Down
2 changes: 1 addition & 1 deletion rubocop-minitest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_runtime_dependency 'rubocop', '>= 1.61', '< 2.0'
spec.add_runtime_dependency 'rubocop-ast', '>= 1.30.0', '< 2.0'
spec.add_runtime_dependency 'rubocop-ast', '>= 1.31.1', '< 2.0'
end

0 comments on commit 43b6ddb

Please sign in to comment.