Sha256: 310dade522a5dae8d27287c4eb75c9ff7fc81db1af258234a268fbd59f4ba46e

Contents?: true

Size: 1.21 KB

Versions: 166

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  # A wrapper around patterns array to perform optimized search.
  #
  # For projects with a large set of rubocop todo files, most items in `Exclude`/`Include`
  # are exact file names. It is wasteful to linearly check the list of patterns over and over
  # to check if the file is relevant to the cop.
  #
  # This class partitions an array of patterns into a set of exact match strings and the rest
  # of the patterns. This way we can firstly do a cheap check in the set and then proceed via
  # the costly patterns check, if needed.
  # @api private
  class FilePatterns
    @cache = {}.compare_by_identity

    def self.from(patterns)
      @cache[patterns] ||= new(patterns)
    end

    def initialize(patterns)
      @strings = Set.new
      @patterns = []
      partition_patterns(patterns)
    end

    def match?(path)
      @strings.include?(path) || @patterns.any? { |pattern| PathUtil.match_path?(pattern, path) }
    end

    private

    def partition_patterns(patterns)
      patterns.each do |pattern|
        if pattern.is_a?(String) && !pattern.match?(/[*{\[?]/)
          @strings << pattern
        else
          @patterns << pattern
        end
      end
    end
  end
end

Version data entries

166 entries across 165 versions & 17 rubygems

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