Skip to content

Commit

Permalink
[Fix rubocop#10140] Fix false positive for Layout/DotPosition when …
Browse files Browse the repository at this point in the history
…a heredoc receives a method on the same line as the start sigil in `trailing` style.
  • Loading branch information
dvandersluis committed Sep 29, 2021
1 parent 502c606 commit b7a2f1d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_false_positive_for_layoutdotposition.md
@@ -0,0 +1 @@
* [#10140](https://github.com/rubocop/rubocop/issues/10140): Fix false positive for `Layout/DotPosition` when a heredoc receives a method on the same line as the start sigil in `trailing` style. ([@dvandersluis][])
10 changes: 7 additions & 3 deletions lib/rubocop/cop/layout/dot_position.rb
Expand Up @@ -68,14 +68,18 @@ def message(dot)
end

def proper_dot_position?(node)
receiver_line = receiver_end_line(node.receiver)
selector_line = selector_range(node).line

# receiver and selector are on the same line
return true if selector_line == receiver_line
# 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

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
10 changes: 10 additions & 0 deletions spec/rubocop/cop/layout/dot_position_spec.rb
Expand Up @@ -465,5 +465,15 @@
RUBY
end
end

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

0 comments on commit b7a2f1d

Please sign in to comment.