Skip to content

Commit

Permalink
[Fix rubocop#12138] Fix a false positive for `Layout/LineContinuation…
Browse files Browse the repository at this point in the history
…LeadingSpace`

Fixes rubocop#12138.

This PR fixes a false positive for `Layout/LineContinuationLeadingSpace`
when a backslash is part of a multiline string literal.
  • Loading branch information
ymap committed Aug 23, 2023
1 parent 51ed9ab commit 5b47705
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12138](https://github.com/rubocop/rubocop/issues/12138): Fix a false positive for `Layout/LineContinuationLeadingSpace` when a backslash is part of a multiline string literal. ([@ymap][])
26 changes: 17 additions & 9 deletions lib/rubocop/cop/layout/line_continuation_leading_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,13 @@ def on_dstr(node)

end_of_first_line = node.source_range.begin_pos - node.source_range.column

raw_lines(node).each_cons(2) do |raw_line_one, raw_line_two|
lines = raw_lines(node)
lines.each_cons(2).with_index(node.first_line) do |(raw_line_one, raw_line_two), line_num|
end_of_first_line += raw_line_one.length

next unless continuation?(raw_line_one)
next unless continuation?(raw_line_one, line_num, node)

if enforced_style_leading?
investigate_leading_style(raw_line_one, raw_line_two, end_of_first_line)
else
investigate_trailing_style(raw_line_one, raw_line_two, end_of_first_line)
end
investigate(raw_line_one, raw_line_two, end_of_first_line)
end
end

Expand All @@ -76,6 +73,14 @@ def raw_lines(node)
processed_source.raw_source.lines[node.first_line - 1, line_range(node).size]
end

def investigate(first_line, second_line, end_of_first_line)
if enforced_style_leading?
investigate_leading_style(first_line, second_line, end_of_first_line)
else
investigate_trailing_style(first_line, second_line, end_of_first_line)
end
end

def investigate_leading_style(first_line, second_line, end_of_first_line)
matches = first_line.match(LEADING_STYLE_OFFENSE)
return if matches.nil?
Expand All @@ -98,8 +103,11 @@ def investigate_trailing_style(first_line, second_line, end_of_first_line)
end
end

def continuation?(line)
line.end_with?("\\\n")
def continuation?(line, line_num, node)
return false unless line.end_with?("\\\n")

# Ensure backslash isn't part of a token spanning to the next line.
node.children.none? { |c| c.first_line == line_num && c.multiline? }
end

def autocorrect(corrector, offense_range, insert_pos, spaces)
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/layout/line_continuation_leading_space_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@
RUBY
end

it 'registers no offense for multiline string with backslash' do
expect_no_offenses(<<~'RUBY')
'"this text is too" \
" long"'
RUBY
end

describe 'interpolated strings' do
it 'registers no offense on interpolated string alone' do
expect_no_offenses(<<~'RUBY')
Expand Down Expand Up @@ -283,6 +290,13 @@
RUBY
end

it 'registers no offense for multiline string with backslash' do
expect_no_offenses(<<~'RUBY')
'"this text is too " \
"long"'
RUBY
end

describe 'interpolated strings' do
it 'registers no offense on interpolated string alone' do
expect_no_offenses(<<~'RUBY')
Expand Down

0 comments on commit 5b47705

Please sign in to comment.