Skip to content

Commit b50f629

Browse files
authored
Merge pull request #1559 from koic/fix_incorrect_autocorrect_for_rails_redirect_back_or_to
[Fix #1558] Fix incorrect autocorrect for `Rails/RedirectBackOrTo`
2 parents 0f0be1c + a78a026 commit b50f629

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1558](https://github.com/rubocop/rubocop-rails/issues/1558): This PR fixes incorrect autocorrect for `Rails/RedirectBackOrTo` when additional options as double splat are used. ([@koic][])

lib/rubocop/cop/rails/redirect_back_or_to.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,36 @@ class RedirectBackOrTo < Base
3333

3434
def_node_matcher :redirect_back_with_fallback_location, <<~PATTERN
3535
(send nil? :redirect_back
36-
(hash <$(pair (sym :fallback_location) $_) ...>)
36+
(hash <$(pair (sym :fallback_location) $_) $...>)
3737
)
3838
PATTERN
3939

4040
def on_send(node)
41-
redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value|
41+
redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value, options|
4242
add_offense(node.loc.selector) do |corrector|
43-
correct_redirect_back(corrector, node, fallback_pair, fallback_value)
43+
correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
4444
end
4545
end
4646
end
4747

4848
private
4949

50-
def correct_redirect_back(corrector, node, fallback_pair, fallback_value)
50+
# rubocop:disable Metrics/AbcSize
51+
def correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
5152
corrector.replace(node.loc.selector, 'redirect_back_or_to')
5253

5354
hash_arg = node.first_argument
5455

5556
if hash_arg.pairs.one?
56-
corrector.replace(hash_arg, fallback_value.source)
57+
arguments = [fallback_value.source] + options.map(&:source)
58+
corrector.replace(hash_arg, arguments.join(', '))
5759
else
5860
remove_fallback_location_pair(corrector, hash_arg, fallback_pair)
5961
first_pair = hash_arg.pairs.find { |pair| pair != fallback_pair }
6062
corrector.insert_before(first_pair, "#{fallback_value.source}, ")
6163
end
6264
end
65+
# rubocop:enable Metrics/AbcSize
6366

6467
def remove_fallback_location_pair(corrector, hash_node, fallback_pair)
6568
pairs = hash_node.pairs

spec/rubocop/cop/rails/redirect_back_or_to_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
RUBY
2525
end
2626

27+
it 'registers an offense and corrects with additional options as double splat' do
28+
expect_offense(<<~RUBY)
29+
redirect_back(fallback_location: root_path, **options)
30+
^^^^^^^^^^^^^ Use `redirect_back_or_to` instead of `redirect_back` with `:fallback_location` keyword argument.
31+
RUBY
32+
33+
expect_correction(<<~RUBY)
34+
redirect_back_or_to(root_path, **options)
35+
RUBY
36+
end
37+
2738
it 'registers no offense when using redirect_back_or_to' do
2839
expect_no_offenses(<<~RUBY)
2940
redirect_back_or_to(root_path)

0 commit comments

Comments
 (0)