Skip to content

Commit

Permalink
Merge pull request #12050 from koic/fix_an_error_for_layout_redundant…
Browse files Browse the repository at this point in the history
…_line_break

Fix a false positive for `Layout/RedundantLineBreak`
  • Loading branch information
koic committed Jul 15, 2023
2 parents ac4a94b + 6e62586 commit 63b3515
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_layout_redundant_line_break.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12050](https://github.com/rubocop/rubocop/pull/12050): Fix a false positive for `Layout/RedundantLineBreak` when inspecting the `%` form string `%\n\n`. ([@koic][])
8 changes: 8 additions & 0 deletions lib/rubocop/cop/layout/redundant_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class RedundantLineBreak < Base

MSG = 'Redundant line break detected.'

def on_lvasgn(node)
super unless end_with_percent_blank_string?(processed_source)
end

def on_send(node)
# Include "the whole expression".
node = node.parent while node.parent&.send_type? ||
Expand All @@ -61,6 +65,10 @@ def on_send(node)

private

def end_with_percent_blank_string?(processed_source)
processed_source.buffer.source.end_with?("%\n\n")
end

def check_assignment(node, _rhs)
return unless offense?(node)

Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/layout/redundant_line_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ def resolve_inheritance_from_gems(hash)
.baz
RUBY
end

it 'does not register an offense when the `%` form string `"%\n\n"` at the end of file' do
expect_no_offenses("%\n\n")
end

it 'does not register an offense when assigning the `%` form string `"%\n\n"` to a variable at the end of file' do
expect_no_offenses("x = %\n\n")
end
end
end

Expand Down

0 comments on commit 63b3515

Please sign in to comment.