Skip to content

Commit 0a80131

Browse files
committed
Change Rails/EnumSyntax to autocorrect underscored options
1 parent b59cecf commit 0a80131

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1350](https://github.com/rubocop/rubocop-rails/pull/1350): Change `Rails/EnumSyntax` to autocorrect underscored options. ([@fatkodima][])

lib/rubocop/cop/rails/enum_syntax.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ def check_and_correct_keyword_args(node)
5757
def check_enum_options(node)
5858
enum_with_options?(node) do |key, _, options|
5959
options.children.each do |option|
60-
add_offense(option.key, message: format(MSG_OPTIONS, enum: enum_name_value(key))) if option_key?(option)
60+
next unless option_key?(option)
61+
62+
add_offense(option.key, message: format(MSG_OPTIONS, enum: enum_name_value(key))) do |corrector|
63+
corrector.replace(option.key, option.key.source.delete_prefix('_'))
64+
end
6165
end
6266
end
6367
end

spec/rubocop/cop/rails/enum_syntax_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@
4747
end
4848

4949
context 'with options prefixed with `_`' do
50-
it 'registers an offense' do
50+
it 'registers an offense and corrects' do
5151
expect_offense(<<~RUBY)
5252
enum :status, { active: 0, archived: 1 }, _prefix: true, _suffix: true
5353
^^^^^^^ Enum defined with deprecated options in `status` enum declaration. Remove the `_` prefix.
5454
^^^^^^^ Enum defined with deprecated options in `status` enum declaration. Remove the `_` prefix.
5555
RUBY
56+
57+
expect_correction(<<~RUBY)
58+
enum :status, { active: 0, archived: 1 }, prefix: true, suffix: true
59+
RUBY
5660
end
5761
end
5862

0 commit comments

Comments
 (0)