aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lib/pronto/eslint.rb38
-rw-r--r--pronto-eslint.gemspec2
-rw-r--r--spec/fixtures/eslintignore.git/.eslintrc7
-rw-r--r--spec/fixtures/test.git/.eslintrc7
-rw-r--r--spec/fixtures/test.git/hello.js2
-rw-r--r--spec/pronto/eslint_spec.rb12
6 files changed, 36 insertions, 32 deletions
diff --git a/lib/pronto/eslint.rb b/lib/pronto/eslint.rb
index 990081e..fa4261d 100644
--- a/lib/pronto/eslint.rb
+++ b/lib/pronto/eslint.rb
@@ -1,6 +1,4 @@
1require 'pronto' 1require 'pronto'
2require 'eslintrb'
3require 'globby'
4 2
5module Pronto 3module Pronto
6 class ESLint < Runner 4 class ESLint < Runner
@@ -14,8 +12,19 @@ module Pronto
14 end 12 end
15 13
16 def inspect(patch) 14 def inspect(patch)
17 options = File.exist?('.eslintrc') ? :eslintrc : :defaults 15 @_repo_path ||= @patches.first.repo.path
18 offences = Eslintrb.lint(patch.new_file_full_path, options).compact 16
17 offences =
18 Dir.chdir(@_repo_path) do
19 JSON.parse(`eslint #{patch.new_file_full_path} -f json`)
20 end
21
22 offences =
23 offences
24 .select { |offence| offence['errorCount'] > 0 || offence['warningCount'] > 0 } # no warning or error, no problem
25 .map { |offence| offence['messages'] } # get error messages for that file
26 .flatten
27 .select { |offence| offence['line'] } # for now ignore errors without a line number
19 28
20 offences.map do |offence| 29 offences.map do |offence|
21 patch.added_lines.select { |line| line.new_lineno == offence['line'] } 30 patch.added_lines.select { |line| line.new_lineno == offence['line'] }
@@ -31,26 +40,7 @@ module Pronto
31 end 40 end
32 41
33 def js_file?(path) 42 def js_file?(path)
34 %w(.js .es6 .js.es6).include?(File.extname(path)) && !eslintignore_matches?(path) 43 %w(.js .es6 .js.es6).include?(File.extname(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 44 end
55 end 45 end
56end 46end
diff --git a/pronto-eslint.gemspec b/pronto-eslint.gemspec
index 9ad00ec..d6590ec 100644
--- a/pronto-eslint.gemspec
+++ b/pronto-eslint.gemspec
@@ -35,8 +35,6 @@ Gem::Specification.new do |s|
35 s.require_paths = ['lib'] 35 s.require_paths = ['lib']
36 36
37 s.add_dependency('pronto', '~> 0.6.0') 37 s.add_dependency('pronto', '~> 0.6.0')
38 s.add_dependency('eslintrb', '~> 2.0', '>= 2.0.0')
39 s.add_dependency('globby', '~> 0.1')
40 s.add_development_dependency('rake', '~> 11.0') 38 s.add_development_dependency('rake', '~> 11.0')
41 s.add_development_dependency('rspec', '~> 3.4') 39 s.add_development_dependency('rspec', '~> 3.4')
42 s.add_development_dependency('rspec-its', '~> 1.2') 40 s.add_development_dependency('rspec-its', '~> 1.2')
diff --git a/spec/fixtures/eslintignore.git/.eslintrc b/spec/fixtures/eslintignore.git/.eslintrc
new file mode 100644
index 0000000..f94ba96
--- /dev/null
+++ b/spec/fixtures/eslintignore.git/.eslintrc
@@ -0,0 +1,7 @@
1{
2 "env": {
3 "browser": true,
4 "es6": true
5 },
6 "extends": "eslint:recommended"
7}
diff --git a/spec/fixtures/test.git/.eslintrc b/spec/fixtures/test.git/.eslintrc
new file mode 100644
index 0000000..f94ba96
--- /dev/null
+++ b/spec/fixtures/test.git/.eslintrc
@@ -0,0 +1,7 @@
1{
2 "env": {
3 "browser": true,
4 "es6": true
5 },
6 "extends": "eslint:recommended"
7}
diff --git a/spec/fixtures/test.git/hello.js b/spec/fixtures/test.git/hello.js
index 2d1b996..6ad633d 100644
--- a/spec/fixtures/test.git/hello.js
+++ b/spec/fixtures/test.git/hello.js
@@ -3,3 +3,5 @@ function HelloWorld(name)
3 if (foo) foo++; 3 if (foo) foo++;
4 alert(name); 4 alert(name);
5} 5}
6
7function Empty() {}
diff --git a/spec/pronto/eslint_spec.rb b/spec/pronto/eslint_spec.rb
index 1117e15..27f57d1 100644
--- a/spec/pronto/eslint_spec.rb
+++ b/spec/pronto/eslint_spec.rb
@@ -17,22 +17,22 @@ module Pronto
17 it { should == [] } 17 it { should == [] }
18 end 18 end
19 19
20 context 'patches with a four and a five warnings' do 20 context 'patches with a one and a four warnings' do
21 include_context 'test repo' 21 include_context 'test repo'
22 22
23 let(:patches) { repo.diff('master') } 23 let(:patches) { repo.diff('master') }
24 24
25 its(:count) { should == 9 } 25 its(:count) { should == 5 }
26 its(:'first.msg') { should == "Expected { after 'if' condition." } 26 its(:'first.msg') { should == "'foo' is not defined." }
27 end 27 end
28 28
29 context 'repo with ignored and not ignored file, each with five warnings' do 29 context 'repo with ignored and not ignored file, each with three warnings' do
30 include_context 'eslintignore repo' 30 include_context 'eslintignore repo'
31 31
32 let(:patches) { repo.diff('master') } 32 let(:patches) { repo.diff('master') }
33 33
34 its(:count) { should == 5 } 34 its(:count) { should == 3 }
35 its(:'first.msg') { should == "Use the function form of 'use strict'." } 35 its(:'first.msg') { should == "'HelloWorld' is defined but never used" }
36 end 36 end
37 end 37 end
38 end 38 end