class RuboCop::Cop::Style::NestedParenthesizedCalls
method1(method2 arg, method3, arg)
@bad
method1(method2(arg), method3(arg))
@good
@example
of a parenthesized method call.
This cop checks for unparenthesized method calls in the argument list
def autocorrect(nested)
def autocorrect(nested) _scope, _method_name, *args = *nested first_arg = args.first.source_range last_arg = args.last.source_range first_arg_with_space = range_with_surrounding_space(first_arg, :left) leading_space = first_arg_with_space.begin.resize(1) lambda do |corrector| corrector.replace(leading_space, '(') corrector.insert_after(last_arg, ')') end end
def on_send(node)
def on_send(node) return unless parenthesized_call?(node) node.each_child_node(:send) do |nested| next if nested.method_args.empty? || parenthesized_call?(nested) || operator?(nested.method_name) || rspec_matcher?(node, nested) || nested.asgn_method_call? add_offense(nested, nested.source_range, format(MSG, nested.source)) end end
def rspec_matcher?(parent, send)
def rspec_matcher?(parent, send) parent.method_args.one? && # .to, .not_to, etc RSPEC_MATCHERS.include?(send.method_name) && send.method_args.one? end