From 0a80131242a53569e6a96a996c86e65da2068320 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Tue, 3 Sep 2024 19:24:27 +0300 Subject: [PATCH] Change `Rails/EnumSyntax` to autocorrect underscored options --- ...change_enum_syntax_to_autoccorect_underscored_options.md | 1 + lib/rubocop/cop/rails/enum_syntax.rb | 6 +++++- spec/rubocop/cop/rails/enum_syntax_spec.rb | 6 +++++- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelog/change_enum_syntax_to_autoccorect_underscored_options.md diff --git a/changelog/change_enum_syntax_to_autoccorect_underscored_options.md b/changelog/change_enum_syntax_to_autoccorect_underscored_options.md new file mode 100644 index 0000000000..f497ea13de --- /dev/null +++ b/changelog/change_enum_syntax_to_autoccorect_underscored_options.md @@ -0,0 +1 @@ +* [#1350](https://github.com/rubocop/rubocop-rails/pull/1350): Change `Rails/EnumSyntax` to autocorrect underscored options. ([@fatkodima][]) diff --git a/lib/rubocop/cop/rails/enum_syntax.rb b/lib/rubocop/cop/rails/enum_syntax.rb index 9a36dadfef..a3d650440a 100644 --- a/lib/rubocop/cop/rails/enum_syntax.rb +++ b/lib/rubocop/cop/rails/enum_syntax.rb @@ -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 diff --git a/spec/rubocop/cop/rails/enum_syntax_spec.rb b/spec/rubocop/cop/rails/enum_syntax_spec.rb index 80c698cc05..9799fe66d1 100644 --- a/spec/rubocop/cop/rails/enum_syntax_spec.rb +++ b/spec/rubocop/cop/rails/enum_syntax_spec.rb @@ -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