diff options
-rw-r--r-- | lib/pronto/eslint-npm.rb | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/lib/pronto/eslint-npm.rb b/lib/pronto/eslint-npm.rb index a3219eb..afbae59 100644 --- a/lib/pronto/eslint-npm.rb +++ b/lib/pronto/eslint-npm.rb | |||
@@ -14,24 +14,18 @@ module Pronto | |||
14 | def inspect(patch) | 14 | def inspect(patch) |
15 | @_repo_path ||= @patches.first.repo.path | 15 | @_repo_path ||= @patches.first.repo.path |
16 | 16 | ||
17 | offences = | 17 | offences = run_eslint(patch) |
18 | Dir.chdir(@_repo_path) do | 18 | clean_up_eslint_output(offences) |
19 | JSON.parse(`eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json`) | 19 | .map do |offence| |
20 | patch | ||
21 | .added_lines | ||
22 | .select { |line| line.new_lineno == offence['line'] } | ||
23 | .map { |line| new_message(offence, line) } | ||
20 | end | 24 | 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 | ||
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 | 25 | end |
34 | 26 | ||
27 | private | ||
28 | |||
35 | def new_message(offence, line) | 29 | def new_message(offence, line) |
36 | path = line.patch.delta.new_file[:path] | 30 | path = line.patch.delta.new_file[:path] |
37 | level = :warning | 31 | level = :warning |
@@ -42,5 +36,24 @@ module Pronto | |||
42 | def js_file?(path) | 36 | def js_file?(path) |
43 | %w(.js .es6 .js.es6).include?(File.extname(path)) | 37 | %w(.js .es6 .js.es6).include?(File.extname(path)) |
44 | end | 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 | ||
45 | end | 58 | end |
46 | end | 59 | end |