Skip to content

Commit

Permalink
Merge pull request #1350 from fatkodima/change-enum_syntax-to-autocor…
Browse files Browse the repository at this point in the history
…rect-underscored-options

Change `Rails/EnumSyntax` to autocorrect underscored options
  • Loading branch information
koic authored Sep 3, 2024
2 parents b59cecf + 0a80131 commit ed33413
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1350](https://github.com/rubocop/rubocop-rails/pull/1350): Change `Rails/EnumSyntax` to autocorrect underscored options. ([@fatkodima][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/enum_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def check_and_correct_keyword_args(node)
def check_enum_options(node)
enum_with_options?(node) do |key, _, options|
options.children.each do |option|
add_offense(option.key, message: format(MSG_OPTIONS, enum: enum_name_value(key))) if option_key?(option)
next unless option_key?(option)

add_offense(option.key, message: format(MSG_OPTIONS, enum: enum_name_value(key))) do |corrector|
corrector.replace(option.key, option.key.source.delete_prefix('_'))
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/rubocop/cop/rails/enum_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@
end

context 'with options prefixed with `_`' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
enum :status, { active: 0, archived: 1 }, _prefix: true, _suffix: true
^^^^^^^ Enum defined with deprecated options in `status` enum declaration. Remove the `_` prefix.
^^^^^^^ Enum defined with deprecated options in `status` enum declaration. Remove the `_` prefix.
RUBY

expect_correction(<<~RUBY)
enum :status, { active: 0, archived: 1 }, prefix: true, suffix: true
RUBY
end
end

Expand Down

0 comments on commit ed33413

Please sign in to comment.