Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8189] Consider spaces for Layout/MultilineBlockLayout #8203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@

### Bug fixes

* [#8189](https://github.com/rubocop-hq/rubocop/issues/8189): Fix an error for `Layout/MultilineBlockLayout` where spaces for a new line where not considered. ([@knejad][])
* [#8252](https://github.com/rubocop-hq/rubocop/issues/8252): Fix a command line option name from `--safe-autocorrect` to `--safe-auto-correct`, which is compatible with RuboCop 0.86 and lower. ([@koic][])
* [#8259](https://github.com/rubocop-hq/rubocop/issues/8259): Fix false positives for `Style/BisectedAttrAccessor` when accessors have different access modifiers. ([@fatkodima][])
* [#8253](https://github.com/rubocop-hq/rubocop/issues/8253): Fix false positives for `Style/AccessorGrouping` when accessors have different access modifiers. ([@fatkodima][])
Expand Down Expand Up @@ -4672,3 +4673,4 @@
[@karlwithak]: https://github.com/karlwithak
[@CamilleDrapier]: https://github.com/CamilleDrapier
[@shekhar-patil]: https://github.com/shekhar-patil
[@knejad]: https://github.com/knejad
2 changes: 1 addition & 1 deletion lib/rubocop/cop/autocorrect_logic.rb
Expand Up @@ -73,7 +73,7 @@ def range_by_lines(range)
end

def max_line_length
config.for_cop('Layout/LineLength')['Max'] || 80
config.for_cop('Layout/LineLength')['Max'] || 120
end

def disable_offense_at_end_of_line(range, eol_comment)
Expand Down
21 changes: 16 additions & 5 deletions lib/rubocop/cop/layout/multiline_block_layout.rb
Expand Up @@ -95,11 +95,22 @@ def args_on_beginning_line?(node)
end

def line_break_necessary_in_args?(node)
needed_length = node.source_range.column +
node.source.lines.first.length +
block_arg_string(node, node.arguments).length +
PIPE_SIZE
needed_length > max_line_length
needed_length_for_args(node) > max_line_length
end

def needed_length_for_args(node)
node.source_range.column +
characters_needed_for_space_and_pipes(node) +
node.source.lines.first.chomp.length +
block_arg_string(node, node.arguments).length
end

def characters_needed_for_space_and_pipes(node)
if node.source.lines.first.end_with?("|\n")
PIPE_SIZE
else
1 + PIPE_SIZE * 2
end
end

def add_offense_for_expression(node, expr, msg)
Expand Down
40 changes: 37 additions & 3 deletions spec/rubocop/cop/layout/multiline_block_layout_spec.rb
Expand Up @@ -78,15 +78,49 @@
RUBY
end

it 'does not register offenses when there are too many parameters to fit ' \
'on one line' do
it 'does not register offenses when there are too many parameters to fit on one line' do
expect_no_offenses(<<~RUBY)
some_result = lambda do |
so_many,
parameters,
it_will,
be_too_long,
for_one_line|
for_one_line,
line_length,
has_increased,
add_3_more|
do_something
end
RUBY
end

it 'registers offenses when there are not too many parameters to fit on one line' do
expect_offense(<<~RUBY)
some_result = lambda do |
^ Block argument expression is not on the same line as the block start.
so_many,
parameters,
it_will,
be_too_long,
for_one_line,
line_length,
has_increased,
add_3_mor|
do_something
end
RUBY

expect_correction(<<~RUBY)
some_result = lambda do |so_many, parameters, it_will, be_too_long, for_one_line, line_length, has_increased, add_3_mor|
do_something
end
RUBY
end

it 'considers the extra space required to join the lines together' do
expect_no_offenses(<<~RUBY)
some_result = lambda do
|so_many, parameters, it_will, be_too_long, for_one_line, line_length, has_increased, add_3_more|
do_something
end
RUBY
Expand Down