Sha256: f4d64dad759f265d7da9743e3e61675b746cb5a19b244f4fdb65fd6e418c9e05

Contents?: true

Size: 1.6 KB

Versions: 11

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for use of the lambda.(args) syntax.
      #
      # @example EnforcedStyle: call (default)
      #  # bad
      #  lambda.(x, y)
      #
      #  # good
      #  lambda.call(x, y)
      #
      # @example EnforcedStyle: braces
      #  # bad
      #  lambda.call(x, y)
      #
      #  # good
      #  lambda.(x, y)
      class LambdaCall < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector

        MSG = 'Prefer the use of `%<prefer>s` over `%<current>s`.'
        RESTRICT_ON_SEND = %i[call].freeze

        def on_send(node)
          return unless node.receiver

          if offense?(node)
            prefer = prefer(node)
            current = node.source

            add_offense(node, message: format(MSG, prefer: prefer, current: current)) do |corrector|
              opposite_style_detected
              corrector.replace(node, prefer)
            end
          else
            correct_style_detected
          end
        end

        private

        def offense?(node)
          (explicit_style? && node.implicit_call?) || (implicit_style? && !node.implicit_call?)
        end

        def prefer(node)
          receiver = node.receiver.source
          arguments = node.arguments.map(&:source).join(', ')
          method = explicit_style? ? "call(#{arguments})" : "(#{arguments})"

          "#{receiver}.#{method}"
        end

        def implicit_style?
          style == :braces
        end

        def explicit_style?
          style == :call
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.29.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.29.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.28.2 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.28.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.28.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.27.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.26.1 lib/rubocop/cop/style/lambda_call.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.26.0 lib/rubocop/cop/style/lambda_call.rb