class RuboCop::Cop::Style::MethodCallWithArgsParentheses
Array 1
# good
Array(1)
# good
# AllowParenthesesInCamelCaseMethod: true
Array 1
# good
Array(1)
# bad
# AllowParenthesesInCamelCaseMethod: false (default)
foo().bar 1
# good
foo().bar(1)
# good
# AllowParenthesesInChaining: true
foo().bar 1
# good
foo().bar(1)
# bad
# AllowParenthesesInChaining: false (default)
strict: true
foo.enforce <br># good
)
strict: true
foo.enforce(
# good
# AllowParenthesesInMultilineCall: true
strict: true
foo.enforce <br># good
)
strict: true
foo.enforce(
# bad
# AllowParenthesesInMultilineCall: false (default)
foo.enforce strict: true
# good
foo.enforce(strict: true)
# bad
array.delete e
# good
array.delete(e)
# bad
@example EnforcedStyle: omit_parentheses
end
bar :baz
class Foo
# bad
# IgnoreMacros: false
end
bar :baz
class Foo
# good
# IgnoreMacros: true (default)
puts ‘test’
# okay with ‘puts` listed in `IgnoredMethods`
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)
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 ignored).
`IncludedMacros` and `IgnoredMethods`, then the latter takes
the `IncludedMacros` list. If a method is listed in both
either setting `IgnoreMacros` to false or adding specific macros to
option is valid only in the default style. Macros can be included by
Additional methods can be added to the `IgnoredMethods` list. This
In the default style (require_parentheses), macro methods are ignored.
method calls containing parameters.
This cop enforces the presence (default) or absence of parentheses in
def add_offense_for_omit_parentheses(node)
def add_offense_for_omit_parentheses(node) return unless node.parenthesized? return if node.implicit_call? return if super_call_without_arguments?(node) return if allowed_camel_case_method_call?(node) return if legitimate_call_with_parentheses?(node) add_offense(node, location: node.loc.begin.join(node.loc.end)) end
def add_offense_for_require_parentheses(node)
def add_offense_for_require_parentheses(node) return if ignored_method?(node.method_name) return if eligible_for_parentheses_omission?(node) return unless node.arguments? && !node.parenthesized? add_offense(node) end
def allowed_camel_case_method_call?(node)
def allowed_camel_case_method_call?(node) node.camel_case_method? && (node.arguments.none? || cop_config['AllowParenthesesInCamelCaseMethod']) end
def allowed_chained_call_with_parentheses?(node)
def allowed_chained_call_with_parentheses?(node) return false unless cop_config['AllowParenthesesInChaining'] previous = node.descendants.first return false unless previous && previous.send_type? previous.parenthesized? || allowed_chained_call_with_parentheses?(previous) end
def allowed_multiline_call_with_parentheses?(node)
def allowed_multiline_call_with_parentheses?(node) cop_config['AllowParenthesesInMultilineCall'] && node.multiline? end
def ambigious_literal?(node)
def ambigious_literal?(node) splat?(node) || ternary_if?(node) || regexp_slash_literal?(node) 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 assigned_before?(node, target)
def assigned_before?(node, target) node.assignment? && node.loc.operator.begin < target.loc.begin end
def autocorrect(node)
def autocorrect(node) case style when :require_parentheses autocorrect_for_require_parentheses(node) when :omit_parentheses autocorrect_for_omit_parentheses(node) end end
def autocorrect_for_omit_parentheses(node)
def autocorrect_for_omit_parentheses(node) lambda do |corrector| if parentheses_at_the_end_of_multiline_call?(node) corrector.replace(args_begin(node), ' \\') else corrector.replace(args_begin(node), ' ') end corrector.remove(node.loc.end) end end
def autocorrect_for_require_parentheses(node)
def autocorrect_for_require_parentheses(node) lambda do |corrector| corrector.replace(args_begin(node), '(') unless args_parenthesized?(node) corrector.insert_after(args_end(node), ')') end end end
def call_as_argument_or_chain?(node)
def call_as_argument_or_chain?(node) node.parent && (node.parent.send_type? && !assigned_before?(node.parent, node) || node.parent.csend_type? || node.parent.super_type?) end
def call_in_literals?(node)
def call_in_literals?(node) node.parent && (node.parent.pair_type? || node.parent.array_type? || node.parent.range_type? || splat?(node.parent) || ternary_if?(node.parent)) end
def call_in_logical_operators?(node)
def call_in_logical_operators?(node) node.parent && (logical_operator?(node.parent) || node.parent.send_type? && node.parent.arguments.any?(&method(:logical_operator?))) end
def call_in_optional_arguments?(node)
def call_in_optional_arguments?(node) node.parent && (node.parent.optarg_type? || node.parent.kwoptarg_type?) end
def call_with_ambiguous_arguments?(node)
def call_with_ambiguous_arguments?(node) call_with_braced_block?(node) || call_as_argument_or_chain?(node) || hash_literal_in_arguments?(node) || node.descendants.any? do |n| ambigious_literal?(n) || logical_operator?(n) || call_with_braced_block?(n) end end
def call_with_braced_block?(node)
def call_with_braced_block?(node) (node.send_type? || node.super_type?) && node.block_node && node.block_node.braces? end
def eligible_for_parentheses_omission?(node)
def eligible_for_parentheses_omission?(node) node.operator_method? || node.setter_method? || ignored_macro?(node) end
def hash_literal?(node)
def hash_literal?(node) node.hash_type? && node.braces? end
def hash_literal_in_arguments?(node)
def hash_literal_in_arguments?(node) node.arguments.any? do |n| hash_literal?(n) || n.send_type? && node.descendants.any?(&method(:hash_literal?)) end end
def ignored_macro?(node)
def ignored_macro?(node) cop_config['IgnoreMacros'] && node.macro? && !included_macros_list.include?(node.method_name) end
def included_macros_list
def included_macros_list cop_config.fetch('IncludedMacros', []).map(&:to_sym) end
def legitimate_call_with_parentheses?(node)
def legitimate_call_with_parentheses?(node) call_in_literals?(node) || call_with_ambiguous_arguments?(node) || call_in_logical_operators?(node) || call_in_optional_arguments?(node) || allowed_multiline_call_with_parentheses?(node) || allowed_chained_call_with_parentheses?(node) end
def logical_operator?(node)
def logical_operator?(node) (node.and_type? || node.or_type?) && node.logical_operator? end
def message(_node = nil)
def message(_node = nil) case style when :require_parentheses 'Use parentheses for method calls with arguments.'.freeze when :omit_parentheses 'Omit parentheses for method calls with arguments.'.freeze end end
def on_send(node)
def on_send(node) case style when :require_parentheses add_offense_for_require_parentheses(node) when :omit_parentheses add_offense_for_omit_parentheses(node) end end
def parentheses_at_the_end_of_multiline_call?(node)
def parentheses_at_the_end_of_multiline_call?(node) node.multiline? && node.loc.begin.source_line .gsub(TRAILING_WHITESPACE_REGEX, '') .end_with?('(') end
def regexp_slash_literal?(node)
def regexp_slash_literal?(node) node.regexp_type? && node.loc.begin.source == '/' end
def splat?(node)
def splat?(node) node.splat_type? || node.kwsplat_type? || node.block_pass_type? end
def super_call_without_arguments?(node)
def super_call_without_arguments?(node) node.super_type? && node.arguments.none? end
def ternary_if?(node)
def ternary_if?(node) node.if_type? && node.ternary? end