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..a80260a127 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, { clow: 1 }, prefix + RUBY + end + end end context 'Rails >= 7.0 and Ruby <= 2.7', :rails70, :ruby27, unsupported_on: :prism do