Skip to content

Commit

Permalink
Merge pull request #9642 from dvandersluis/issue/9641
Browse files Browse the repository at this point in the history
[Fix #9641] Fix `Layout/MultilineMethodCallIndentation` triggering on method calls that look like operators
  • Loading branch information
koic committed Mar 27, 2021
2 parents e888a42 + dd7dba8 commit 3dba8f3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
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

0 comments on commit 3dba8f3

Please sign in to comment.