From b7a2f1d259a363bb3a7d89b6f99303edd41ac0dc Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Wed, 29 Sep 2021 09:56:06 -0400 Subject: [PATCH] [Fix #10140] Fix false positive for `Layout/DotPosition` when a heredoc receives a method on the same line as the start sigil in `trailing` style. --- .../fix_fix_false_positive_for_layoutdotposition.md | 1 + lib/rubocop/cop/layout/dot_position.rb | 10 +++++++--- spec/rubocop/cop/layout/dot_position_spec.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_fix_false_positive_for_layoutdotposition.md diff --git a/changelog/fix_fix_false_positive_for_layoutdotposition.md b/changelog/fix_fix_false_positive_for_layoutdotposition.md new file mode 100644 index 00000000000..7e3d11356b8 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/layout/dot_position.rb b/lib/rubocop/cop/layout/dot_position.rb index 27857e27119..b4e2554607e 100644 --- a/lib/rubocop/cop/layout/dot_position.rb +++ b/lib/rubocop/cop/layout/dot_position.rb @@ -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 diff --git a/spec/rubocop/cop/layout/dot_position_spec.rb b/spec/rubocop/cop/layout/dot_position_spec.rb index e25974411dc..c13a188e35f 100644 --- a/spec/rubocop/cop/layout/dot_position_spec.rb +++ b/spec/rubocop/cop/layout/dot_position_spec.rb @@ -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