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 #9641] Fix Layout/MultilineMethodCallIndentation triggering on method calls that look like operators #9642

Merged
merged 1 commit into from Mar 27, 2021
Merged
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
1 change: 1 addition & 0 deletions changelog/fix_fix_layoutmultilinemethodcallindentation.md
@@ -0,0 +1 @@
* [#9641](https://github.com/rubocop/rubocop/issues/9641): Fix `Layout/MultilineMethodCallIndentation` triggering on method calls that look like operators. ([@dvandersluis][])
12 changes: 12 additions & 0 deletions lib/rubocop/cop/layout/multiline_method_call_indentation.rb
Expand Up @@ -72,6 +72,18 @@ def relevant_node?(send_node)
send_node.loc.dot # Only check method calls with dot operator
end

def right_hand_side(send_node)
dot = send_node.loc.dot
selector = send_node.loc.selector
if send_node.dot? && selector && dot.line == selector.line
dot.join(selector)
elsif selector
selector
elsif send_node.implicit_call?
dot.join(send_node.loc.begin)
end
end

def offending_range(node, lhs, rhs, given_style)
return false unless begins_its_line?(rhs)
return false if not_for_this_cop?(node)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/layout/multiline_operation_indentation.rb
Expand Up @@ -118,6 +118,10 @@ def message(node, lhs, rhs)
"spaces for indenting #{what} spanning multiple lines."
end
end

def right_hand_side(send_node)
send_node.first_argument.source_range
end
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Expand Up @@ -35,26 +35,6 @@ def left_hand_side(lhs)
lhs
end

def right_hand_side(send_node)
if send_node.operator_method? && send_node.arguments?
send_node.first_argument.source_range # not used for method calls
else
regular_method_right_hand_side(send_node)
end
end

def regular_method_right_hand_side(send_node)
dot = send_node.loc.dot
selector = send_node.loc.selector
if send_node.dot? && selector && dot.line == selector.line
dot.join(selector)
elsif selector
selector
elsif send_node.implicit_call?
dot.join(send_node.loc.begin)
end
end

# The correct indentation of `node` is usually `IndentationWidth`, with
# one exception: prefix keywords.
#
Expand Down
Expand Up @@ -95,6 +95,15 @@
.my_method
RUBY
end

it 'accepts alignment of method with assignment and operator-like method' do
expect_no_offenses(<<~RUBY)
query = x.|(
foo,
bar
)
RUBY
end
end

shared_examples 'common for aligned and indented' do
Expand Down
Expand Up @@ -81,6 +81,11 @@
.a
.b(c)

Foo.&(
foo,
bar
)

expect { Foo.new }.
to change { Bar.count }.
from(1).to(2)
Expand Down