Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rails/LinkToBlank auto-correct bug when using symbol for target #6868

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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