From eee7416d3f71d0e4cdf929507cce59df71d3acea Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 18 Oct 2019 18:07:57 +0900 Subject: [PATCH] [Fix #7442] Fix an incorrect autocorrect for `Style/SafeNavigation` Fixes #7442. This PR fixes an incorrect autocorrect for `Style/SafeNavigation` when an object check followed by a method call with a comment at EOL. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/safe_navigation.rb | 13 ++++++------- spec/rubocop/cop/style/safe_navigation_spec.rb | 9 +++++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99e53e2389d..6a606581db9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#7439](https://github.com/rubocop-hq/rubocop/issues/7439): Make `Style/FormatStringToken` ignore percent escapes (`%%`). ([@buehmann][]) * [#7438](https://github.com/rubocop-hq/rubocop/issues/7438): Fix assignment edge-cases in `Layout/MultilineAssignmentLayout`. ([@gsamokovarov][]) * [#7449](https://github.com/rubocop-hq/rubocop/pull/7449): Make `Style/IfUnlessModifier` respect `rubocop:disable` comments for `Metrics/LineLength`. ([@jonas054][]) +* [#7442](https://github.com/rubocop-hq/rubocop/issues/7442): Fix an incorrect autocorrect for `Style/SafeNavigation` when an object check followed by a method call with a comment at EOL. ([@koic][]) ## 0.75.1 (2019-10-14) diff --git a/lib/rubocop/cop/style/safe_navigation.rb b/lib/rubocop/cop/style/safe_navigation.rb index 7f76bfa3a52..ed406c763b9 100644 --- a/lib/rubocop/cop/style/safe_navigation.rb +++ b/lib/rubocop/cop/style/safe_navigation.rb @@ -129,19 +129,18 @@ def autocorrect(node) private def handle_comments(corrector, node, method_call) - return if processed_source.comments.empty? + comments = comments(node) + return if comments.empty? corrector.insert_before(method_call.loc.expression, - "#{comments(node).join("\n")}\n") + "#{comments.map(&:text).join("\n")}\n") end def comments(node) - comments = processed_source.comments.select do |comment| - comment.loc.first_line >= node.loc.first_line && - comment.loc.last_line <= node.loc.last_line + processed_source.comments.select do |comment| + comment.loc.first_line > node.loc.first_line && + comment.loc.last_line < node.loc.last_line end - - comments.map(&:text) end def allowed_if_condition?(node) diff --git a/spec/rubocop/cop/style/safe_navigation_spec.rb b/spec/rubocop/cop/style/safe_navigation_spec.rb index 2dd3d350211..a67f99a2741 100644 --- a/spec/rubocop/cop/style/safe_navigation_spec.rb +++ b/spec/rubocop/cop/style/safe_navigation_spec.rb @@ -674,6 +674,15 @@ expect(new_source).to eq("#{variable}&.bar(baz)") end + it 'corrects an object check followed by a method call ' \ + 'with a comment at EOL' do + source = "foo if #{variable} && #{variable}.bar # comment" + + new_source = autocorrect_source(source) + + expect(new_source).to eq("foo if #{variable}&.bar # comment") + end + it 'corrects a method call with a block safeguarded with a check ' \ 'for the object' do source = "#{variable}.bar { |e| e.qux } if #{variable}"