Class: Rubocop::Cop::Style::LambdaCall

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/lambda_call.rb

Overview

This cop checks for use of the lambda.(args) syntax.

Examples:


# bad
lambda.(x, y)

# good
lambda.call(x, y)

Constant Summary collapse

MSG =
'Prefer the use of `lambda.call(...)` over `lambda.(...)`.'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, rails?, style?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#autocorrect_action(node) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/style/lambda_call.rb', line 27

def autocorrect_action(node)
  @corrections << lambda do |corrector|
    receiver_node, = *node
    expr = node.loc.expression
    receiver = receiver_node.loc.expression.source
    replacement = expr.source.sub("#{receiver}.", "#{receiver}.call")
    corrector.replace(expr, replacement)
  end
end

#on_send(node) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/style/lambda_call.rb', line 18

def on_send(node)
  _receiver, selector, = *node

  # lambda.() does not have a selector
  return unless selector == :call && node.loc.selector.nil?

  convention(node, :expression)
end