Skip to content

Commit

Permalink
[Fix rubocop#1362] Fix false positives for Rails/EnumSyntax
Browse files Browse the repository at this point in the history
Fixes rubocop#1362.

This PR fixes false positives for `Rails/EnumSyntax` when using Ruby 2.7.

The warning shown in rubocop#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 rubocop#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 rubocop#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.
  • Loading branch information
koic committed Sep 12, 2024
1 parent cbb7257 commit 490b0fb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_positives_for_rails_enum_syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1362](https://github.com/rubocop/rubocop-rails/issues/1362): Fix false positives for `Rails/EnumSyntax` when using Ruby 2.7. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/enum_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 `%<enum>s` enum declaration. Use positional arguments instead.'
Expand Down
14 changes: 13 additions & 1 deletion spec/rubocop/cop/rails/enum_syntax_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 490b0fb

Please sign in to comment.