aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorMarkus Doits <markus.doits@stellenticket.de>2016-09-11 14:49:59 +0200
committerMarkus Doits <markus.doits@stellenticket.de>2016-09-11 15:13:42 +0200
commit1845f2e31adc6f9b1513b779f44e3bd347d6fd11 (patch)
tree41d61b6b061234f0f1ccab2f86898386a3ee6bcf /lib
parent3403f9d12247884c18ffe7a1636fe12c3fb0f0da (diff)
downloadpronto-hlint-1845f2e31adc6f9b1513b779f44e3bd347d6fd11.tar.gz
pronto-hlint-1845f2e31adc6f9b1513b779f44e3bd347d6fd11.tar.zst
pronto-hlint-1845f2e31adc6f9b1513b779f44e3bd347d6fd11.zip
Allow to set config with `.pronto_eslint_npm.yml`
Make configuration dependent on instance instead of class, too.
Diffstat (limited to 'lib')
-rw-r--r--lib/pronto/eslint_npm.rb44
1 files changed, 32 insertions, 12 deletions
diff --git a/lib/pronto/eslint_npm.rb b/lib/pronto/eslint_npm.rb
index 6bbef09..9ff7df5 100644
--- a/lib/pronto/eslint_npm.rb
+++ b/lib/pronto/eslint_npm.rb
@@ -3,20 +3,38 @@ require 'shellwords'
3 3
4module Pronto 4module Pronto
5 class ESLintNpm < Runner 5 class ESLintNpm < Runner
6 class << self 6 CONFIG_FILE = '.pronto_eslint_npm.yml'.freeze
7 attr_writer :eslint_executable, :files_to_lint 7 CONFIG_KEYS = %i(eslint_executable files_to_lint).freeze
8 8
9 def eslint_executable 9 attr_writer :eslint_executable
10 @eslint_executable || 'eslint'.freeze 10
11 end 11 def eslint_executable
12 @eslint_executable || 'eslint'.freeze
13 end
14
15 def files_to_lint
16 @files_to_lint || /(\.js|\.es6)$/
17 end
18
19 def files_to_lint=(regexp)
20 @files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp)
21 end
22
23 def read_config
24 config_file = File.join(repo_path, CONFIG_FILE)
25 return unless File.exist?(config_file)
26 config = YAML.load_file(config_file)
12 27
13 def files_to_lint 28 CONFIG_KEYS.each do |config_key|
14 @files_to_lint || /(\.js|\.es6)$/ 29 next unless config[config_key]
30 send("#{config_key}=", config[config_key])
15 end 31 end
16 end 32 end
17 33
18 def run 34 def run
19 return [] unless @patches 35 return [] if !@patches || @patches.count.zero?
36
37 read_config
20 38
21 @patches 39 @patches
22 .select { |patch| patch.additions > 0 } 40 .select { |patch| patch.additions > 0 }
@@ -27,9 +45,11 @@ module Pronto
27 45
28 private 46 private
29 47
30 def inspect(patch) 48 def repo_path
31 @_repo_path ||= @patches.first.repo.path 49 @_repo_path ||= @patches.first.repo.path
50 end
32 51
52 def inspect(patch)
33 offences = run_eslint(patch) 53 offences = run_eslint(patch)
34 clean_up_eslint_output(offences) 54 clean_up_eslint_output(offences)
35 .map do |offence| 55 .map do |offence|
@@ -48,14 +68,14 @@ module Pronto
48 end 68 end
49 69
50 def js_file?(path) 70 def js_file?(path)
51 self.class.files_to_lint =~ path.to_s 71 files_to_lint =~ path.to_s
52 end 72 end
53 73
54 def run_eslint(patch) 74 def run_eslint(patch)
55 Dir.chdir(@_repo_path) do 75 Dir.chdir(repo_path) do
56 escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s) 76 escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s)
57 JSON.parse( 77 JSON.parse(
58 `#{self.class.eslint_executable} #{escaped_file_path} -f json` 78 `#{eslint_executable} #{escaped_file_path} -f json`
59 ) 79 )
60 end 80 end
61 end 81 end