diff options
Diffstat (limited to 'lib/pronto/eslint-npm.rb')
-rw-r--r-- | lib/pronto/eslint-npm.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/pronto/eslint-npm.rb b/lib/pronto/eslint-npm.rb new file mode 100644 index 0000000..a3219eb --- /dev/null +++ b/lib/pronto/eslint-npm.rb | |||
@@ -0,0 +1,46 @@ | |||
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 = | ||
18 | Dir.chdir(@_repo_path) do | ||
19 | JSON.parse(`eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json`) | ||
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 | ||
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 | |||
39 | Message.new(path, line, level, offence['message'], nil, self.class) | ||
40 | end | ||
41 | |||
42 | def js_file?(path) | ||
43 | %w(.js .es6 .js.es6).include?(File.extname(path)) | ||
44 | end | ||
45 | end | ||
46 | end | ||