Skip to content

Commit

Permalink
Merge pull request #9382 from dvandersluis/issue/9374
Browse files Browse the repository at this point in the history
[Fix #9374] Fix autocorrection for `Layout/LineLength` when the first argument to a send node is a overly long hash pair
  • Loading branch information
koic authored Jan 20, 2021
2 parents 70c62cc + f560f88 commit 270eff8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_dont_autocorrect_layoutlinelength_for_a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9374](https://github.com/rubocop-hq/rubocop/issues/9374): Fix autocorrection for `Layout/LineLength` when the first argument to a send node is a overly long hash pair. ([@dvandersluis][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/mixin/check_line_breakable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def extract_breakable_node_from_elements(node, elements, max)
# @api private
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?

i = 0
i += 1 while within_column_limit?(elements[i], max, line)
return elements.first if i.zero?
Expand Down
69 changes: 69 additions & 0 deletions spec/rubocop/cop/layout/line_length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,75 @@ def baz(bar)
end
end

context 'with a hash with a too long first item' do
context 'when parenthesized' do
it 'corrects' do
expect_offense(<<~RUBY)
foo(abc: '10000000000000000000000000000000000000000000000000000', def: '1000')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [78/40]
RUBY

expect_correction(<<~RUBY)
foo(
abc: '10000000000000000000000000000000000000000000000000000', def: '1000')
RUBY
end
end

context 'when the hash is parenthesized' do
it 'corrects' do
expect_offense(<<~RUBY)
foo({ abc: '10000000000000000000000000000000000000000000000000000', def: '1000' })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [82/40]
RUBY

expect_correction(<<~RUBY)
foo({#{trailing_whitespace}
abc: '10000000000000000000000000000000000000000000000000000', def: '1000' })
RUBY
end
end

context 'when not parenthesized' do
context 'when there is only one element' do
it 'does not autocorrect' do
expect_offense(<<~RUBY)
foo abc: '10000000000000000000000000000000000000000000000000000'
^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [64/40]
RUBY

expect_no_corrections
end
end

context 'when there are multiple elements' do
it 'moves the 2nd element to a new line' do
expect_offense(<<~RUBY)
foo abc: '10000000000000000000000000000000000000000000000000000', ghi: '1000'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [77/40]
RUBY

expect_correction(<<~RUBY, loop: false)
foo abc: '10000000000000000000000000000000000000000000000000000',#{trailing_whitespace}
ghi: '1000'
RUBY
end
end

context 'when on multiple lines' do
it 'does not correct' do
expect_offense(<<~RUBY)
foo abc: '10000000000000000000000000000000000000000000000000000',
^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [65/40]
ghi: '1000'
RUBY

expect_no_corrections
end
end
end
end

context 'when two method calls' do
it 'adds an offense only to outer and autocorrects it' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 270eff8

Please sign in to comment.