Sha256: 33b807981ec50b2c5bd7fed826360d80774ff6907ab17935c06b593fcd4c17cf

Contents?: true

Size: 1.22 KB

Versions: 11

Compression:

Stored size: 1.22 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

          return unless block_method == TARGET
          selector = block_method.loc.selector.source
          lambda_length = lambda_length(node)

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

11 entries across 11 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/lambda.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/lambda.rb
rubocop-0.28.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.27.1 lib/rubocop/cop/style/lambda.rb
rubocop-0.27.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.26.1 lib/rubocop/cop/style/lambda.rb
rubocop-0.26.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.25.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.24.1 lib/rubocop/cop/style/lambda.rb
rubocop-0.24.0 lib/rubocop/cop/style/lambda.rb
rubocop-0.23.0 lib/rubocop/cop/style/lambda.rb