Sha256: b4232064562042d07275dea23c6ff7d8f9eaa37be14ba1fb26346a88c2cd84be

Contents?: true

Size: 1.58 KB

Versions: 15

Compression:

Stored size: 1.58 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
        include ConfigurableEnforcedStyle

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

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

          if offense?(node)
            add_offense(node, :expression) { opposite_style_detected }
          else
            correct_style_detected
          end
        end

        private

        def offense?(node)
          # lambda.() does not have a selector
          style == :call && node.loc.selector.nil? ||
            style == :braces && node.loc.selector
        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
      end
    end
  end
end

Version data entries

15 entries across 15 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_call.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/lambda_call.rb
rubocop-0.30.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.30.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.29.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.29.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.28.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.27.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.27.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.26.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.26.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.25.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.24.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.24.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.23.0 lib/rubocop/cop/style/lambda_call.rb