Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #11346] Fix a false positive for Style/RedundantStringEscape #11347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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