Class: RuboCop::TargetFinder

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

Overview

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TargetFinder.



61
62
63
64
# File 'lib/rubocop/target_finder.rb', line 61

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

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubocop/target_finder.rb', line 70

def debug?
  @options[:debug]
end

#excluded_dirs(base_dir) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/rubocop/target_finder.rb', line 160

def excluded_dirs(base_dir)
  all_cops_config = @config_store.for(base_dir).for_all_cops
  dir_tree_excludes = all_cops_config['Exclude'].select do |pattern|
    pattern.is_a?(String) && pattern.end_with?('/**/*')
  end
  dir_tree_excludes.map { |pattern| pattern.sub(%r{/\*\*/\*$}, '') }
end

#fail_fast?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rubocop/target_finder.rb', line 74

def fail_fast?
  @options[:fail_fast]
end

#find(args) ⇒ Array

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubocop/target_finder.rb', line 82

def find(args)
  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)
             end
  end

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

#find_files(base_dir, flags) ⇒ Object

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/*/).



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rubocop/target_finder.rb', line 137

def find_files(base_dir, flags)
  wanted_toplevel_dirs = toplevel_dirs(base_dir, flags) -
                         excluded_dirs(base_dir)
  wanted_toplevel_dirs.map! { |dir| dir << '/**/*' }

  pattern = if wanted_toplevel_dirs.empty?
              # We need this special case to avoid creating the pattern
              # /**/* which searches the entire file system.
              ["#{base_dir}/**/*"]
            else
              # Search the non-excluded top directories, but also add files
              # on the top level, which would otherwise not be found.
              wanted_toplevel_dirs.unshift("#{base_dir}/*")
            end
  Dir.glob(pattern, flags).select { |path| FileTest.file?(path) }
end

#force_exclusion?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rubocop/target_finder.rb', line 66

def force_exclusion?
  @options[:force_exclusion]
end

#process_explicit_path(path) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rubocop/target_finder.rb', line 189

def process_explicit_path(path)
  files = path.include?('*') ? Dir[path] : [path]

  files.select! { |file| ruby_file?(file) }

  return files unless force_exclusion?

  files.reject do |file|
    config = @config_store.for(file)
    config.file_to_exclude?(file)
  end
end

#ruby_executable?(file) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
# File 'lib/rubocop/target_finder.rb', line 176

def ruby_executable?(file)
  return false unless File.extname(file).empty? && File.exist?(file)
  first_line = File.open(file, &:readline)
  !(first_line =~ /#!.*(#{RUBY_INTERPRETERS.join('|')})/).nil?
rescue EOFError, ArgumentError => e
  warn "Unprocessable file #{file}: #{e.class}, #{e.message}" if debug?
  false
end

#ruby_extension?(file) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/rubocop/target_finder.rb', line 168

def ruby_extension?(file)
  RUBY_EXTENSIONS.include?(File.extname(file))
end

#ruby_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/rubocop/target_finder.rb', line 185

def ruby_file?(file)
  ruby_extension?(file) || ruby_filename?(file) || ruby_executable?(file)
end

#ruby_filename?(file) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/rubocop/target_finder.rb', line 172

def ruby_filename?(file)
  RUBY_FILENAMES.include?(File.basename(file))
end

#target_files_in_dir(base_dir = Dir.pwd) ⇒ Array

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubocop/target_finder.rb', line 107

def target_files_in_dir(base_dir = Dir.pwd)
  # Support Windows: Backslashes from command-line -> forward slashes
  if File::ALT_SEPARATOR
    base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
  end
  all_files = find_files(base_dir, File::FNM_DOTMATCH)
  hidden_files = Set.new(all_files - find_files(base_dir, 0))
  base_dir_config = @config_store.for(base_dir)

  target_files = all_files.select do |file|
    to_inspect?(file, hidden_files, base_dir_config)
  end

  # Most recently modified file first.
  target_files.sort_by! { |path| -File.mtime(path).to_i } if fail_fast?

  target_files
end

#to_inspect?(file, hidden_files, base_dir_config) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/rubocop/target_finder.rb', line 126

def to_inspect?(file, hidden_files, base_dir_config)
  return false if base_dir_config.file_to_exclude?(file)
  return true if !hidden_files.include?(file) && ruby_file?(file)
  base_dir_config.file_to_include?(file)
end

#toplevel_dirs(base_dir, flags) ⇒ Object



154
155
156
157
158
# File 'lib/rubocop/target_finder.rb', line 154

def toplevel_dirs(base_dir, flags)
  Dir.glob(File.join(base_dir, '*'), flags).select do |dir|
    File.directory?(dir) && !dir.end_with?('/.', '/..')
  end
end