]> git.immae.eu Git - github/fretlink/pronto-hlint.git/blame - lib/pronto/eslint_npm.rb
correct name according to recommended use of `_` and `-`
[github/fretlink/pronto-hlint.git] / lib / pronto / eslint_npm.rb
CommitLineData
9be00a32 1require 'pronto'
9be00a32
MM
2
3module 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
156f8167
MD
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) }
29509625 24 end
9be00a32
MM
25 end
26
156f8167
MD
27 private
28
9be00a32
MM
29 def new_message(offence, line)
30 path = line.patch.delta.new_file[:path]
31 level = :warning
32
b338a7ad 33 Message.new(path, line, level, offence['message'], nil, self.class)
9be00a32
MM
34 end
35
36 def js_file?(path)
29509625 37 %w(.js .es6 .js.es6).include?(File.extname(path))
9be00a32 38 end
156f8167
MD
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
9be00a32
MM
58 end
59end