Skip to content

Commit

Permalink
[Fix rubocop#10165] Fix Layout/DotPosition false positives when the…
Browse files Browse the repository at this point in the history
… selector and receiver are on the same line.
  • Loading branch information
dvandersluis committed Oct 5, 2021
1 parent f9d5e32 commit 8f634b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_layoutdotposition_false_positives.md
@@ -0,0 +1 @@
* [#10165](https://github.com/rubocop/rubocop/issues/10165): Fix `Layout/DotPosition` false positives when the selector and receiver are on the same line. ([@dvandersluis][])
12 changes: 5 additions & 7 deletions lib/rubocop/cop/layout/dot_position.rb
Expand Up @@ -68,18 +68,14 @@ def message(dot)
end

def proper_dot_position?(node)
selector_line = selector_range(node).line
selector_range = selector_range(node)

# If the receiver is a HEREDOC and the selector is on the same line
# then there is nothing to do
return true if heredoc?(node.receiver) && node.receiver.loc.first_line == selector_line
return true if same_line?(selector_range, selector_range(node.receiver))

selector_line = selector_range.line
receiver_line = receiver_end_line(node.receiver)
dot_line = node.loc.dot.line

# receiver and selector are on the same line
return true if selector_line == receiver_line

# don't register an offense if there is a line comment between the
# dot and the selector otherwise, we might break the code while
# "correcting" it (even if there is just an extra blank line, treat
Expand Down Expand Up @@ -124,6 +120,8 @@ def heredoc?(node)
end

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

# l.(1) has no selector, so we use the opening parenthesis instead
node.loc.selector || node.loc.begin
end
Expand Down
22 changes: 21 additions & 1 deletion spec/rubocop/cop/layout/dot_position_spec.rb
Expand Up @@ -257,6 +257,16 @@
RUBY
end
end

context 'with another method on the same line' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
foo(<<~HEREDOC).squish
something
HEREDOC
RUBY
end
end
end

context 'when the receiver is a heredoc' do
Expand Down Expand Up @@ -445,6 +455,16 @@
RUBY
end
end

context 'with another method on the same line' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
foo(<<~HEREDOC).squish
something
HEREDOC
RUBY
end
end
end

context 'when the receiver is a heredoc' do
Expand All @@ -467,7 +487,7 @@
end

context 'when there is a heredoc with a following method' do
it 'does not register an offense' do
it 'does not register an offense for a heredoc' do
expect_no_offenses(<<~RUBY)
<<~HEREDOC.squish
something
Expand Down

0 comments on commit 8f634b8

Please sign in to comment.