diff options
Diffstat (limited to 'lib/pronto/eslint_npm.rb')
-rw-r--r-- | lib/pronto/eslint_npm.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/pronto/eslint_npm.rb b/lib/pronto/eslint_npm.rb new file mode 100644 index 0000000..afbae59 --- /dev/null +++ b/lib/pronto/eslint_npm.rb | |||
@@ -0,0 +1,59 @@ | |||
1 | require 'pronto' | ||
2 | |||
3 | module Pronto | ||
4 | class ESLintNpm < Runner | ||
5 | def run | ||
6 | return [] unless @patches | ||
7 | |||
8 | @patches.select { |patch| patch.additions > 0 } | ||
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) | ||
15 | @_repo_path ||= @patches.first.repo.path | ||
16 | |||
17 | offences = run_eslint(patch) | ||
18 | clean_up_eslint_output(offences) | ||
19 | .map do |offence| | ||
20 | patch | ||
21 | .added_lines | ||
22 | .select { |line| line.new_lineno == offence['line'] } | ||
23 | .map { |line| new_message(offence, line) } | ||
24 | end | ||
25 | end | ||
26 | |||
27 | private | ||
28 | |||
29 | def new_message(offence, line) | ||
30 | path = line.patch.delta.new_file[:path] | ||
31 | level = :warning | ||
32 | |||
33 | Message.new(path, line, level, offence['message'], nil, self.class) | ||
34 | end | ||
35 | |||
36 | def js_file?(path) | ||
37 | %w(.js .es6 .js.es6).include?(File.extname(path)) | ||
38 | end | ||
39 | |||
40 | def run_eslint(patch) | ||
41 | Dir.chdir(@_repo_path) do | ||
42 | JSON.parse( | ||
43 | `eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json` | ||
44 | ) | ||
45 | end | ||
46 | end | ||
47 | |||
48 | # rubocop:disable Metrics/LineLength | ||
49 | def clean_up_eslint_output(output) | ||
50 | # 1. Filter out offences without a warning or error | ||
51 | # 2. Get the messages for that file | ||
52 | # 3. Ignore errors without a line number for now | ||
53 | output | ||
54 | .select { |offence| offence['errorCount'] + offence['warningCount'] > 0 } | ||
55 | .map { |offence| offence['messages'] } | ||
56 | .flatten.select { |offence| offence['line'] } | ||
57 | end | ||
58 | end | ||
59 | end | ||