Sha256: 3d0adcad64eeeba3bce58b9227315e56701e4693c0975b22a2676f641b6c7f75

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for uses of the pre 1.9 lambda syntax for one-line
      # anonymous functions and uses of the 1.9 lambda syntax for multi-line
      # anonymous functions.
      class Lambda < Cop
        SINGLE_MSG = 'Use the new lambda literal syntax ->(params) {...}.'
        MULTI_MSG = 'Use the lambda method for multi-line lambdas.'

        TARGET = s(:send, nil, :lambda)

        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
              convention(block_method, :expression, SINGLE_MSG)
            elsif selector == '->' && lambda_length > 0
              convention(block_method, :expression, MULTI_MSG)
            end
          end
        end

        private

        def lambda_length(block_node)
          start_line = block_node.loc.begin.line
          end_line = block_node.loc.end.line

          end_line - start_line
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.15.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.14.1 lib/rubocop/cop/style/lambda.rb
rubocop-0.14.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.13.1 lib/rubocop/cop/style/lambda.rb
rubocop-0.13.0 lib/rubocop/cop/style/lambda.rb