Skip to content

Commit

Permalink
Merge pull request #12581 from eugeneius/trailing_line_continuation
Browse files Browse the repository at this point in the history
Handle trailing line continuation in Layout/LineContinuationLeadingSpace
  • Loading branch information
koic committed Dec 29, 2023
2 parents f848986 + be6b3e9 commit 1cadf98
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12581](https://github.com/rubocop/rubocop/pull/12581): Handle trailing line continuation in `Layout/LineContinuationLeadingSpace`. ([@eugeneius][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/line_continuation_leading_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ 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? }
node.children.none? { |c| (c.first_line...c.last_line).cover?(line_num) && c.multiline? }
end

def autocorrect(corrector, offense_range, insert_pos, spaces)
Expand Down
23 changes: 23 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 @@ -113,6 +113,29 @@
RUBY
end

it 'registers no offense when multiline string ends with a line continuation' do
expect_no_offenses(<<~'RUBY')
"" "foo
bar\
"
RUBY
end

it 'registers an offense when multiline string ends on 1st line' do
expect_offense(<<~'RUBY')
"foo
bar" \
" baz"
^ Move leading spaces to the end of previous line.
RUBY

expect_correction(<<~'RUBY')
"foo
bar " \
"baz"
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 1cadf98

Please sign in to comment.