Skip to content

Commit

Permalink
Merge pull request #12550 from koic/fix_a_false_positive_for_style_re…
Browse files Browse the repository at this point in the history
…dundant_line_continuation

[Fix #12549] Fix a false positive for `Style/RedundantLineContinuation`
  • Loading branch information
koic committed Dec 18, 2023
2 parents 4843acd + fa660e1 commit faf48f1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12549](https://github.com/rubocop/rubocop/issues/12549): Fix a false positive for `Style/RedundantLineContinuation` when line continuations for multiline leading dot method chain with a blank line. ([@koic][])
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/redundant_line_continuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def require_line_continuation?(range)
!ends_with_backslash_without_comment?(range.source_line) ||
string_concatenation?(range.source_line) ||
start_with_arithmetic_operator?(processed_source[range.line]) ||
inside_string_literal_or_method_with_argument?(range)
inside_string_literal_or_method_with_argument?(range) ||
leading_dot_method_chain_with_blank_line?(range)
end

def ends_with_backslash_without_comment?(source_line)
Expand All @@ -113,6 +114,12 @@ def inside_string_literal_or_method_with_argument?(range)
end
end

def leading_dot_method_chain_with_blank_line?(range)
return false unless range.source_line.strip.start_with?('.', '&.')

processed_source[range.line].strip.empty?
end

def redundant_line_continuation?(range)
return true unless (node = find_node_for_line(range.line))
return false if argument_newline?(node)
Expand Down
74 changes: 74 additions & 0 deletions spec/rubocop/cop/style/redundant_line_continuation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,80 @@ def self.foo(bar,#{trailing_whitespace}
RUBY
end

it 'does not register an offense when required line continuations for multiline leading dot method chain with an empty line' do
expect_no_offenses(<<~'RUBY')
obj
.foo(42) \
.bar
RUBY
end

it 'does not register an offense when required line continuations for multiline leading dot safe navigation method chain with an empty line' do
expect_no_offenses(<<~'RUBY')
obj
&.foo(42) \
.bar
RUBY
end

it 'does not register an offense when required line continuations for multiline leading dot method chain with a blank line' do
expect_no_offenses(<<~RUBY)
obj
.foo(42) \\
#{' '}
.bar
RUBY
end

it 'registers an offense when redundant line continuations for multiline leading dot method chain without an empty line' do
expect_offense(<<~'RUBY')
obj
.foo(42) \
^ Redundant line continuation.
.bar
RUBY

expect_correction(<<~RUBY)
obj
.foo(42)#{' '}
.bar
RUBY
end

it 'registers an offense when redundant line continuations for multiline trailing dot method chain with an empty line' do
expect_offense(<<~'RUBY')
obj.
foo(42). \
^ Redundant line continuation.
bar
RUBY

expect_correction(<<~RUBY)
obj.
foo(42).#{' '}
bar
RUBY
end

it 'registers an offense when redundant line continuations for multiline trailing dot method chain without an empty line' do
expect_offense(<<~'RUBY')
obj.
foo(42). \
^ Redundant line continuation.
bar
RUBY

expect_correction(<<~RUBY)
obj.
foo(42).#{' '}
bar
RUBY
end

it 'registers an offense when redundant line continuations for array' do
expect_offense(<<~'RUBY')
[foo, \
Expand Down

0 comments on commit faf48f1

Please sign in to comment.