diff --git a/CHANGELOG.md b/CHANGELOG.md index c0082f2a5af..6ffaa05c144 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [#6710](https://github.com/rubocop-hq/rubocop/issues/6710): Fix `Naming/MemoizedInstanceVariableName` on method starts with underscore. ([@pocke][]) * [#6722](https://github.com/rubocop-hq/rubocop/issues/6722): Fix an error for `Style/OneLineConditional` when `then` branch has no body. ([@koic][]) * [#6702](https://github.com/rubocop-hq/rubocop/pull/6702): Fix `TrailingComma` regression where heredoc with commas caused false positives. ([@abrom][]) +* [#6737](https://github.com/rubocop-hq/rubocop/issues/6737): Fix an incorrect auto-correct for `Rails/LinkToBlank` when `link_to` method arguments are enclosed in parentheses. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/rails/link_to_blank.rb b/lib/rubocop/cop/rails/link_to_blank.rb index d27b1ef04cc..70417ac2cfa 100644 --- a/lib/rubocop/cop/rails/link_to_blank.rb +++ b/lib/rubocop/cop/rails/link_to_blank.rb @@ -74,7 +74,9 @@ def append_to_rel(rel_node, corrector) def add_rel(send_node, offence_node, corrector) quote_style = offence_node.children.last.source[0] new_rel_exp = ", rel: #{quote_style}noopener#{quote_style}" - corrector.insert_after(send_node.loc.expression, new_rel_exp) + range = send_node.arguments.last.source_range + + corrector.insert_after(range, new_rel_exp) end def contains_noopener?(str) diff --git a/spec/rubocop/cop/rails/link_to_blank_spec.rb b/spec/rubocop/cop/rails/link_to_blank_spec.rb index dd1120f0c57..1c0b67c54fa 100644 --- a/spec/rubocop/cop/rails/link_to_blank_spec.rb +++ b/spec/rubocop/cop/rails/link_to_blank_spec.rb @@ -59,6 +59,21 @@ end RUBY end + + it 'autocorrects with a new rel when using the block syntax ' \ + 'with parenthesis' do + new_source = autocorrect_source(<<-RUBY.strip_indent) + link_to('https://www.example.com', target: '_blank') do + "Click here" + end + RUBY + + expect(new_source).to eq(<<-RUBY.strip_indent) + link_to('https://www.example.com', target: '_blank', rel: 'noopener') do + "Click here" + end + RUBY + end end context 'when using rel' do