Skip to content

Commit

Permalink
Fix false positive in Layout/DotPosition
Browse files Browse the repository at this point in the history
This fixes a false positive in Layout/DotPosition where an offense would
be registered for the trailing style when the receiver was a method call
with multiline arguments, and the selector was on the same line as the
receiver's closing bracket.

For example, before this change, the following offense would be registered:

    foo(
      bar
    ).baz
     ^ Place the . on the previous line, together with the method call receiver.
  • Loading branch information
mvz committed Oct 22, 2021
1 parent f598124 commit 7d5d5ff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_false_positive_in_layoutdotposition.md
@@ -0,0 +1 @@
* [#x](https://github.com/rubocop/rubocop/pull/x): Fix false positive in Layout/DotPosition when the selector is on the same line as the closing bracket of the receiver. ([@mvz][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/layout/dot_position.rb
Expand Up @@ -70,7 +70,7 @@ def message(dot)
def proper_dot_position?(node)
selector_range = selector_range(node)

return true if same_line?(selector_range, selector_range(node.receiver))
return true if same_line?(selector_range, end_range(node.receiver))

selector_line = selector_range.line
receiver_line = receiver_end_line(node.receiver)
Expand Down Expand Up @@ -119,6 +119,10 @@ def heredoc?(node)
(node.str_type? || node.dstr_type?) && node.heredoc?
end

def end_range(node)
node.source_range.end
end

def selector_range(node)
return node unless node.call_type?

Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/layout/dot_position_spec.rb
Expand Up @@ -316,6 +316,14 @@
expect_no_offenses('puts something')
end

it 'does not err on method call with multi-line arguments' do
expect_no_offenses(<<~RUBY)
foo(
bar
).baz
RUBY
end

it 'does not err on method call without a method name' do
expect_offense(<<~RUBY)
l
Expand Down

0 comments on commit 7d5d5ff

Please sign in to comment.