diff --git a/changelog/fix_false_positives_for_rails_enum_syntax.md b/changelog/fix_false_positives_for_rails_enum_syntax.md new file mode 100644 index 0000000000..914d96ff49 --- /dev/null +++ b/changelog/fix_false_positives_for_rails_enum_syntax.md @@ -0,0 +1 @@ +* [#1362](https://github.com/rubocop/rubocop-rails/issues/1362): Fix false positives for `Rails/EnumSyntax` when using Ruby 2.7. ([@koic][]) diff --git a/lib/rubocop/cop/rails/enum_syntax.rb b/lib/rubocop/cop/rails/enum_syntax.rb index a3d650440a..5b3d683916 100644 --- a/lib/rubocop/cop/rails/enum_syntax.rb +++ b/lib/rubocop/cop/rails/enum_syntax.rb @@ -17,8 +17,10 @@ module Rails # class EnumSyntax < Base extend AutoCorrector + extend TargetRubyVersion extend TargetRailsVersion + minimum_target_ruby_version 3.0 minimum_target_rails_version 7.0 MSG = 'Enum defined with keyword arguments in `%s` enum declaration. Use positional arguments instead.' diff --git a/spec/rubocop/cop/rails/enum_syntax_spec.rb b/spec/rubocop/cop/rails/enum_syntax_spec.rb index 9799fe66d1..f80aafe2a0 100644 --- a/spec/rubocop/cop/rails/enum_syntax_spec.rb +++ b/spec/rubocop/cop/rails/enum_syntax_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Rails::EnumSyntax, :config do - context 'Rails >= 7.0', :rails70 do + context 'Rails >= 7.0 and Ruby >= 3.0', :rails70, :ruby30 do context 'when keyword arguments are used' do context 'with %i[] syntax' do it 'registers an offense' do @@ -148,6 +148,18 @@ end end + context 'Rails >= 7.0 and Ruby <= 2.7', :rails70, :ruby27 do + context 'when keyword arguments are used' do + context 'with %i[] syntax' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + enum status: %i[active archived], _prefix: true + RUBY + end + end + end + end + context 'Rails <= 6.1', :rails61 do context 'when keyword arguments are used' do context 'with %i[] syntax' do