Skip to content

Commit

Permalink
Merge pull request #10124 from dvandersluis/fix/redundant-line-break
Browse files Browse the repository at this point in the history
Fix `Layout/RedundantLineBreak` adding extra space within method chains
  • Loading branch information
koic committed Sep 28, 2021
2 parents 4177c67 + 472458d commit 9d534e2
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions 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][])
4 changes: 1 addition & 3 deletions lib/rubocop/cop/layout/dot_position.rb
Expand Up @@ -109,9 +109,7 @@ def receiver_end_line(node)

def last_heredoc_line(node)
if node.send_type?
node.arguments.select { |arg| heredoc?(arg) }
.map { |arg| arg.loc.heredoc_end.line }
.max
node.arguments.select { |arg| heredoc?(arg) }.map { |arg| arg.loc.heredoc_end.line }.max
elsif heredoc?(node)
node.loc.heredoc_end.line
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/layout/redundant_line_break.rb
Expand Up @@ -127,6 +127,7 @@ def to_single_line(source)
.gsub(/" *\\\n\s*'/, %q(" + ')) # Double quote, backslash, and then single quote
.gsub(/' *\\\n\s*"/, %q(' + ")) # Single quote, backslash, and then double quote
.gsub(/(["']) *\\\n\s*\1/, '') # Double or single quote, backslash, then same quote
.gsub(/\n\s*(?=\.\w)/, '') # Extra space within method chaining
.gsub(/\s*\\?\n\s*/, ' ') # Any other line break, with or without backslash
end

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/mixin/heredoc.rb
Expand Up @@ -21,9 +21,7 @@ def on_heredoc(_node)
private

def indent_level(str)
indentations = str.lines
.map { |line| line[/^\s*/] }
.reject { |line| line.end_with?("\n") }
indentations = str.lines.map { |line| line[/^\s*/] }.reject { |line| line.end_with?("\n") }
indentations.empty? ? 0 : indentations.min_by(&:size).size
end

Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -1010,8 +1010,7 @@ class Loop < Cop
gem_class = Struct.new(:gem_dir)
%w[gemone gemtwo].each do |gem_name|
mock_spec = gem_class.new(File.join(gem_root, gem_name))
allow(Gem::Specification).to receive(:find_by_name)
.with(gem_name).and_return(mock_spec)
allow(Gem::Specification).to receive(:find_by_name).with(gem_name).and_return(mock_spec)
end
allow(Gem).to receive(:path).and_return([gem_root])
end
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/config_spec.rb
Expand Up @@ -192,8 +192,7 @@
end

it 'raises validation error' do
expect { configuration.validate }
.to raise_error(RuboCop::ValidationError, /trailing_comma/)
expect { configuration.validate }.to raise_error(RuboCop::ValidationError, /trailing_comma/)
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/layout/case_indentation_spec.rb
Expand Up @@ -2,8 +2,7 @@

RSpec.describe RuboCop::Cop::Layout::CaseIndentation, :config do
let(:config) do
merged = RuboCop::ConfigLoader
.default_configuration['Layout/CaseIndentation'].merge(cop_config)
merged = RuboCop::ConfigLoader.default_configuration['Layout/CaseIndentation'].merge(cop_config)
RuboCop::Config.new('Layout/CaseIndentation' => merged,
'Layout/IndentationWidth' => { 'Width' => 2 })
end
Expand Down
54 changes: 54 additions & 0 deletions spec/rubocop/cop/layout/redundant_line_break_spec.rb
Expand Up @@ -271,6 +271,60 @@ def resolve_inheritance_from_gems(hash)
m(7 + 8 + 9)
RUBY
end

context 'method chains' do
it 'properly corrects a method chain on multiple lines' do
expect_offense(<<~RUBY)
foo(' .x')
^^^^^^^^^^ Redundant line break detected.
.bar
.baz
RUBY

expect_correction(<<~RUBY)
foo(' .x').bar.baz
RUBY
end

it 'registers an offense and corrects with a arguments on multiple lines' do
expect_offense(<<~RUBY)
foo(x,
^^^^^^ Redundant line break detected.
y,
z)
.bar
.baz
RUBY

expect_correction(<<~RUBY)
foo(x, y, z).bar.baz
RUBY
end

it 'registers an offense and corrects with a string argument on multiple lines' do
expect_offense(<<~RUBY)
foo('....' \\
^^^^^^^^^^^^ Redundant line break detected.
'....')
.bar
.baz
RUBY

expect_correction(<<~RUBY)
foo('........').bar.baz
RUBY
end

it 'does not register an offense with a heredoc argument' do
expect_no_offenses(<<~RUBY)
foo(<<~EOS)
xyz
EOS
.bar
.baz
RUBY
end
end
end

context 'for an expression that does not fit on a single line' do
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/options_spec.rb
Expand Up @@ -365,8 +365,7 @@ def abs(path)
end

it 'fails if given without --auto-gen-config' do
expect { options.parse %w[--exclude-limit 10] }
.to raise_error(RuboCop::OptionArgumentError)
expect { options.parse %w[--exclude-limit 10] }.to raise_error(RuboCop::OptionArgumentError)
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/result_cache_spec.rb
Expand Up @@ -226,8 +226,7 @@ def abs(path)
cache2.save(offenses)

expect(cache2.valid?).to eq(true)
expect($stderr.string)
.not_to match(/Warning: .* is a symlink, which is not allowed.\n/)
expect($stderr.string).not_to match(/Warning: .* is a symlink, which is not allowed.\n/)
end
end
end
Expand Down

0 comments on commit 9d534e2

Please sign in to comment.