Skip to content

Commit

Permalink
Fix Rails/LinkToBlank auto-correct bug when using symbol for target
Browse files Browse the repository at this point in the history
  • Loading branch information
r7kamura authored and bbatsov committed Apr 1, 2019
1 parent 22f0da7 commit 81dcb45
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* [#6856](https://github.com/rubocop-hq/rubocop/pull/6856): Fix auto-correction for `Style/BlockComments` when the file is missing a trailing blank line. ([@ericsullivan][])
* [#6858](https://github.com/rubocop-hq/rubocop/issues/6858): Fix an incorrect auto-correct for `Lint/ToJSON` when there are no `to_json` arguments. ([@koic][])
* [#6865](https://github.com/rubocop-hq/rubocop/pull/6865): Fix deactivated `StyleGuideBaseURL` for `Layout/ClassStructure`. ([@aeroastro][])
* [#6868](https://github.com/rubocop-hq/rubocop/pull/6868): Fix `Rails/LinkToBlank` auto-correct bug when using symbol for target. ([@r7kamura][])

### Changes

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/rails/link_to_blank.rb
Expand Up @@ -72,8 +72,9 @@ def append_to_rel(rel_node, corrector)
end

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}"
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

corrector.insert_after(range, new_rel_exp)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rails/link_to_blank_spec.rb
Expand Up @@ -81,6 +81,17 @@
end
RUBY
end

it 'autocorrects with a new rel when using a symbol for the target \
value' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: :_blank
RUBY

expect(new_source).to eq(<<-RUBY.strip_indent)
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 81dcb45

Please sign in to comment.