Class: RuboCop::TargetFinder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/target_finder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class finds target files to inspect by scanning the directory tree and picking ruby files.

Constant Summary collapse

HIDDEN_PATH_SUBSTRING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"#{File::SEPARATOR}."

Instance Method Summary collapse

Constructor Details

#initialize(config_store, options = {}) ⇒ TargetFinder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TargetFinder.



9
10
11
12
# File 'lib/rubocop/target_finder.rb', line 9

def initialize(config_store, options = {})
  @config_store = config_store
  @options = options
end

Instance Method Details

#find(args, mode) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a list of target files by expanding globbing patterns (if any). If args is empty, recursively find all Ruby source files under the current directory

Returns:

  • (Array)

    array of file paths



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/target_finder.rb', line 17

def find(args, mode)
  return target_files_in_dir if args.empty?

  files = []

  args.uniq.each do |arg|
    files += if File.directory?(arg)
               target_files_in_dir(arg.chomp(File::SEPARATOR))
             else
               process_explicit_path(arg, mode)
             end
  end

  files.map { |f| File.expand_path(f) }.uniq
end

#find_files(base_dir, flags) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Search for files recursively starting at the given base directory using the given flags that determine how the match is made. Excluded files will be removed later by the caller, but as an optimization find_files removes the top level directories that are excluded in configuration in the normal way (dir/*/).



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubocop/target_finder.rb', line 58

def find_files(base_dir, flags)
  # get all wanted directories first to improve speed of finding all files
  exclude_pattern = combined_exclude_glob_patterns(base_dir)
  dir_flags = flags | File::FNM_PATHNAME | File::FNM_EXTGLOB
  patterns = wanted_dir_patterns(base_dir, exclude_pattern, dir_flags)
  patterns.map! { |dir| File.join(dir, '*') }
  # We need this special case to avoid creating the pattern
  # /**/* which searches the entire file system.
  patterns = [File.join(base_dir, '**/*')] if patterns.empty?

  Dir.glob(patterns, flags).select { |path| FileTest.file?(path) }
end

#target_files_in_dir(base_dir = Dir.pwd) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds all Ruby source files under the current or other supplied directory. A Ruby source file is defined as a file with the ‘.rb` extension or a file with no extension that has a ruby shebang line as its first line. It is possible to specify includes and excludes using the config file, so you can include other Ruby files like Rakefiles and gemspecs.

Parameters:

  • base_dir (defaults to: Dir.pwd)

    Root directory under which to search for ruby source files

Returns:

  • (Array)

    Array of filenames



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/target_finder.rb', line 41

def target_files_in_dir(base_dir = Dir.pwd)
  # Support Windows: Backslashes from command-line -> forward slashes
  base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
  all_files = find_files(base_dir, File::FNM_DOTMATCH)
  # use file.include? for performance optimization
  hidden_files = all_files.select { |file| file.include?(HIDDEN_PATH_SUBSTRING) }.sort
  base_dir_config = @config_store.for(base_dir)

  target_files = all_files.select { |file| to_inspect?(file, hidden_files, base_dir_config) }

  target_files.sort_by!(&order)
end