class RuboCop::Cop::Style::MethodCallWithArgsParentheses

“#{t ‘this.is.also.good’}”
# good
“#{t(‘this.is.good’)}”
# good
@example AllowParenthesesInStringInterpolation: true
“#{t ‘this.is.better’}”
# good
“#{t(‘this.is.bad’)}”
# bad
@example AllowParenthesesInStringInterpolation: false (default)
Array 1
# good
Array(1)
# good
@example AllowParenthesesInCamelCaseMethod: true
Array 1
# good
Array(1)
# bad
@example AllowParenthesesInCamelCaseMethod: false (default)
foo().bar 1
# good
foo().bar(1)
# good
@example AllowParenthesesInChaining: true
foo().bar 1
# good
foo().bar(1)
# bad
@example AllowParenthesesInChaining: false (default)
strict: true
foo.enforce <br># good
)
strict: true
foo.enforce(
# good
@example AllowParenthesesInMultilineCall: true
strict: true
foo.enforce <br># good
)
strict: true
foo.enforce(
# bad
@example AllowParenthesesInMultilineCall: false (default)
end
bar :baz
class Foo
# bad
@example IgnoreMacros: false
end
bar :baz
class Foo
# good
@example IgnoreMacros: true (default)
array.[] index
# Operators methods without parens, if you prefer
# good
array&.[](index)
# Operators methods calls with parens
# good
array.[] index
# Operators methods without parens, if you prefer
# good
array&.[](index)
# Operators methods calls with parens
# good
yield path, File.basename(path)
# Allows parens for calls that won’t produce valid Ruby or be ambiguous.
# good
model.validate strict(true)
# Allows parens for calls that won’t produce valid Ruby or be ambiguous.
# good
foo.enforce strict: true
# good
foo.enforce(strict: true)
# bad
array.delete e
# good
array.delete(e)
# bad
@example EnforcedStyle: omit_parentheses
assert_equal ‘test’, x
# okay with ‘^assert` listed in `AllowedPatterns`
puts ’test’
# okay with ‘puts` listed in `AllowedMethods`
foo.bar = baz
# Setter methods don’t need parens
# good
foo == bar
# Operators don’t need parens
# good
array.delete(e)
# good
array.delete e
# bad
@example EnforcedStyle: require_parentheses (default)
because of the following issue: bugs.ruby-lang.org/issues/18396.
And Ruby 3.1’s hash omission syntax has a case that requires parentheses
in Ruby 2.7 as omitting them starts an endless range.
allowed when forwarding arguments with the triple-dot syntax introduced
endless method definition introduced in Ruby 3.0. Parentheses are also
parentheses are required around a method with arguments when inside an
results in ambiguous or syntactically incorrect code. For example,
NOTE: Parentheses are still allowed in cases where omitting them
even with arguments.
to ‘true` allows the presence of parentheses in such a method call
begins with a capital letter and which has no arguments. Setting it
allows the presence of parentheses when calling a method whose name
3. `AllowParenthesesInCamelCaseMethod` is `false` by default. This
calls.
to `true` allows the presence of parentheses in multi-line method
2. `AllowParenthesesInMultilineCall` is `false` by default. Setting it
method chaining.
`true` allows the presence of parentheses in the last call during
1. `AllowParenthesesInChaining` is `false` by default. Setting it to
options.
In the alternative style (omit_parentheses), there are three additional
precedence (that is, the method is allowed).
`IncludedMacros` and `AllowedMethods`, then the latter takes
eg. If a method is listed in both
3. `IncludedMacros`
2. `AllowedPatterns`
1. `AllowedMethods`
Precedence of options is all follows:
the `IncludedMacros` list.
either setting `IgnoreMacros` to false or adding specific macros to
valid only in the default style. Macros can be included by
or `AllowedPatterns` list. These options are
Additional methods can be added to the `AllowedMethods`
In the default style (require_parentheses), macro methods are allowed.
method calls containing parameters.
Enforces the presence (default) or absence of parentheses in

def self.autocorrect_incompatible_with

def self.autocorrect_incompatible_with
  [Style::NestedParenthesizedCalls, Style::RescueModifier]
end

def args_begin(node)

def args_begin(node)
  loc = node.loc
  selector =
    node.super_type? || node.yield_type? ? loc.keyword : loc.selector
  resize_by = args_parenthesized?(node) ? 2 : 1
  selector.end.resize(resize_by)
end

def args_end(node)

def args_end(node)
  node.loc.expression.end
end

def args_parenthesized?(node)

def args_parenthesized?(node)
  return false unless node.arguments.one?
  first_node = node.arguments.first
  first_node.begin_type? && first_node.parenthesized_call?
end

def on_send(node)

def on_send(node)
  send(style, node) # call require_parentheses or omit_parentheses
end