From 679364500ac4dfbb9ca11b5e894951a45bce09fc Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 24 Apr 2020 18:56:05 +0900 Subject: [PATCH] Support autocorrection for `Lint/ParenthesesAsGroupedExpression` Follow up to https://github.com/rubocop-hq/rubocop/pull/7909. This PR supports autocorrection for `Lint/ParenthesesAsGroupedExpression`. --- CHANGELOG.md | 1 + config/default.yml | 1 + .../lint/parentheses_as_grouped_expression.rb | 11 +++++++++- manual/cops_lint.md | 2 +- .../parentheses_as_grouped_expression_spec.rb | 22 ++++++++++++++----- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f708247abd..54df5bcf232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [#7918](https://github.com/rubocop-hq/rubocop/pull/7918): Support autocorrection for `Lint/AmbiguousOperator`. ([@koic][]) * [#7937](https://github.com/rubocop-hq/rubocop/pull/7937): Support autocorrection for `Style/IfWithSemicolon`. ([@koic][]) * [#3696](https://github.com/rubocop-hq/rubocop/issues/3696): Add `AllowComments` option to `Lint/EmptyWhen` cop. ([@koic][]) +* [#7910](https://github.com/rubocop-hq/rubocop/pull/7910): Support autocorrection for `Lint/ParenthesesAsGroupedExpression`. ([@koic][]) ### Bug fixes diff --git a/config/default.yml b/config/default.yml index 7306789af05..510d43a67e4 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1571,6 +1571,7 @@ Lint/ParenthesesAsGroupedExpression: StyleGuide: '#parens-no-spaces' Enabled: true VersionAdded: '0.12' + VersionChanged: '0.83' Lint/PercentStringArray: Description: >- diff --git a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb index 4a21bb7b8d9..6d767cd5c2c 100644 --- a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +++ b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb @@ -30,10 +30,19 @@ def on_send(node) range = space_range(node.first_argument.source_range, space_length) - add_offense(nil, location: range) + add_offense(node, location: range) end alias on_csend on_send + def autocorrect(node) + space_length = spaces_before_left_parenthesis(node) + range = space_range(node.first_argument.source_range, space_length) + + lambda do |corrector| + corrector.remove(range) + end + end + private def grouped_parentheses?(node) diff --git a/manual/cops_lint.md b/manual/cops_lint.md index 5dae782b017..8fa3709e693 100644 --- a/manual/cops_lint.md +++ b/manual/cops_lint.md @@ -1449,7 +1449,7 @@ p [''.frozen?, ''.encoding] #=> [true, #] Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- -Enabled | Yes | No | 0.12 | - +Enabled | Yes | Yes | 0.12 | 0.83 Checks for space between the name of a called method and a left parenthesis. diff --git a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb index b027b4b3605..c7a5d0a1e4b 100644 --- a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +++ b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb @@ -3,20 +3,28 @@ RSpec.describe RuboCop::Cop::Lint::ParenthesesAsGroupedExpression do subject(:cop) { described_class.new } - it 'registers an offense for method call with space before the ' \ - 'parenthesis' do + it 'registers an offense and corrects for method call with space before ' \ + 'the parenthesis' do expect_offense(<<~RUBY) a.func (x) ^ `(...)` interpreted as grouped expression. RUBY + + expect_correction(<<~RUBY) + a.func(x) + RUBY end - it 'registers an offense for predicate method call with space ' \ + it 'registers an offense and corrects for predicate method call with space ' \ 'before the parenthesis' do expect_offense(<<~RUBY) is? (x) ^ `(...)` interpreted as grouped expression. RUBY + + expect_correction(<<~RUBY) + is?(x) + RUBY end it 'does not register an offense for math expression' do @@ -67,12 +75,16 @@ end context 'when using safe navigation operator' do - it 'registers an offense for method call with space before the ' \ - 'parenthesis' do + it 'registers an offense and corrects for method call with space before ' \ + 'the parenthesis' do expect_offense(<<~RUBY) a&.func (x) ^ `(...)` interpreted as grouped expression. RUBY + + expect_correction(<<~RUBY) + a&.func(x) + RUBY end end end