]>
Commit | Line | Data |
---|---|---|
9be00a32 | 1 | require 'pronto' |
9be00a32 MM |
2 | |
3 | module Pronto | |
5001cb27 | 4 | class ESLintNpm < Runner |
b338a7ad MM |
5 | def run |
6 | return [] unless @patches | |
9be00a32 | 7 | |
b338a7ad | 8 | @patches.select { |patch| patch.additions > 0 } |
9be00a32 MM |
9 | .select { |patch| js_file?(patch.new_file_full_path) } |
10 | .map { |patch| inspect(patch) } | |
11 | .flatten.compact | |
12 | end | |
13 | ||
14 | def inspect(patch) | |
29509625 MD |
15 | @_repo_path ||= @patches.first.repo.path |
16 | ||
17 | offences = | |
18 | Dir.chdir(@_repo_path) do | |
9e25e804 | 19 | JSON.parse(`eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json`) |
29509625 MD |
20 | end |
21 | ||
22 | offences = | |
23 | offences | |
24 | .select { |offence| offence['errorCount'] > 0 || offence['warningCount'] > 0 } # no warning or error, no problem | |
25 | .map { |offence| offence['messages'] } # get error messages for that file | |
26 | .flatten | |
27 | .select { |offence| offence['line'] } # for now ignore errors without a line number | |
9be00a32 MM |
28 | |
29 | offences.map do |offence| | |
30 | patch.added_lines.select { |line| line.new_lineno == offence['line'] } | |
31 | .map { |line| new_message(offence, line) } | |
32 | end | |
33 | end | |
34 | ||
35 | def new_message(offence, line) | |
36 | path = line.patch.delta.new_file[:path] | |
37 | level = :warning | |
38 | ||
b338a7ad | 39 | Message.new(path, line, level, offence['message'], nil, self.class) |
9be00a32 MM |
40 | end |
41 | ||
42 | def js_file?(path) | |
29509625 | 43 | %w(.js .es6 .js.es6).include?(File.extname(path)) |
9be00a32 MM |
44 | end |
45 | end | |
46 | end |