diff --git a/changelog/fix_fix_layoutredundantlinebreak_adding.md b/changelog/fix_fix_layoutredundantlinebreak_adding.md new file mode 100644 index 00000000000..b3d662eb015 --- /dev/null +++ b/changelog/fix_fix_layoutredundantlinebreak_adding.md @@ -0,0 +1 @@ +* [#10124](https://github.com/rubocop/rubocop/pull/10124): Fix `Layout/RedundantLineBreak` adding extra space within method chains. ([@dvandersluis][]) diff --git a/lib/rubocop/cop/layout/redundant_line_break.rb b/lib/rubocop/cop/layout/redundant_line_break.rb index 5e1a996c818..863a67041f4 100644 --- a/lib/rubocop/cop/layout/redundant_line_break.rb +++ b/lib/rubocop/cop/layout/redundant_line_break.rb @@ -128,6 +128,7 @@ def to_single_line(source) .gsub(/' *\\\n\s*"/, %q(' + ")) # Single quote, backslash, and then double quote .gsub(/(["']) *\\\n\s*\1/, '') # Double or single quote, backslash, then same quote .gsub(/\s*\\?\n\s*/, ' ') # Any other line break, with or without backslash + .gsub(/\s+(?=\.\w)/, '') # Extra space within method chaining end def max_line_length diff --git a/spec/rubocop/cop/layout/redundant_line_break_spec.rb b/spec/rubocop/cop/layout/redundant_line_break_spec.rb index 9517322d0a0..282500690c4 100644 --- a/spec/rubocop/cop/layout/redundant_line_break_spec.rb +++ b/spec/rubocop/cop/layout/redundant_line_break_spec.rb @@ -271,6 +271,19 @@ def resolve_inheritance_from_gems(hash) m(7 + 8 + 9) RUBY end + + it 'properly corrects a method chain on multiple lines' do + expect_offense(<<~RUBY) + foo + ^^^ Redundant line break detected. + .bar + .baz + RUBY + + expect_correction(<<~RUBY) + foo.bar.baz + RUBY + end end context 'for an expression that does not fit on a single line' do