]> git.immae.eu Git - github/fretlink/pronto-hlint.git/blob - lib/pronto/eslint.rb
Respect `.eslintignore`
[github/fretlink/pronto-hlint.git] / lib / pronto / eslint.rb
1 require 'pronto'
2 require 'eslintrb'
3 require 'globby'
4
5 module Pronto
6 class ESLint < Runner
7 def run
8 return [] unless @patches
9
10 @patches.select { |patch| patch.additions > 0 }
11 .select { |patch| js_file?(patch.new_file_full_path) }
12 .map { |patch| inspect(patch) }
13 .flatten.compact
14 end
15
16 def inspect(patch)
17 options = File.exist?('.eslintrc') ? :eslintrc : :defaults
18 offences = Eslintrb.lint(patch.new_file_full_path, options).compact
19
20 offences.map do |offence|
21 patch.added_lines.select { |line| line.new_lineno == offence['line'] }
22 .map { |line| new_message(offence, line) }
23 end
24 end
25
26 def new_message(offence, line)
27 path = line.patch.delta.new_file[:path]
28 level = :warning
29
30 Message.new(path, line, level, offence['message'], nil, self.class)
31 end
32
33 def js_file?(path)
34 %w(.js .es6 .js.es6).include?(File.extname(path)) && !eslintignore_matches?(path)
35 end
36
37 def eslintignore_matches?(path)
38 @_repo_path ||= @patches.first.repo.path
39 @_eslintignore_path ||= File.join(@_repo_path, '.eslintignore')
40 @_eslintignore_exists ||= File.exist?(@_eslintignore_path)
41
42 return false unless @_eslintignore_exists
43
44 @_eslintignored_files ||=
45 Dir.chdir @_repo_path do # change to the repo path where `.eslintignore` was found
46 eslintignore_content = File.readlines(@_eslintignore_path).map(&:chomp)
47 ignored_files = Globby.select(eslintignore_content)
48
49 # prefix each found file with `repo_path`, because `path` is absolute, too
50 ignored_files.map { |file| File.join(@_repo_path, file).to_s }
51 end
52
53 @_eslintignored_files.include?(path.to_s)
54 end
55 end
56 end