diff --git a/CHANGELOG.md b/CHANGELOG.md index d9be78ffb62..0b034b41660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#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][]) +* [#6869](https://github.com/rubocop-hq/rubocop/pull/6869): Fix false positive for `Rails/LinkToBlank` when rel is a symbol value. ([@r7kamura][]) ### Changes diff --git a/lib/rubocop/cop/rails/link_to_blank.rb b/lib/rubocop/cop/rails/link_to_blank.rb index 2731916a567..5b0cfc1b0da 100644 --- a/lib/rubocop/cop/rails/link_to_blank.rb +++ b/lib/rubocop/cop/rails/link_to_blank.rb @@ -22,7 +22,7 @@ class LinkToBlank < Cop PATTERN def_node_matcher :includes_noopener?, <<-PATTERN - (pair {(sym :rel) (str "rel")} (str #contains_noopener?)) + (pair {(sym :rel) (str "rel")} ({str sym} #contains_noopener?)) PATTERN def_node_matcher :rel_node?, <<-PATTERN @@ -80,10 +80,10 @@ def add_rel(send_node, offence_node, corrector) corrector.insert_after(range, new_rel_exp) end - def contains_noopener?(str) - return false unless str + def contains_noopener?(value) + return false unless value - str.split(' ').include?('noopener') + value.to_s.split(' ').include?('noopener') end end end diff --git a/spec/rubocop/cop/rails/link_to_blank_spec.rb b/spec/rubocop/cop/rails/link_to_blank_spec.rb index 1bb551a5a6a..bdcca1ff183 100644 --- a/spec/rubocop/cop/rails/link_to_blank_spec.rb +++ b/spec/rubocop/cop/rails/link_to_blank_spec.rb @@ -115,6 +115,14 @@ RUBY end end + + context 'when the rel is symbol noopener' do + it 'register no offence' do + expect_no_offenses(<<-RUBY.strip_indent) + link_to 'Click here', 'https://www.example.com', target: :_blank, rel: :noopener + RUBY + end + end end end end