]>
Commit | Line | Data |
---|---|---|
0ab4d181 MD |
1 | # frozen_string_literal: true |
2 | ||
9be00a32 | 3 | require 'pronto' |
2950a68a | 4 | require 'shellwords' |
9be00a32 MM |
5 | |
6 | module Pronto | |
5001cb27 | 7 | class ESLintNpm < Runner |
1845f2e3 | 8 | CONFIG_FILE = '.pronto_eslint_npm.yml'.freeze |
0ab4d181 | 9 | CONFIG_KEYS = %w[eslint_executable files_to_lint].freeze |
3403f9d1 | 10 | |
1845f2e3 MD |
11 | attr_writer :eslint_executable |
12 | ||
13 | def eslint_executable | |
0ab4d181 | 14 | @eslint_executable || 'eslint' |
1845f2e3 MD |
15 | end |
16 | ||
17 | def files_to_lint | |
18 | @files_to_lint || /(\.js|\.es6)$/ | |
19 | end | |
20 | ||
21 | def files_to_lint=(regexp) | |
22 | @files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp) | |
23 | end | |
24 | ||
25 | def read_config | |
26 | config_file = File.join(repo_path, CONFIG_FILE) | |
27 | return unless File.exist?(config_file) | |
28 | config = YAML.load_file(config_file) | |
3403f9d1 | 29 | |
1845f2e3 MD |
30 | CONFIG_KEYS.each do |config_key| |
31 | next unless config[config_key] | |
32 | send("#{config_key}=", config[config_key]) | |
3403f9d1 MD |
33 | end |
34 | end | |
35 | ||
b338a7ad | 36 | def run |
1845f2e3 MD |
37 | return [] if !@patches || @patches.count.zero? |
38 | ||
39 | read_config | |
9be00a32 | 40 | |
3403f9d1 MD |
41 | @patches |
42 | .select { |patch| patch.additions > 0 } | |
9be00a32 MM |
43 | .select { |patch| js_file?(patch.new_file_full_path) } |
44 | .map { |patch| inspect(patch) } | |
45 | .flatten.compact | |
46 | end | |
47 | ||
3403f9d1 MD |
48 | private |
49 | ||
1845f2e3 | 50 | def repo_path |
29509625 | 51 | @_repo_path ||= @patches.first.repo.path |
1845f2e3 | 52 | end |
29509625 | 53 | |
1845f2e3 | 54 | def inspect(patch) |
156f8167 MD |
55 | offences = run_eslint(patch) |
56 | clean_up_eslint_output(offences) | |
57 | .map do |offence| | |
58 | patch | |
59 | .added_lines | |
60 | .select { |line| line.new_lineno == offence['line'] } | |
61 | .map { |line| new_message(offence, line) } | |
29509625 | 62 | end |
9be00a32 MM |
63 | end |
64 | ||
65 | def new_message(offence, line) | |
0ab4d181 | 66 | path = line.patch.delta.new_file[:path] |
9be00a32 MM |
67 | level = :warning |
68 | ||
b338a7ad | 69 | Message.new(path, line, level, offence['message'], nil, self.class) |
9be00a32 MM |
70 | end |
71 | ||
72 | def js_file?(path) | |
1845f2e3 | 73 | files_to_lint =~ path.to_s |
9be00a32 | 74 | end |
156f8167 MD |
75 | |
76 | def run_eslint(patch) | |
1845f2e3 | 77 | Dir.chdir(repo_path) do |
3403f9d1 | 78 | escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s) |
156f8167 | 79 | JSON.parse( |
1845f2e3 | 80 | `#{eslint_executable} #{escaped_file_path} -f json` |
156f8167 MD |
81 | ) |
82 | end | |
83 | end | |
84 | ||
156f8167 MD |
85 | def clean_up_eslint_output(output) |
86 | # 1. Filter out offences without a warning or error | |
87 | # 2. Get the messages for that file | |
88 | # 3. Ignore errors without a line number for now | |
89 | output | |
90 | .select { |offence| offence['errorCount'] + offence['warningCount'] > 0 } | |
91 | .map { |offence| offence['messages'] } | |
92 | .flatten.select { |offence| offence['line'] } | |
93 | end | |
9be00a32 MM |
94 | end |
95 | end |