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

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?, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

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

Instance Method Details

#autocorrect(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/style/lambda_call.rb', line 30

def autocorrect(node)
  @corrections << lambda do |corrector|
    if style == :call
      receiver_node, = *node
      expr = node.loc.expression
      receiver = receiver_node.loc.expression.source
      replacement = expr.source.sub("#{receiver}.", "#{receiver}.call")
      corrector.replace(expr, replacement)
    else
      corrector.remove(node.loc.selector)
    end
  end
end

#message(node) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rubocop/cop/style/lambda_call.rb', line 44

def message(node)
  if style == :call
    'Prefer the use of `lambda.call(...)` over `lambda.(...)`.'
  else
    'Prefer the use of `lambda.(...)` over `lambda.call(...)`.'
  end
end

#on_send(node) ⇒ Object



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

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

  # we care only about `call` methods
  return unless selector == :call

  if style == :call && node.loc.selector.nil?
    # lambda.() does not have a selector
    convention(node, :expression)
  elsif style == :braces && node.loc.selector
    convention(node, :expression)
  end
end