Skip to content

Commit

Permalink
Merge pull request #423 from koic/fix_incorrect_autocorrect_for_rails…
Browse files Browse the repository at this point in the history
…_link_to_blank

[Fix #421] Fix incorrect auto-correct for `Rails/LinkToBlank`
  • Loading branch information
koic authored Jan 17, 2021
2 parents f2613d6 + ebff591 commit 0e99ca4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#421](https://github.com/rubocop-hq/rubocop-rails/issues/421): Fix incorrect auto-correct for `Rails/LinkToBlank` when using `target: '_blank'` with hash brackets for the option. ([@koic][])

### Changes

* [#409](https://github.com/rubocop-hq/rubocop-rails/pull/409): Deconstruct "table.column" in `Rails/WhereNot`. ([@mobilutz][])
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/link_to_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def add_rel(send_node, offence_node, corrector)
opening_quote = offence_node.children.last.source[0]
closing_quote = opening_quote == ':' ? '' : opening_quote
new_rel_exp = ", rel: #{opening_quote}noopener#{closing_quote}"
range = send_node.arguments.last.source_range
range = if (last_argument = send_node.last_argument).hash_type?
last_argument.pairs.last.source_range
else
last_argument.source_range
end

corrector.insert_after(range, new_rel_exp)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rails/link_to_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@
link_to 'Click here', 'https://www.example.com', target: :_blank, rel: :noopener
RUBY
end

it 'registers and corrects an offence when using hash brackets for the option' do
expect_offense(<<~RUBY)
link_to 'Click here', 'https://www.example.com', { target: :_blank }
^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener.
RUBY

expect_correction(<<~RUBY)
link_to 'Click here', 'https://www.example.com', { target: :_blank, rel: :noopener }
RUBY
end
end

context 'when using rel' do
Expand Down

0 comments on commit 0e99ca4

Please sign in to comment.