Sha256: 855c8d53fd65f00af5f838f8d23aec21feeb51bcee9e519f0a82d0399ceea970

Contents?: true

Size: 1.01 KB

Versions: 35

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require 'pathname'

module RuboCop
  # Common methods for finding files.
  # @api private
  module FileFinder
    def self.root_level=(level)
      @root_level = level
    end

    def self.root_level?(path, stop_dir)
      (@root_level || stop_dir) == path.to_s
    end

    def find_file_upwards(filename, start_dir, stop_dir = nil)
      traverse_files_upwards(filename, start_dir, stop_dir) do |file|
        # minimize iteration for performance
        return file if file
      end
    end

    def find_last_file_upwards(filename, start_dir, stop_dir = nil)
      last_file = nil
      traverse_files_upwards(filename, start_dir, stop_dir) do |file|
        last_file = file
      end
      last_file
    end

    private

    def traverse_files_upwards(filename, start_dir, stop_dir)
      Pathname.new(start_dir).expand_path.ascend do |dir|
        file = dir + filename
        yield(file.to_s) if file.exist?

        break if FileFinder.root_level?(dir, stop_dir)
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/file_finder.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/file_finder.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/file_finder.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/file_finder.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/file_finder.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/file_finder.rb
rubocop-1.12.1 lib/rubocop/file_finder.rb
rubocop-1.12.0 lib/rubocop/file_finder.rb
rubocop-1.11.0 lib/rubocop/file_finder.rb
rubocop-1.10.0 lib/rubocop/file_finder.rb
rubocop-1.9.1 lib/rubocop/file_finder.rb
rubocop-1.9.0 lib/rubocop/file_finder.rb
rubocop-1.8.1 lib/rubocop/file_finder.rb
rubocop-1.8.0 lib/rubocop/file_finder.rb
rubocop-1.7.0 lib/rubocop/file_finder.rb
rubocop-1.6.1 lib/rubocop/file_finder.rb
rubocop-1.6.0 lib/rubocop/file_finder.rb
rubocop-1.5.2 lib/rubocop/file_finder.rb
rubocop-1.5.1 lib/rubocop/file_finder.rb
rubocop-1.5.0 lib/rubocop/file_finder.rb