]> git.immae.eu Git - github/fretlink/pronto-hlint.git/blame - lib/pronto/eslint_npm.rb
require shellwords (which might not be required by default)
[github/fretlink/pronto-hlint.git] / lib / pronto / eslint_npm.rb
CommitLineData
9be00a32 1require 'pronto'
2950a68a 2require 'shellwords'
9be00a32
MM
3
4module Pronto
5001cb27 5 class ESLintNpm < Runner
b338a7ad
MM
6 def run
7 return [] unless @patches
9be00a32 8
b338a7ad 9 @patches.select { |patch| patch.additions > 0 }
9be00a32
MM
10 .select { |patch| js_file?(patch.new_file_full_path) }
11 .map { |patch| inspect(patch) }
12 .flatten.compact
13 end
14
15 def inspect(patch)
29509625
MD
16 @_repo_path ||= @patches.first.repo.path
17
156f8167
MD
18 offences = run_eslint(patch)
19 clean_up_eslint_output(offences)
20 .map do |offence|
21 patch
22 .added_lines
23 .select { |line| line.new_lineno == offence['line'] }
24 .map { |line| new_message(offence, line) }
29509625 25 end
9be00a32
MM
26 end
27
156f8167
MD
28 private
29
9be00a32
MM
30 def new_message(offence, line)
31 path = line.patch.delta.new_file[:path]
32 level = :warning
33
b338a7ad 34 Message.new(path, line, level, offence['message'], nil, self.class)
9be00a32
MM
35 end
36
37 def js_file?(path)
29509625 38 %w(.js .es6 .js.es6).include?(File.extname(path))
9be00a32 39 end
156f8167
MD
40
41 def run_eslint(patch)
42 Dir.chdir(@_repo_path) do
43 JSON.parse(
44 `eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json`
45 )
46 end
47 end
48
49 # rubocop:disable Metrics/LineLength
50 def clean_up_eslint_output(output)
51 # 1. Filter out offences without a warning or error
52 # 2. Get the messages for that file
53 # 3. Ignore errors without a line number for now
54 output
55 .select { |offence| offence['errorCount'] + offence['warningCount'] > 0 }
56 .map { |offence| offence['messages'] }
57 .flatten.select { |offence| offence['line'] }
58 end
9be00a32
MM
59 end
60end