Skip to content

Commit

Permalink
Merge pull request #12789 from boardfish/redundant-percent-q-multiline
Browse files Browse the repository at this point in the history
Make Style/RedundantPercentQ handle multiline source safely
  • Loading branch information
koic committed Mar 15, 2024
2 parents 66c8276 + 72bea19 commit 9dd0792
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12789](https://github.com/rubocop/rubocop/pull/12789): Make `Style/RedundantPercentQ` safe on multiline strings. ([@boardfish][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_percent_q.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def check(node)
return if interpolated_quotes?(node) || allowed_percent_q?(node)

add_offense(node) do |corrector|
delimiter = /^%Q[^"]+$|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE
delimiter = /\A%Q[^"]+\z|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE

corrector.replace(node.loc.begin, delimiter)
corrector.replace(node.loc.end, delimiter)
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/redundant_percent_q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@
'boogers'
RUBY
end

it 'autocorrects safely for multiline strings containing double quotes' do
expect_offense(<<~RUBY)
%Q(
^^^ Use `%Q` only for strings that contain both single [...]
Quoth the Raven "Nevermore."
)
RUBY

expect_correction(<<~RUBY)
'
Quoth the Raven "Nevermore."
'
RUBY
end
end

it 'accepts a heredoc string that contains %q' do
Expand Down

0 comments on commit 9dd0792

Please sign in to comment.