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

[Fix #7962] Fix a false positive for Lint/ParenthesesAsGroupedExpression #7963

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 @@ -5,6 +5,7 @@
### Bug fixes

* [#7953](https://github.com/rubocop-hq/rubocop/issues/7953): Fix an error for `Lint/AmbiguousOperator` when a method with no arguments is used in advance. ([@koic][])
* [#7962](https://github.com/rubocop-hq/rubocop/issues/7962): Fix a false positive for `Lint/ParenthesesAsGroupedExpression` when heredoc has a space between the same string as the method name and `(`. ([@koic][])

### Changes

Expand Down
14 changes: 11 additions & 3 deletions lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
Expand Up @@ -21,9 +21,7 @@ class ParenthesesAsGroupedExpression < Cop
MSG = '`(...)` interpreted as grouped expression.'

def on_send(node)
return unless node.arguments.one?
return if node.operator_method? || node.setter_method?
return if grouped_parentheses?(node)
return if valid_context?(node)

space_length = spaces_before_left_parenthesis(node)
return unless space_length.positive?
Expand All @@ -45,6 +43,16 @@ def autocorrect(node)

private

def valid_context?(node)
return true unless node.arguments.one? && first_arugment_starts_with_left_parenthesis?(node)

node.operator_method? || node.setter_method? || grouped_parentheses?(node)
end

def first_arugment_starts_with_left_parenthesis?(node)
node.first_argument.source.start_with?('(')
end

def grouped_parentheses?(node)
first_argument = node.first_argument

Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb
Expand Up @@ -74,6 +74,17 @@
expect_no_offenses('assert_equal (0..1.9), acceleration.domain')
end

it 'does not register an offesne when heredoc has a space between the same string as the method name and `(`' do
expect_no_offenses(<<~RUBY)
foo(
<<~EOS
foo (
)
EOS
)
RUBY
end

context 'when using safe navigation operator' do
it 'registers an offense and corrects for method call with space before ' \
'the parenthesis' do
Expand Down