Skip to content

Commit

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

Fixes rubocop#10763.

This PR fixes a false positive for `Layout/LineContinuationSpacing`
when using continuation keyword `\` after `__END__`.
  • Loading branch information
koic committed Jun 28, 2022
1 parent f5b77c8 commit 562a9c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
@@ -0,0 +1 @@
* [#10763](https://github.com/rubocop/rubocop/issues/10763): Fix a false positive for `Layout/LineContinuationSpacing` when using continuation keyword `\` after `__END__`. ([@koic][])
10 changes: 10 additions & 0 deletions lib/rubocop/cop/layout/line_continuation_spacing.rb
Expand Up @@ -32,10 +32,14 @@ class LineContinuationSpacing < Base
extend AutoCorrector

def on_new_investigation
last_line = last_line(processed_source)

@ignored_ranges = string_literal_ranges(processed_source.ast) +
comment_ranges(processed_source.comments)

processed_source.raw_source.lines.each_with_index do |line, index|
break if index >= last_line

line_number = index + 1
investigate(line, line_number)
end
Expand Down Expand Up @@ -103,6 +107,12 @@ def comment_ranges(comments)
comments.map(&:loc).map(&:expression)
end

def last_line(processed_source)
last_token = processed_source.tokens.last

last_token ? last_token.line : processed_source.lines.length
end

def ignore_range?(backtick_range)
@ignored_ranges.any? { |range| range.contains?(backtick_range) }
end
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/layout/line_continuation_spacing_spec.rb
Expand Up @@ -79,6 +79,20 @@
X
RUBY
end

it 'ignores when too much space in front of backslash after `__END__`' do
expect_no_offenses(<<~'RUBY')
foo
bar
__END__
baz \
qux
RUBY
end

it 'ignores empty code' do
expect_no_offenses('')
end
end

context 'EnforcedStyle: no_space' do
Expand Down Expand Up @@ -159,5 +173,19 @@
X
RUBY
end

it 'ignores when too much space in front of backslash after `__END__`' do
expect_no_offenses(<<~'RUBY')
foo
bar
__END__
baz \
qux
RUBY
end

it 'ignores empty code' do
expect_no_offenses('')
end
end
end

0 comments on commit 562a9c1

Please sign in to comment.