diff --git a/CHANGELOG.md b/CHANGELOG.md index fa0e8125687..9c6f147e3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/rails/link_to_blank.rb b/lib/rubocop/cop/rails/link_to_blank.rb index 1d03e0329e8..2731916a567 100644 --- a/lib/rubocop/cop/rails/link_to_blank.rb +++ b/lib/rubocop/cop/rails/link_to_blank.rb @@ -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) diff --git a/spec/rubocop/cop/rails/link_to_blank_spec.rb b/spec/rubocop/cop/rails/link_to_blank_spec.rb index a9c8cbc4960..1bb551a5a6a 100644 --- a/spec/rubocop/cop/rails/link_to_blank_spec.rb +++ b/spec/rubocop/cop/rails/link_to_blank_spec.rb @@ -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