-
Notifications
You must be signed in to change notification settings - Fork 58
/
runner.rb
83 lines (73 loc) · 2.8 KB
/
runner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true
module SlimLint
# Responsible for running the applicable linters against the desired files.
class Runner
# Runs the appropriate linters against the desired files given the specified
# options.
#
# @param [Hash] options
# @option options :config_file [String] path of configuration file to load
# @option options :config [SlimLint::Configuration] configuration to use
# @option options :excluded_files [Array<String>]
# @option options :included_linters [Array<String>]
# @option options :excluded_linters [Array<String>]
# @return [SlimLint::Report] a summary of all lints found
def run(options = {})
config = load_applicable_config(options)
files = extract_applicable_files(config, options)
linter_selector = SlimLint::LinterSelector.new(config, options)
lints =
if options[:stdin_file_path].nil?
files.map do |file|
collect_lints(File.read(file), file, linter_selector, config)
end.flatten
else
collect_lints($stdin.read, options[:stdin_file_path], linter_selector, config)
end
SlimLint::Report.new(lints, files)
end
private
# Returns the {SlimLint::Configuration} that should be used given the
# specified options.
#
# @param options [Hash]
# @return [SlimLint::Configuration]
def load_applicable_config(options)
if options[:config_file]
SlimLint::ConfigurationLoader.load_file(options[:config_file])
elsif options[:config]
options[:config]
else
SlimLint::ConfigurationLoader.load_applicable_config
end
end
# Runs all provided linters using the specified config against the given
# file.
#
# @param file [String] path to file to lint
# @param linter_selector [SlimLint::LinterSelector]
# @param config [SlimLint::Configuration]
def collect_lints(file_content, file_name, linter_selector, config)
begin
document = SlimLint::Document.new(file_content, file: file_name, config: config)
rescue SlimLint::Exceptions::ParseError => e
return [SlimLint::Lint.new(nil, file, e.lineno, e.error, :error)]
end
linter_selector.linters_for_file(file_name).map do |linter|
linter.run(document)
end.flatten
end
# Returns the list of files that should be linted given the specified
# configuration and options.
#
# @param config [SlimLint::Configuration]
# @param options [Hash]
# @return [Array<String>]
def extract_applicable_files(config, options)
included_patterns = options[:files]
excluded_patterns = config['exclude']
excluded_patterns += options.fetch(:excluded_files, [])
SlimLint::FileFinder.new(config).find(included_patterns, excluded_patterns)
end
end
end