Sha256: cff2175ce1f34409f39939359986b208de94e9ac8916867ad5f1663f30b6f86d

Contents?: true

Size: 1.59 KB

Versions: 61

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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

61 entries across 54 versions & 8 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/lambda_call.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/lambda_call.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.55.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.55.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.54.2 lib/rubocop/cop/style/lambda_call.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/lambda_call.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/lambda_call.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.54.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.54.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.53.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.53.0 lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/lambda_call.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/lambda_call.rb