Skip to content

Commit 1b3c43d

Browse files
authored
Merge pull request #988 from r7kamura/ignored-skip-action-filter-option-autocorrection
Add autocorrection for `Rails/IgnoredSkipActionFilterOption`
2 parents af24792 + beabfbc commit 1b3c43d

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#988](https://github.com/rubocop/rubocop-rails/pull/988): Add autocorrection for `Rails/IgnoredSkipActionFilterOption`. ([@r7kamura][])

lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ module Rails
3636
# if: -> { trusted_origin? && action_name != "admin" }
3737
# end
3838
class IgnoredSkipActionFilterOption < Base
39+
extend AutoCorrector
40+
41+
include RangeHelp
42+
3943
MSG = <<~MSG.chomp.freeze
4044
`%<ignore>s` option will be ignored when `%<prefer>s` and `%<ignore>s` are used together.
4145
MSG
@@ -60,9 +64,13 @@ def on_send(node)
6064
options = options_hash(options)
6165

6266
if if_and_only?(options)
63-
add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if))
67+
add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if)) do |corrector|
68+
remove_node_with_left_space_and_comma(corrector, options[:if])
69+
end
6470
elsif if_and_except?(options)
65-
add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except))
71+
add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except)) do |corrector|
72+
remove_node_with_left_space_and_comma(corrector, options[:except])
73+
end
6674
end
6775
end
6876

@@ -81,6 +89,18 @@ def if_and_only?(options)
8189
def if_and_except?(options)
8290
options.key?(:if) && options.key?(:except)
8391
end
92+
93+
def remove_node_with_left_space_and_comma(corrector, node)
94+
corrector.remove(
95+
range_with_surrounding_comma(
96+
range_with_surrounding_space(
97+
node.source_range,
98+
side: :left
99+
),
100+
:left
101+
)
102+
)
103+
end
84104
end
85105
end
86106
end

spec/rubocop/cop/rails/ignored_skip_action_filter_option_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
skip_before_action :login_required, only: :show, if: :trusted_origin?
77
^^^^^^^^^^^^^^^^^^^^ `if` option will be ignored when `only` and `if` are used together.
88
RUBY
9+
10+
expect_correction(<<~RUBY)
11+
skip_before_action :login_required, only: :show
12+
RUBY
913
end
1014

1115
it 'registers an offense when `if` and `except` are used together' do
1216
expect_offense(<<~RUBY)
1317
skip_before_action :login_required, except: :admin, if: :trusted_origin?
1418
^^^^^^^^^^^^^^ `except` option will be ignored when `if` and `except` are used together.
1519
RUBY
20+
21+
expect_correction(<<~RUBY)
22+
skip_before_action :login_required, if: :trusted_origin?
23+
RUBY
1624
end
1725

1826
it 'does not register an offense when `if` is used only' do

0 commit comments

Comments
 (0)