Sha256: 757ee673dee72b9f96563622cb2e69409ca6b62b09d178f264e82f06cc8c5c6f

Contents?: true

Size: 1.25 KB

Versions: 166

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Enforces the use of `node.lambda_or_proc?` instead of `node.lambda? || node.proc?`.
      #
      # @example
      #   # bad
      #   node.lambda? || node.proc?
      #   node.proc? || node.lambda?
      #
      #   # good
      #   node.lambda_or_proc?
      #
      class LambdaOrProc < Base
        extend AutoCorrector

        MSG = 'Use `%<prefer>s`.'

        # @!method lambda_or_proc(node)
        def_node_matcher :lambda_or_proc, <<~PATTERN
          {
            (or $(send _node :lambda?) $(send _node :proc?))
            (or $(send _node :proc?) $(send _node :lambda?))
            (or
              (or _ $(send _node :lambda?)) $(send _node :proc?))
            (or
              (or _ $(send _node :proc?)) $(send _node :lambda?))
          }
        PATTERN

        def on_or(node)
          return unless (lhs, rhs = lambda_or_proc(node))

          offense = lhs.receiver.source_range.join(rhs.source_range.end)
          prefer = "#{lhs.receiver.source}.lambda_or_proc?"

          add_offense(offense, message: format(MSG, prefer: prefer)) do |corrector|
            corrector.replace(offense, prefer)
          end
        end
      end
    end
  end
end

Version data entries

166 entries across 165 versions & 17 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/lambda_or_proc.rb