class RuboCop::Cop::Style::LambdaCall

Checks for use of the lambda.(args) syntax.

@example EnforcedStyle: call (default)

# bad
lambda.(x, y)

# good
lambda.call(x, y)

@example EnforcedStyle: braces

# bad
lambda.call(x, y)

# good
lambda.(x, y)

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/lambda_call.rb, line 29
def on_send(node)
  return unless node.receiver

  if offense?(node)
    prefer = prefer(node)
    current = node.source

    add_offense(node, message: format(MSG, prefer: prefer, current: current)) do |corrector|
      next if part_of_ignored_node?(node)

      opposite_style_detected
      corrector.replace(node, prefer)

      ignore_node(node)
    end
  else
    correct_style_detected
  end
end

Private Instance Methods

explicit_style?() click to toggle source
# File lib/rubocop/cop/style/lambda_call.rb, line 67
def explicit_style?
  style == :call
end
implicit_style?() click to toggle source
# File lib/rubocop/cop/style/lambda_call.rb, line 63
def implicit_style?
  style == :braces
end
offense?(node) click to toggle source
# File lib/rubocop/cop/style/lambda_call.rb, line 51
def offense?(node)
  (explicit_style? && node.implicit_call?) || (implicit_style? && !node.implicit_call?)
end
prefer(node) click to toggle source
# File lib/rubocop/cop/style/lambda_call.rb, line 55
def prefer(node)
  receiver = node.receiver.source
  arguments = node.arguments.map(&:source).join(', ')
  method = explicit_style? ? "call(#{arguments})" : "(#{arguments})"

  "#{receiver}.#{method}"
end