Skip to content

Commit 99422a0

Browse files
koicbbatsov
authored andcommitted
[Fix #14858] Fix an error in Layout/FirstArgumentIndentation
This PR fixes an infinite loop error in `Layout/FirstArgumentIndentation` when first arguments are over-indented in nested method calls. Fixes #14858.
1 parent a614ed8 commit 99422a0

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14858](https://github.com/rubocop/rubocop/issues/14858): Fix an infinite loop error in `Layout/FirstArgumentIndentation` when first arguments are over-indented in nested method calls. ([@koic][])

lib/rubocop/cop/layout/first_argument_indentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def autocorrect(corrector, node)
187187
def should_correct_entire_chain?(send_node, top_level_send)
188188
return false unless style == :special_for_inner_method_call_in_parentheses
189189
return false unless inner_call?(top_level_send)
190+
return false unless display_column(send_node.source_range) < column_delta.abs
190191

191192
top_level_send != send_node || begins_its_line?(top_level_send.loc.end)
192193
end

spec/rubocop/cop/layout/first_argument_indentation_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,50 @@
132132
RUBY
133133
end
134134

135+
it 'registers an offense and corrects over-indented first arguments in nested method calls' do
136+
expect_offense(<<~RUBY)
137+
foo
138+
.bar(
139+
baz(
140+
^^^^ Indent the first argument one step more than the start of the previous line.
141+
qux
142+
^^^ Bad indentation of the first argument.
143+
)
144+
)
145+
RUBY
146+
147+
expect_correction(<<~RUBY)
148+
foo
149+
.bar(
150+
baz(
151+
qux
152+
)
153+
)
154+
RUBY
155+
end
156+
157+
it 'registers an offense and corrects over-indented first arguments in nested method calls with hash arguments' do
158+
expect_offense(<<~RUBY)
159+
foo
160+
.bar(
161+
bar(
162+
^^^^ Indent the first argument one step more than the start of the previous line.
163+
key: value
164+
^^^^^^^^^^ Bad indentation of the first argument.
165+
)
166+
)
167+
RUBY
168+
169+
expect_correction(<<~RUBY)
170+
foo
171+
.bar(
172+
bar(
173+
key: value
174+
)
175+
)
176+
RUBY
177+
end
178+
135179
context 'when using safe navigation operator' do
136180
it 'registers an offense and corrects an under-indented 1st argument' do
137181
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)