From 50fe9a72fd1fa2012b05c1c2909427bf762760a0 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 12 Sep 2024 12:30:58 +0900 Subject: [PATCH] [Fix #1362] Fix false positives for `Rails/EnumSyntax` Fixes #1362. This PR fixes false positives for `Rails/EnumSyntax` when using Ruby 2.7. The warning shown in https://github.com/rubocop/rubocop-rails/issues/1238 appears starting from Rails 7.2 (Requires Ruby 3.1+). On the other hand, if users use Ruby 2.7, the warning reported in https://github.com/rubocop/rubocop-rails/issues/1362 will be displayed. Therefore, it would be appropriate to enable this cop only when analyzing Ruby 3.0+. Nothing will happen when using Ruby 2.7 with Rails 7.0 or Rails 7.1, but the warning in https://github.com/rubocop/rubocop-rails/issues/1238 will not be displayed either. Meanwhile, in Rails 7.2, which requires Ruby 3.1+, Ruby 2.7 cannot be used, so there is no issue. --- .../fix_false_positives_for_rails_enum_syntax.md | 1 + lib/rubocop/cop/rails/enum_syntax.rb | 2 ++ spec/rubocop/cop/rails/enum_syntax_spec.rb | 14 +++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positives_for_rails_enum_syntax.md 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..b5f20a2436 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, unsupported_on: :prism 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