Skip to content

Commit

Permalink
[Fix rubocop#12548] Fix an infinite loop error for `Layout/FirstArgum…
Browse files Browse the repository at this point in the history
…entIndentation`

Fixes rubocop#12548.

This PR fixes an infinite loop error for `Layout/FirstArgumentIndentation`
when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArrayAlignment`.
  • Loading branch information
koic committed Dec 23, 2023
1 parent 14e26fd commit 10e0f2a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12548](https://github.com/rubocop/rubocop/issues/12548): Fix an infinite loop error for `Layout/FirstArgumentIndentation` when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArrayAlignment`. ([@koic][])
17 changes: 16 additions & 1 deletion lib/rubocop/cop/layout/first_array_element_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Cop
module Layout
# Checks the indentation of the first element in an array literal
# where the opening bracket and the first element are on separate lines.
# The other elements' indentations are handled by the ArrayAlignment cop.
# The other elements' indentations are handled by `Layout/ArrayAlignment` cop.
#
# This cop will respect `Layout/ArrayAlignment` and will not work when
# `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArrayAlignment`.
#
# By default, array literals that are arguments in a method call with
# parentheses, and where the opening square bracket of the array is on the
Expand Down Expand Up @@ -93,6 +96,8 @@ def on_array(node)
end

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

each_argument_node(node, :array) do |array_node, left_parenthesis|
check(array_node, left_parenthesis)
end
Expand Down Expand Up @@ -174,6 +179,16 @@ def message_for_right_bracket(indent_base_type)
'where the left bracket is.'
end
end

def enforce_first_argument_with_fixed_indentation?
return false unless array_alignment_config['Enabled']

array_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
end

def array_alignment_config
config.for_cop('Layout/ArrayAlignment')
end
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,34 @@ def do_even_more_stuff
RUBY
end

it 'corrects when specifying `EnforcedStyle: with_fixed_indentation` of `Layout/ArrayAlignment` and ' \
'`Layout/FirstArrayElementIndentation`' do
create_file('example.rb', <<~RUBY)
puts([
'foo',
'bar'
])
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)
puts([
'foo',
'bar'
])
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 10e0f2a

Please sign in to comment.