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.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb