aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarkus Doits <markus.doits@stellenticket.de>2016-09-11 14:07:42 +0200
committerMarkus Doits <markus.doits@stellenticket.de>2016-09-11 14:07:42 +0200
commit3403f9d12247884c18ffe7a1636fe12c3fb0f0da (patch)
treea343b6b25b22553489e95c3ffc93e5b9502f03b9
parent7c0daa97e07ca409d1ee9f394adb4967196c6ca1 (diff)
downloadpronto-hlint-3403f9d12247884c18ffe7a1636fe12c3fb0f0da.tar.gz
pronto-hlint-3403f9d12247884c18ffe7a1636fe12c3fb0f0da.tar.zst
pronto-hlint-3403f9d12247884c18ffe7a1636fe12c3fb0f0da.zip
add class variables to prepare setting external config
-rw-r--r--lib/pronto/eslint_npm.rb25
-rw-r--r--spec/pronto/eslint_spec.rb20
2 files changed, 39 insertions, 6 deletions
diff --git a/lib/pronto/eslint_npm.rb b/lib/pronto/eslint_npm.rb
index 4f32e5b..6bbef09 100644
--- a/lib/pronto/eslint_npm.rb
+++ b/lib/pronto/eslint_npm.rb
@@ -3,15 +3,30 @@ require 'shellwords'
3 3
4module Pronto 4module Pronto
5 class ESLintNpm < Runner 5 class ESLintNpm < Runner
6 class << self
7 attr_writer :eslint_executable, :files_to_lint
8
9 def eslint_executable
10 @eslint_executable || 'eslint'.freeze
11 end
12
13 def files_to_lint
14 @files_to_lint || /(\.js|\.es6)$/
15 end
16 end
17
6 def run 18 def run
7 return [] unless @patches 19 return [] unless @patches
8 20
9 @patches.select { |patch| patch.additions > 0 } 21 @patches
22 .select { |patch| patch.additions > 0 }
10 .select { |patch| js_file?(patch.new_file_full_path) } 23 .select { |patch| js_file?(patch.new_file_full_path) }
11 .map { |patch| inspect(patch) } 24 .map { |patch| inspect(patch) }
12 .flatten.compact 25 .flatten.compact
13 end 26 end
14 27
28 private
29
15 def inspect(patch) 30 def inspect(patch)
16 @_repo_path ||= @patches.first.repo.path 31 @_repo_path ||= @patches.first.repo.path
17 32
@@ -25,8 +40,6 @@ module Pronto
25 end 40 end
26 end 41 end
27 42
28 private
29
30 def new_message(offence, line) 43 def new_message(offence, line)
31 path = line.patch.delta.new_file[:path] 44 path = line.patch.delta.new_file[:path]
32 level = :warning 45 level = :warning
@@ -35,18 +48,18 @@ module Pronto
35 end 48 end
36 49
37 def js_file?(path) 50 def js_file?(path)
38 %w(.js .es6 .js.es6).include?(File.extname(path)) 51 self.class.files_to_lint =~ path.to_s
39 end 52 end
40 53
41 def run_eslint(patch) 54 def run_eslint(patch)
42 Dir.chdir(@_repo_path) do 55 Dir.chdir(@_repo_path) do
56 escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s)
43 JSON.parse( 57 JSON.parse(
44 `eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json` 58 `#{self.class.eslint_executable} #{escaped_file_path} -f json`
45 ) 59 )
46 end 60 end
47 end 61 end
48 62
49 # rubocop:disable Metrics/LineLength
50 def clean_up_eslint_output(output) 63 def clean_up_eslint_output(output)
51 # 1. Filter out offences without a warning or error 64 # 1. Filter out offences without a warning or error
52 # 2. Get the messages for that file 65 # 2. Get the messages for that file
diff --git a/spec/pronto/eslint_spec.rb b/spec/pronto/eslint_spec.rb
index 2065998..7bbc77a 100644
--- a/spec/pronto/eslint_spec.rb
+++ b/spec/pronto/eslint_spec.rb
@@ -51,5 +51,25 @@ module Pronto
51 end 51 end
52 end 52 end
53 end 53 end
54
55 describe '.files_to_lint' do
56 subject(:files_to_lint) { ESLintNpm.files_to_lint }
57
58 it 'matches .js by default' do
59 expect(files_to_lint).to match('my_js.js')
60 end
61
62 it 'matches .es6 by default' do
63 expect(files_to_lint).to match('my_js.es6')
64 end
65 end
66
67 describe '.eslint_executable' do
68 subject(:eslint_executable) { ESLintNpm.eslint_executable }
69
70 it 'is `eslint` by default' do
71 expect(eslint_executable).to eql('eslint')
72 end
73 end
54 end 74 end
55end 75end