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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #9749] Fix autocorrection for Layout/LineLength to not move the first argument of an unparenthesized send node to the next line #9801

Merged
merged 1 commit into from May 16, 2021
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
1 change: 1 addition & 0 deletions changelog/fix_fix_autocorrection_for_layoutlinelength.md
@@ -0,0 +1 @@
* [#9749](https://github.com/rubocop/rubocop/issues/9749): Fix autocorrection for `Layout/LineLength` to not move the first argument of an unparenthesized `send` node to the next line, which changes behaviour. ([@dvandersluis][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/mixin/check_line_breakable.rb
Expand Up @@ -70,9 +70,9 @@ def extract_breakable_node_from_elements(node, elements, max)
def extract_first_element_over_column_limit(node, elements, max)
line = node.first_line

# If the first argument is a hash pair but the method is not parenthesized,
# the argument cannot be moved to another line because it cause a syntax error.
elements.shift if node.send_type? && !node.parenthesized? && elements.first.pair_type?
# If a `send` node is not parenthesized, don't move the first element, because it
# can result in changed behavior or a syntax error.
elements = elements.drop(1) if node.send_type? && !node.parenthesized?

i = 0
i += 1 while within_column_limit?(elements[i], max, line)
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -605,6 +605,34 @@ def baz(bar)
end
end

context 'when unparenthesized' do
context 'when there is one argument' do
it 'does not autocorrect' do
expect_offense(<<~RUBY)
method_call xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
^^ Line is too long. [42/40]
RUBY

expect_no_corrections
end
end

context 'when there are multiple arguments' do
it 'splits the line after the first element' do
args = 'x' * 28
expect_offense(<<~RUBY, args: args)
method_call #{args}, abc
_{args}^^^^^ Line is too long. [45/40]
RUBY

expect_correction(<<~RUBY, loop: false)
method_call #{args},#{trailing_whitespace}
abc
RUBY
end
end
end

context 'when call with hash on same line' do
it 'adds an offense only to outer and autocorrects it' do
expect_offense(<<~RUBY)
Expand Down