Skip to content

Commit

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

This PR fixes an infinite loop error for `Layout/FirstArgumentIndentation`
when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArrayAlignment`.
  • Loading branch information
koic committed Apr 30, 2024
1 parent 3b3a0cd commit 26c26a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12877](https://github.com/rubocop/rubocop/issues/12877): Fix an infinite loop error for `Layout/FirstArgumentIndentation` when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArrayAlignment`. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/layout/first_array_element_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class FirstArrayElementIndentation < Base
'in an array, relative to %<base_description>s.'

def on_array(node)
return if style != :consistent && enforce_first_argument_with_fixed_indentation?

check(node, nil) if node.loc.begin
end

Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,36 @@ def do_even_more_stuff
RUBY
end

it 'corrects the indentation of array elements when specifying `EnforcedStyle: with_fixed_indentation` of ' \
'`Layout/ArrayAlignment` and `Layout/FirstArrayElementIndentation`' do
create_file('example.rb', <<~RUBY)
foo bar: [
'foo',
'bar'
],
baz: 'baz'
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/ArrayAlignment:
EnforcedStyle: with_fixed_indentation
YAML

expect(
cli.run(
['--autocorrect', '--only', 'Layout/ArrayAlignment,Layout/FirstArrayElementIndentation']
)
).to eq(0)
expect($stderr.string).to eq('')
expect(File.read('example.rb')).to eq(<<~RUBY)
foo bar: [
'foo',
'bar'
],
baz: 'baz'
RUBY
end

it 'does not crash Lint/SafeNavigationWithEmpty and offenses and accepts Style/SafeNavigation ' \
'when checking `foo&.empty?` in a conditional' do
create_file('example.rb', <<~RUBY)
Expand Down

0 comments on commit 26c26a1

Please sign in to comment.