Skip to content

Commit

Permalink
Merge pull request #6743 from koic/fix_incorrect_autocorrect_for_rail…
Browse files Browse the repository at this point in the history
…s_link_to_blank

[Fix #6737] Fix an incorrect auto-correct for `Rails/LinkToBlank`
  • Loading branch information
koic committed Feb 7, 2019
2 parents b675f26 + 810c92f commit 24e2c79
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rails/link_to_blank.rb
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/rails/link_to_blank_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 24e2c79

Please sign in to comment.