class RuboCop::Cop::Style::MethodCallParentheses

This cop checks for unwanted parentheses in parameterless method calls.

Constants

ASGN_NODES
MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/method_call_parentheses.rb, line 26
def autocorrect(node)
  lambda do |corrector|
    corrector.remove(node.loc.begin)
    corrector.remove(node.loc.end)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/method_call_parentheses.rb, line 13
def on_send(node)
  _receiver, method_name, *args = *node

  # methods starting with a capital letter should be skipped
  return if method_name =~ /\A[A-Z]/
  return unless args.empty? && node.loc.begin
  return if same_name_assignment?(node)
  return if lambda_call_syntax?(node)
  return if node.keyword_not?

  add_offense(node, :begin)
end

Private Instance Methods

lambda_call_syntax?(node) click to toggle source

don't check `lambda.()` syntax; the Style/LambdaCall cop does that

# File lib/rubocop/cop/style/method_call_parentheses.rb, line 57
def lambda_call_syntax?(node)
  node.method_name == :call && node.loc.selector.nil?
end
same_name_assignment?(node) click to toggle source
# File lib/rubocop/cop/style/method_call_parentheses.rb, line 35
def same_name_assignment?(node)
  _receiver, method_name, *_args = *node

  node.each_ancestor(*ASGN_NODES).any? do |asgn_node|
    if asgn_node.masgn_type?
      mlhs_node, _mrhs_node = *asgn_node
      asgn_node = mlhs_node.children[node.sibling_index]
    end
    # `obj.method = value` parses as (send ... :method= ...), and will
    # not be returned as an `asgn_node` here
    # however, `obj.method ||= value` parses as (or-asgn (send ...) ...)
    # which IS an `asgn_node`
    if asgn_node.or_asgn_type? || asgn_node.and_asgn_type?
      asgn_node, _value = *asgn_node
      return false if asgn_node.send_type?
    end

    asgn_node.loc.name.source == method_name.to_s
  end
end