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
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb