Skip to content

Commit

Permalink
Merge pull request #9801 from dvandersluis/issue/9749
Browse files Browse the repository at this point in the history
[Fix #9749] Fix autocorrection for `Layout/LineLength` to not move the first argument of an unparenthesized `send` node to the next line
  • Loading branch information
koic committed May 16, 2021
2 parents fcde5d7 + 60834d4 commit 4f13546
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
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

0 comments on commit 4f13546

Please sign in to comment.