class Fasterer::FileTraverser

Constants

CONFIG_FILE_NAME
EXCLUDE_PATHS_KEY
SPEEDUPS_KEY

Attributes

config[R]
offenses_found[RW]
offenses_total_count[RW]
parse_error_paths[R]

Public Class Methods

new(path) click to toggle source
# File lib/fasterer/file_traverser.rb, line 18
def initialize(path)
  @path = Pathname(path || '.')
  @parse_error_paths = []
  @config = Config.new
  @offenses_total_count = 0
end

Public Instance Methods

config_file() click to toggle source
# File lib/fasterer/file_traverser.rb, line 31
def config_file
  config.file
end
offenses_found?() click to toggle source
# File lib/fasterer/file_traverser.rb, line 35
def offenses_found?
  !!offenses_found
end
scannable_files() click to toggle source
# File lib/fasterer/file_traverser.rb, line 39
def scannable_files
  all_files - ignored_files
end
traverse() click to toggle source
# File lib/fasterer/file_traverser.rb, line 25
def traverse
  traverse_files
  output_parse_errors
  output_statistics
end

Private Instance Methods

all_files() click to toggle source
# File lib/fasterer/file_traverser.rb, line 68
def all_files
  if @path.directory?
    Dir[File.join(@path, '**', '*.rb')].map do |ruby_file_path|
      Pathname(ruby_file_path).relative_path_from(root_dir).to_s
    end
  else
    [@path.to_s]
  end
end
ignored_files() click to toggle source
# File lib/fasterer/file_traverser.rb, line 122
def ignored_files
  config.ignored_files
end
ignored_speedups() click to toggle source
# File lib/fasterer/file_traverser.rb, line 118
def ignored_speedups
  config.ignored_speedups
end
nil_config_file() click to toggle source
# File lib/fasterer/file_traverser.rb, line 126
def nil_config_file
  config.nil_file
end
offenses_grouped_by_type(analyzer) click to toggle source
# File lib/fasterer/file_traverser.rb, line 93
def offenses_grouped_by_type(analyzer)
  analyzer.errors.group_by(&:name).delete_if do |offense_name, _|
    ignored_speedups.include?(offense_name)
  end
end
output(analyzer) click to toggle source
# File lib/fasterer/file_traverser.rb, line 82
def output(analyzer)
  offenses_grouped_by_type(analyzer).each do |error_group_name, error_occurences|
    error_occurences.map(&:line_number).each do |line|
      file_and_line = "#{analyzer.file_path}:#{line}"
      print "#{Painter.paint(file_and_line, :red)} #{Fasterer::Offense::EXPLANATIONS[error_group_name]}.\n"
    end
  end

  print "\n"
end
output_parse_errors() click to toggle source
# File lib/fasterer/file_traverser.rb, line 99
def output_parse_errors
  return if parse_error_paths.none?

  puts 'Fasterer was unable to process some files because the'
  puts 'internal parser is not able to read some characters or'
  puts 'has timed out. Unprocessable files were:'
  puts '-----------------------------------------------------'
  puts parse_error_paths
  puts
end
output_statistics() click to toggle source
# File lib/fasterer/file_traverser.rb, line 110
def output_statistics
  puts Statistics.new(self)
end
output_unable_to_find_file(path) click to toggle source
# File lib/fasterer/file_traverser.rb, line 114
def output_unable_to_find_file(path)
  puts Painter.paint("No such file or directory - #{path}", :red)
end
root_dir() click to toggle source
# File lib/fasterer/file_traverser.rb, line 78
def root_dir
  @root_dir ||= Pathname('.')
end
scan_file(path) click to toggle source
# File lib/fasterer/file_traverser.rb, line 55
def scan_file(path)
  analyzer = Analyzer.new(path)
  analyzer.scan
rescue RubyParser::SyntaxError, Racc::ParseError, Timeout::Error => e
  parse_error_paths.push(ErrorData.new(path, e.class, e.message).to_s)
else
  if offenses_grouped_by_type(analyzer).any?
    output(analyzer)
    self.offenses_found = true
    self.offenses_total_count += analyzer.errors.count
  end
end
traverse_files() click to toggle source
# File lib/fasterer/file_traverser.rb, line 47
def traverse_files
  if @path.exist?
    scannable_files.each { |ruby_file| scan_file(ruby_file) }
  else
    output_unable_to_find_file(@path)
  end
end