Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10488] Add KeepAlignedDots option for Layout/MultilineMethodCallIndentation #10549

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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