Skip to content

Commit 9064595

Browse files
committed
[Fix #6737] Fix an incorrect auto-correct for Rails/LinkToBlank
Fixes #6737. This PR fixes an incorrect auto-correct for `Rails/LinkToBlank` when `link_to` method arguments are enclosed in parentheses.
1 parent 4c4e4fa commit 9064595

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/rubocop/cop/rails/link_to_blank.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ def append_to_rel(rel_node, corrector)
7474
def add_rel(send_node, offence_node, corrector)
7575
quote_style = offence_node.children.last.source[0]
7676
new_rel_exp = ", rel: #{quote_style}noopener#{quote_style}"
77-
corrector.insert_after(send_node.loc.expression, new_rel_exp)
77+
range = send_node.arguments.last.source_range
78+
79+
corrector.insert_after(range, new_rel_exp)
7880
end
7981

8082
def contains_noopener?(str)

spec/rubocop/cop/rails/link_to_blank_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@
5959
end
6060
RUBY
6161
end
62+
63+
it 'autocorrects with a new rel when using the block syntax ' \
64+
'with parenthesis' do
65+
new_source = autocorrect_source(<<-RUBY.strip_indent)
66+
link_to('https://www.example.com', target: '_blank') do
67+
"Click here"
68+
end
69+
RUBY
70+
71+
expect(new_source).to eq(<<-RUBY.strip_indent)
72+
link_to('https://www.example.com', target: '_blank', rel: 'noopener') do
73+
"Click here"
74+
end
75+
RUBY
76+
end
6277
end
6378

6479
context 'when using rel' do

0 commit comments

Comments
 (0)