diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a6f8fd1240..546bac31128 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,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 diff --git a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb index 6d767cd5c2c..f35f10c2a8b 100644 --- a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +++ b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb @@ -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? @@ -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 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 c7a5d0a1e4b..e21f7ca0ac5 100644 --- a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +++ b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb @@ -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