From fa0c3f023529ba378ebe373d537c7878d461775f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 26 Nov 2020 11:52:29 +0900 Subject: [PATCH] [Fix #9097] Fix a false positive for `Layout/EmptyLinesAroundArguments` Fixes #9097. This PR fixes a false positive for `Layout/EmptyLinesAroundArguments` when blank line is inserted between method with arguments and receiver. --- ...fix_false_positive_for_empty_lines_around_arguments.md | 1 + lib/rubocop/cop/layout/empty_lines_around_arguments.rb | 7 ++++++- .../cop/layout/empty_lines_around_arguments_spec.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_for_empty_lines_around_arguments.md diff --git a/changelog/fix_false_positive_for_empty_lines_around_arguments.md b/changelog/fix_false_positive_for_empty_lines_around_arguments.md new file mode 100644 index 000000000000..5784041fb35e --- /dev/null +++ b/changelog/fix_false_positive_for_empty_lines_around_arguments.md @@ -0,0 +1 @@ +* [#9097](https://github.com/rubocop-hq/rubocop/issues/9097): Fix a false positive for `Layout/EmptyLinesAroundArguments` when blank line is inserted between method with arguments and receiver. ([@koic][]) diff --git a/lib/rubocop/cop/layout/empty_lines_around_arguments.rb b/lib/rubocop/cop/layout/empty_lines_around_arguments.rb index e174859b41c5..d2ace763ea9c 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_arguments.rb @@ -45,7 +45,8 @@ class EmptyLinesAroundArguments < Base MSG = 'Empty line detected around arguments.' def on_send(node) - return if node.single_line? || node.arguments.empty? + return if node.single_line? || node.arguments.empty? || + receiver_and_method_call_on_different_lines?(node) extra_lines(node) do |range| add_offense(range) do |corrector| @@ -57,6 +58,10 @@ def on_send(node) private + def receiver_and_method_call_on_different_lines?(node) + node.receiver && node.receiver.loc.last_line != node.loc.selector.line + end + def empty_lines(node) lines = processed_lines(node) lines.select! { |code, _| code.empty? } diff --git a/spec/rubocop/cop/layout/empty_lines_around_arguments_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_arguments_spec.rb index 9d901593fa85..892b99d1db30 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_arguments_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_arguments_spec.rb @@ -284,6 +284,14 @@ def anything; end RUBY end + it 'accepts when blank line is inserted between method with arguments and receiver' do + expect_no_offenses(<<~RUBY) + foo. + + bar(arg) + RUBY + end + context 'with one argument' do it 'ignores empty lines inside of method arguments' do expect_no_offenses(<<~RUBY)