Skip to content

Commit 061a18a

Browse files
authored
Merge pull request #1245 from koic/support_prism
Support Prism as a Ruby parser
2 parents 6dc0e59 + d8ce294 commit 061a18a

20 files changed

+87
-38
lines changed

.github/workflows/test.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ jobs:
5353
- name: internal_investigation
5454
run: bundle exec rake internal_investigation
5555

56+
prism:
57+
runs-on: ubuntu-latest
58+
name: Prism
59+
steps:
60+
- uses: actions/checkout@v4
61+
- name: set up Ruby
62+
uses: ruby/setup-ruby@v1
63+
with:
64+
# Specify the minimum Ruby version 2.7 required for Prism to run.
65+
ruby-version: 2.7
66+
bundler-cache: true
67+
- name: spec
68+
env:
69+
PARSER_ENGINE: parser_prism
70+
run: bundle exec rake prism_spec
71+
5672
documentation_checks:
5773
runs-on: ubuntu-latest
5874
name: Check documentation syntax

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
77
gemspec
88

99
gem 'bump', require: false
10+
gem 'prism'
1011
gem 'rake'
1112
gem 'rspec'
1213
gem 'rubocop', github: 'rubocop/rubocop'

Rakefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ task :spec do
2727
end
2828
end
2929

30+
desc 'Run RSpec with Prism'
31+
task :prism_spec do
32+
sh('PARSER_ENGINE=parser_prism bundle exec rake spec')
33+
end
34+
3035
desc 'Run RSpec with code coverage'
3136
task :coverage do
3237
ENV['COVERAGE'] = 'true'
@@ -36,7 +41,7 @@ end
3641
desc 'Run RuboCop over itself'
3742
RuboCop::RakeTask.new(:internal_investigation)
3843

39-
task default: %i[documentation_syntax_check spec internal_investigation]
44+
task default: %i[documentation_syntax_check spec prism_spec internal_investigation]
4045

4146
desc 'Generate a new cop template'
4247
task :new_cop, [:cop] do |_task, args|

changelog/new_support_prism.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1245](https://github.com/rubocop/rubocop-rails/pull/1245): Support Prism as a Ruby parser. ([@koic][])

lib/rubocop/cop/mixin/active_record_helper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ def external_dependency_checksum
3939
end
4040

4141
def schema
42-
RuboCop::Rails::SchemaLoader.load(target_ruby_version)
42+
# For compatibility with RuboCop 1.61.0 or lower.
43+
if respond_to?(:parser_engine)
44+
RuboCop::Rails::SchemaLoader.load(target_ruby_version, parser_engine)
45+
else
46+
RuboCop::Rails::SchemaLoader.load(target_ruby_version, :parser_whitequark)
47+
end
4348
end
4449

4550
def table_name(class_node)

lib/rubocop/rails/schema_loader.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ module SchemaLoader
1212
# So a cop that uses the loader should handle `nil` properly.
1313
#
1414
# @return [Schema, nil]
15-
def load(target_ruby_version)
15+
def load(target_ruby_version, parser_engine)
1616
return @load if defined?(@load)
1717

18-
@load = load!(target_ruby_version)
18+
@load = load!(target_ruby_version, parser_engine)
1919
end
2020

2121
def reset!
@@ -38,23 +38,13 @@ def db_schema_path
3838

3939
private
4040

41-
def load!(target_ruby_version)
41+
def load!(target_ruby_version, parser_engine)
4242
path = db_schema_path
4343
return unless path
4444

45-
ast = parse(path, target_ruby_version)
46-
Schema.new(ast) if ast
47-
end
48-
49-
def parse(path, target_ruby_version)
50-
klass_name = :"Ruby#{target_ruby_version.to_s.sub('.', '')}"
51-
klass = ::Parser.const_get(klass_name)
52-
parser = klass.new(RuboCop::AST::Builder.new)
45+
ast = RuboCop::ProcessedSource.new(File.read(path), target_ruby_version, path, parser_engine: parser_engine).ast
5346

54-
buffer = Parser::Source::Buffer.new(path, 1)
55-
buffer.source = path.read
56-
57-
parser.parse(buffer)
47+
Schema.new(ast) if ast
5848
end
5949
end
6050
end

rubocop-rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ Gem::Specification.new do |s|
3636
# introduced in rack 1.1
3737
s.add_runtime_dependency 'rack', '>= 1.1'
3838
s.add_runtime_dependency 'rubocop', '>= 1.33.0', '< 2.0'
39-
s.add_runtime_dependency 'rubocop-ast', '>= 1.30.0', '< 2.0'
39+
s.add_runtime_dependency 'rubocop-ast', '>= 1.31.1', '< 2.0'
4040
end

spec/rubocop/cop/rails/blank_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
RSpec.describe RuboCop::Cop::Rails::Blank, :config do
44
shared_examples 'offense' do |source, correction, message|
5-
it 'registers an offense and corrects' do
5+
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
6+
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
7+
it 'registers an offense and corrects', broken_on: :prism do
68
expect_offense(<<~RUBY, source: source, message: message)
79
#{source}
810
^{source} #{message}

spec/rubocop/cop/rails/exit_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
RUBY
99
end
1010

11-
it 'registers an offense for an exit! call with no receiver' do
11+
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
12+
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
13+
it 'registers an offense for an exit! call with no receiver', broken_on: :prism do
1214
expect_offense(<<~RUBY)
1315
exit!
1416
^^^^^ Do not use `exit` in Rails applications.

spec/rubocop/cop/rails/index_by_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
end
176176
end
177177

178-
context 'when using Ruby 2.5 or older', :ruby25 do
178+
context 'when using Ruby 2.5 or older', :ruby25, unsupported_on: :prism do
179179
it 'does not register an offense for `to_h { ... }`' do
180180
expect_no_offenses(<<~RUBY)
181181
x.to_h { |el| [el.to_sym, el] }

0 commit comments

Comments
 (0)