Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autocorrection for Lint/ParenthesesAsGroupedExpression #7910

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1571,6 +1571,7 @@ Lint/ParenthesesAsGroupedExpression:
StyleGuide: '#parens-no-spaces'
Enabled: true
VersionAdded: '0.12'
VersionChanged: '0.83'

Lint/PercentStringArray:
Description: >-
Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_lint.md
Expand Up @@ -1449,7 +1449,7 @@ p [''.frozen?, ''.encoding] #=> [true, #<Encoding:US-ASCII>]

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.
Expand Down
22 changes: 17 additions & 5 deletions spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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