From 94a8370ff480c712214f396ac5eb988d31d0e072 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 18 Oct 2024 12:07:11 +0900 Subject: [PATCH] [Fix #1377] Fix an error for `Rails/EnumSyntax` Fixes #1377 This PR fixes an error for `Rails/EnumSyntax` when positional arguments are used and options are not passed as keyword arguments. --- changelog/fix_an_error_for_rails_enum_syntax.md | 1 + lib/rubocop/cop/rails/enum_syntax.rb | 2 ++ spec/rubocop/cop/rails/enum_syntax_spec.rb | 8 ++++++++ 3 files changed, 11 insertions(+) create mode 100644 changelog/fix_an_error_for_rails_enum_syntax.md diff --git a/changelog/fix_an_error_for_rails_enum_syntax.md b/changelog/fix_an_error_for_rails_enum_syntax.md new file mode 100644 index 0000000000..0ad353b258 --- /dev/null +++ b/changelog/fix_an_error_for_rails_enum_syntax.md @@ -0,0 +1 @@ +* [#1377](https://github.com/rubocop/rubocop-rails/issues/1377): Fix an error for `Rails/EnumSyntax` when positional arguments are used and options are not passed as keyword arguments. ([@koic][]) diff --git a/lib/rubocop/cop/rails/enum_syntax.rb b/lib/rubocop/cop/rails/enum_syntax.rb index 5b3d683916..6f603a3056 100644 --- a/lib/rubocop/cop/rails/enum_syntax.rb +++ b/lib/rubocop/cop/rails/enum_syntax.rb @@ -106,6 +106,8 @@ def enum_name(elem) end def option_key?(pair) + return false unless pair.respond_to?(:key) + UNDERSCORED_OPTION_NAMES.include?(pair.key.source) end diff --git a/spec/rubocop/cop/rails/enum_syntax_spec.rb b/spec/rubocop/cop/rails/enum_syntax_spec.rb index b5f20a2436..c51bd496f2 100644 --- a/spec/rubocop/cop/rails/enum_syntax_spec.rb +++ b/spec/rubocop/cop/rails/enum_syntax_spec.rb @@ -146,6 +146,14 @@ expect_no_offenses('enum') end end + + context 'when positional arguments are used and options are not passed as keyword arguments' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + enum :status, { active: 0, archived: 1 }, prefix + RUBY + end + end end context 'Rails >= 7.0 and Ruby <= 2.7', :rails70, :ruby27, unsupported_on: :prism do