From 53bac7e1228df19111ae8ef977e45916a714a246 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 22 Oct 2021 14:45:33 +0200 Subject: [PATCH] Fix false positive in Layout/DotPosition 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. --- changelog/fix_fix_false_positive_in_layoutdotposition.md | 1 + lib/rubocop/cop/layout/dot_position.rb | 6 +++++- spec/rubocop/cop/layout/dot_position_spec.rb | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_fix_false_positive_in_layoutdotposition.md diff --git a/changelog/fix_fix_false_positive_in_layoutdotposition.md b/changelog/fix_fix_false_positive_in_layoutdotposition.md new file mode 100644 index 00000000000..c03cd7a2c0a --- /dev/null +++ b/changelog/fix_fix_false_positive_in_layoutdotposition.md @@ -0,0 +1 @@ +* [#10207](https://github.com/rubocop/rubocop/pull/10207): Fix false positive in Layout/DotPosition when the selector is on the same line as the closing bracket of the receiver. ([@mvz][]) diff --git a/lib/rubocop/cop/layout/dot_position.rb b/lib/rubocop/cop/layout/dot_position.rb index 98da99adc1e..07651ff2e8e 100644 --- a/lib/rubocop/cop/layout/dot_position.rb +++ b/lib/rubocop/cop/layout/dot_position.rb @@ -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) @@ -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? diff --git a/spec/rubocop/cop/layout/dot_position_spec.rb b/spec/rubocop/cop/layout/dot_position_spec.rb index 257c6375364..d393e6fd44e 100644 --- a/spec/rubocop/cop/layout/dot_position_spec.rb +++ b/spec/rubocop/cop/layout/dot_position_spec.rb @@ -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