Skip to content

Commit

Permalink
Merge pull request #11347 from koic/fix_false_positive_style_redundan…
Browse files Browse the repository at this point in the history
…t_string_escape

[Fix #11346] Fix a false positive for `Style/RedundantStringEscape`
  • Loading branch information
koic committed Dec 30, 2022
2 parents aaa4166 + 7cdede1 commit 908eda0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
@@ -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
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
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 908eda0

Please sign in to comment.