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 allowed_omission?(send_node)
def allowed_omission?(send_node) !send_node.arguments? || send_node.parenthesized? || send_node.setter_method? || send_node.operator_method? || whitelisted?(send_node) end
def autocorrect(nested)
def autocorrect(nested) first_arg = nested.first_argument.source_range last_arg = nested.last_argument.source_range leading_space = range_with_surrounding_space(range: first_arg, side: :left).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 node.parenthesized? node.each_child_node(:send, :csend) do |nested| next if allowed_omission?(nested) add_offense(nested, location: nested.source_range, message: format(MSG, source: nested.source)) end end
def whitelisted?(send_node)
def whitelisted?(send_node) send_node.parent.arguments.one? && whitelisted_methods.include?(send_node.method_name.to_s) && send_node.arguments.one? end
def whitelisted_methods
def whitelisted_methods cop_config['Whitelist'] || [] end