Sha256: 063c8397a22e7515e277f7c736f8f44b538fd6c059e6331c09ddbf72b0dbb87c

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

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)
          lambda do |corrector|
            if style == :call
              receiver_node, = *node
              receiver = receiver_node.source
              replacement = node.source.sub("#{receiver}.", "#{receiver}.call")
              corrector.replace(node.source_range, 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

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/lambda_call.rb
rubocop-0.47.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.47.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.46.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.45.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.44.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-0.44.0 lib/rubocop/cop/style/lambda_call.rb