Sha256: f537086fda7847120d6de97b5bbca8d560afc7ff0df322287fc63eb99d73bea0

Contents?: true

Size: 1.7 KB

Versions: 125

Compression:

Stored size: 1.7 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
        include IgnoredNode
        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|
              next if part_of_ignored_node?(node)

              opposite_style_detected
              corrector.replace(node, prefer)

              ignore_node(node)
            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

125 entries across 125 versions & 11 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.67.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.66.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.66.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.65.1 lib/rubocop/cop/style/lambda_call.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.65.0 lib/rubocop/cop/style/lambda_call.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.64.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.63.4 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.63.3 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.63.2 lib/rubocop/cop/style/lambda_call.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.63.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.63.0 lib/rubocop/cop/style/lambda_call.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/lambda_call.rb
rubocop-1.62.1 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.62.0 lib/rubocop/cop/style/lambda_call.rb
rubocop-1.61.0 lib/rubocop/cop/style/lambda_call.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/lambda_call.rb