Skip to content

Commit

Permalink
[Fix #10488] Fix autocorrection for `Layout/MultilineMethodCallIndent…
Browse files Browse the repository at this point in the history
…ation` breaks indentation for nesting of method calls
  • Loading branch information
nobuyo committed Apr 19, 2022
1 parent 6193277 commit c0a9b9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10488](https://github.com/rubocop/rubocop/issues/10488): Fix autocorrection for `Layout/MultilineMethodCallIndentation` breaks indentation for nesting of method calls. ([@nobuyo][])
21 changes: 19 additions & 2 deletions lib/rubocop/cop/layout/multiline_method_call_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,31 @@ def receiver_alignment_base(node)
def semantic_alignment_node(node)
return if argument_in_method_call(node, :with_parentheses)

dot_right_above = get_dot_right_above(node)
return dot_right_above if dot_right_above

node = first_call_has_a_dot(node)
return if node.loc.dot.line != node.first_line

node
end

def get_dot_right_above(node)
node.each_ancestor.find do |a|
dot = a.loc.respond_to?(:dot) && a.loc.dot
next unless dot

dot.line == node.loc.dot.line - 1 && dot.column == node.loc.dot.column
end
end

def first_call_has_a_dot(node)
# descend to root of method chain
node = node.receiver while node.receiver
# ascend to first call which has a dot
node = node.parent
node = node.parent until node.loc.respond_to?(:dot) && node.loc.dot

return if node.loc.dot.line != node.first_line

node
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ def foo
end
RUBY
end

it 'accepts nested method calls' do
expect_no_offenses(<<~RUBY)
expect { post :action, params: params, format: :json }.to change { Foo.bar }.by(0)
.and change { Baz.quux }.by(0)
.and raise_error(StandardError)
RUBY
end
end

it 'accepts correctly aligned methods in operands' do
Expand Down

0 comments on commit c0a9b9e

Please sign in to comment.