Skip to content

Commit

Permalink
[Fix #11346] Fix a false positive for Style/RedundantStringEscape
Browse files Browse the repository at this point in the history
Fixes #11346.

This PR fixes a false positive for `Style/RedundantStringEscape`
when using escaped space in heredoc.
  • Loading branch information
koic committed Dec 28, 2022
1 parent 742bef3 commit 7cdede1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11346](https://github.com/rubocop/rubocop/issues/11346): Fix a false positive for `Style/RedundantStringEscape` when using escaped space in heredoc. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/redundant_string_escape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def begin_loc_present?(node)
node.loc.to_hash.key?(:begin) && !node.loc.begin.nil?
end

# rubocop:disable Metrics/CyclomaticComplexity
def allowed_escape?(node, range)
escaped = range.source[(1..-1)]

Expand All @@ -88,13 +89,14 @@ def allowed_escape?(node, range)
# with different versions of Ruby so that e.g. /\d/ != /d/
return true if /[\n\\[[:alnum:]]]/.match?(escaped[0])

return true if escaped[0] == ' ' && percent_array_literal?(node)
return true if escaped[0] == ' ' && (percent_array_literal?(node) || node.heredoc?)

return true if disabling_interpolation?(range)
return true if delimiter?(node, escaped[0])

false
end
# rubocop:enable Metrics/CyclomaticComplexity

def interpolation_not_enabled?(node)
single_quoted?(node) ||
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/redundant_string_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ def wrap(contents)
MYHEREDOC
RUBY
end
it 'does register an offense an escaped space' do
expect_no_offenses(<<~'RUBY')
<<~MYHEREDOC
\ text
MYHEREDOC
RUBY
end
end
context 'with an interpolation-disabled HEREDOC' do
Expand Down

0 comments on commit 7cdede1

Please sign in to comment.