Skip to content

Commit

Permalink
Fix an infinite loop error for Layout/ArrayAlignment
Browse files Browse the repository at this point in the history
Fixes standardrb/standard#517.

This PR fixes an infinite loop error for `Layout/ArrayAlignment`
when using assigning unbracketed array elements.
  • Loading branch information
koic committed Feb 4, 2023
1 parent 3c9c494 commit 617b884
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11537](https://github.com/rubocop/rubocop/pull/11537): Fix an infinite loop error for `Layout/ArrayAlignment` when using assigning unbracketed array elements. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/array_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def base_column(node, args)
end

def target_method_lineno(node)
node.loc.line
node.bracketed? ? node.loc.line : node.parent.loc.line
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/layout/array_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,39 @@
RUBY
end

it 'accepts when assigning aligned bracketed array elements' do
expect_no_offenses(<<~RUBY)
var = [
first,
second
]
RUBY
end

it 'accepts when assigning aligned unbracketed array elements' do
expect_no_offenses(<<~RUBY)
var =
first,
second
RUBY
end

it 'registers an offense when assigning not aligned unbracketed array elements' do
expect_offense(<<~RUBY)
var =
first,
^^^^^ Use one level of indentation for elements following the first line of a multi-line array.
second
^^^^^^ Use one level of indentation for elements following the first line of a multi-line array.
RUBY

expect_correction(<<~RUBY)
var =
first,
second
RUBY
end

it 'accepts single line array' do
expect_no_offenses('array = [ a, b ]')
end
Expand Down

0 comments on commit 617b884

Please sign in to comment.