Class: Rubocop::Cop::Lambda

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

Constant Summary collapse

SINGLE_MSG =
'Use the new lambda literal syntax ->(params) {...}.'
MULTI_MSG =
'Use the lambda method for multi-line lambdas.'
TARGET =
s(:send, nil, :lambda)

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#on_block(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubocop/cop/lambda.rb', line 11

def on_block(node)
  # We're looking for
  # (block
  #   (send nil :lambda)
  #   ...)
  block_method, = *node

  if block_method == TARGET
    selector = block_method.loc.selector.source
    lambda_length = lambda_length(node)

    if selector != '->' && lambda_length == 0
      add_offence(:convention, block_method.loc.line, SINGLE_MSG)
    elsif selector == '->' && lambda_length > 0
      add_offence(:convention, block_method.loc.line, MULTI_MSG)
    end
  end

  super
end