Sha256: f66d074a2cbcbacb09082b7be6ee89df64943cb0389154b6d79843b5feb9f6f0

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for use of the lambda.(args) syntax.
      #
      # @example
      #
      #  # bad
      #  lambda.(x, y)
      #
      #  # good
      #  lambda.call(x, y)
      class LambdaCall < Cop
        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

        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

        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

        private

        def style
          case cop_config['EnforcedStyle']
          when 'call' then :call
          when 'braces' then :braces
          else fail 'Unknown style selected!'
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.15.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.14.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.14.0 lib/rubocop/cop/style/lambda_call.rb