Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Rails/EnumSyntax to autocorrect underscored options #1350

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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