class RubbyCop::Cop::Style::NestedParenthesizedCalls

This cop checks for unparenthesized method calls in the argument list of a parenthesized method call.

@example

@good
method1(method2(arg), method3(arg))

@bad
method1(method2 arg, method3, arg)

Constants

MSG
RSPEC_MATCHERS

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubbycop/cop/style/nested_parenthesized_calls.rb, line 21
def on_send(node)
  return unless node.parenthesized?

  node.each_child_node(:send) do |nested|
    next if allowed_omission?(nested)

    add_offense(nested, nested.source_range, format(MSG, nested.source))
  end
end

Private Instance Methods

allowed_omission?(send_node) click to toggle source
# File lib/rubbycop/cop/style/nested_parenthesized_calls.rb, line 33
def allowed_omission?(send_node)
  !send_node.arguments? || send_node.parenthesized? ||
    send_node.setter_method? || send_node.operator_method? ||
    rspec_matcher?(send_node)
end
autocorrect(nested) click to toggle source
# File lib/rubbycop/cop/style/nested_parenthesized_calls.rb, line 46
def autocorrect(nested)
  first_arg = nested.first_argument.source_range
  last_arg = nested.last_argument.source_range

  leading_space =
    range_with_surrounding_space(first_arg, :left).begin.resize(1)

  lambda do |corrector|
    corrector.replace(leading_space, '(')
    corrector.insert_after(last_arg, ')')
  end
end
rspec_matcher?(send_node) click to toggle source

TODO: Relegate this from RubbyCop core

# File lib/rubbycop/cop/style/nested_parenthesized_calls.rb, line 40
def rspec_matcher?(send_node)
  send_node.parent.arguments.one? && # .to, .not_to, etc
    RSPEC_MATCHERS.include?(send_node.method_name) &&
    send_node.arguments.one?
end