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

Allow parentheses in operator method calls for Style/MethodCallWithArgsParentheses #9620

Merged
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
@@ -0,0 +1 @@
* [#9620](https://github.com/rubocop/rubocop/pull/9620): Allow parentheses in operator methods calls for `Style/MethodCallWithArgsParentheses` `EnforcedStyle: omit_parentheses`. ([@gsamokovarov][])
24 changes: 24 additions & 0 deletions lib/rubocop/cop/style/method_call_with_args_parentheses.rb
Expand Up @@ -79,6 +79,30 @@ module Style
# # good
# foo.enforce strict: true
#
# # good
# # Allows parens for calls that won't produce valid Ruby or be ambiguous.
# model.validate strict(true)
#
# # good
# # Allows parens for calls that won't produce valid Ruby or be ambiguous.
# yield path, File.basename(path)
#
# # good
# # Operators methods calls with parens
# array&.[](index)
#
# # good
# # Operators methods without parens, if you prefer
# array.[] index
#
# # good
# # Operators methods calls with parens
# array&.[](index)
#
# # good
# # Operators methods without parens, if you prefer
# array.[] index
#
# @example IgnoreMacros: true (default)
#
# # good
Expand Down
Expand Up @@ -5,6 +5,7 @@ module Cop
module Style
class MethodCallWithArgsParentheses
# Style omit_parentheses
# rubocop:disable Metrics/ModuleLength
module OmitParentheses
TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
OMIT_MSG = 'Omit parentheses for method calls with arguments.'
Expand All @@ -15,7 +16,7 @@ module OmitParentheses
def omit_parentheses(node)
return unless node.parenthesized?
return if inside_endless_method_def?(node)
return if node.implicit_call?
return if syntax_like_method_call?(node)
return if super_call_without_arguments?(node)
return if allowed_camel_case_method_call?(node)
return if legitimate_call_with_parentheses?(node)
Expand Down Expand Up @@ -43,6 +44,10 @@ def inside_endless_method_def?(node)
node.each_ancestor(:def).any?(&:endless?) && node.arguments.any?
end

def syntax_like_method_call?(node)
node.implicit_call? || node.operator_method?
end

def super_call_without_arguments?(node)
node.super_type? && node.arguments.none?
end
Expand Down Expand Up @@ -173,6 +178,7 @@ def assigned_before?(node, target)
node.loc.operator.begin < target.loc.begin
end
end
# rubocop:enable Metrics/ModuleLength
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
Expand Up @@ -753,6 +753,16 @@ def seatle_style arg: default(42)
expect_no_offenses('Something.find(criteria: given)&.field')
end

it 'accepts parens in operator method calls' do
expect_no_offenses(<<~RUBY)
data.[](value)
data&.[](value)
string.<<(even_more_string)
ruby.==(good)
ruby&.===(better)
RUBY
end

context 'allowing parenthesis in chaining' do
let(:cop_config) do
{
Expand Down