]> git.immae.eu Git - github/fretlink/pronto-hlint.git/commitdiff
Respect `.eslintignore`
authorMarkus Doits <markus.doits@stellenticket.de>
Mon, 25 Jul 2016 12:36:23 +0000 (14:36 +0200)
committerMarkus Doits <markus.doits@stellenticket.de>
Mon, 25 Jul 2016 12:51:26 +0000 (14:51 +0200)
If an `.eslintignore` is present, it's patterns are now respected.
See http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories
for more info.

Due to the file format of `.eslintignore`, the gem `globby` has to be
used for those patterns (which respects negations etc.).

22 files changed:
lib/pronto/eslint.rb
pronto-eslint.gemspec
spec/fixtures/eslintignore.git/.eslintignore [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/HEAD [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/config [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/index [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/logs/HEAD [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/logs/refs/heads/eslintignore [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/logs/refs/heads/master [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/2d/1b9966feb19d665a9e5bb6a5bde410203a68f7 [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/5f/fc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8 [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/d8/5195b5ff74cc05d121b758f00da43018f53b6f [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46 [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/refs/heads/eslintignore [new file with mode: 0644]
spec/fixtures/eslintignore.git/git/refs/heads/master [new file with mode: 0644]
spec/fixtures/eslintignore.git/ignored/ignored.js [new file with mode: 0644]
spec/fixtures/eslintignore.git/ignored/not_ignored.js [new file with mode: 0644]
spec/pronto/eslint_spec.rb
spec/spec_helper.rb

index 498df63733f474bb26a107e256b60921bf0362bb..990081e5c2d72785623b2051efb6eb9e138d8a95 100644 (file)
@@ -1,5 +1,6 @@
 require 'pronto'
 require 'eslintrb'
+require 'globby'
 
 module Pronto
   class ESLint < Runner
@@ -30,7 +31,26 @@ module Pronto
     end
 
     def js_file?(path)
-      %w(.js .es6 .js.es6).include? File.extname(path)
+      %w(.js .es6 .js.es6).include?(File.extname(path)) && !eslintignore_matches?(path)
+    end
+
+    def eslintignore_matches?(path)
+      @_repo_path ||= @patches.first.repo.path
+      @_eslintignore_path ||= File.join(@_repo_path, '.eslintignore')
+      @_eslintignore_exists ||= File.exist?(@_eslintignore_path)
+
+      return false unless @_eslintignore_exists
+
+      @_eslintignored_files ||=
+        Dir.chdir @_repo_path do # change to the repo path where `.eslintignore` was found
+          eslintignore_content = File.readlines(@_eslintignore_path).map(&:chomp)
+          ignored_files = Globby.select(eslintignore_content)
+
+          # prefix each found file with `repo_path`, because `path` is absolute, too
+          ignored_files.map { |file| File.join(@_repo_path, file).to_s }
+        end
+
+      @_eslintignored_files.include?(path.to_s)
     end
   end
 end
index 77da780974bffeecde7d96d395c667287b88aebd..9ad00ecf31c586af9f1938cb38514415215d4168 100644 (file)
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
 
   s.add_dependency('pronto', '~> 0.6.0')
   s.add_dependency('eslintrb', '~> 2.0', '>= 2.0.0')
+  s.add_dependency('globby', '~> 0.1')
   s.add_development_dependency('rake', '~> 11.0')
   s.add_development_dependency('rspec', '~> 3.4')
   s.add_development_dependency('rspec-its', '~> 1.2')
diff --git a/spec/fixtures/eslintignore.git/.eslintignore b/spec/fixtures/eslintignore.git/.eslintignore
new file mode 100644 (file)
index 0000000..d85195b
--- /dev/null
@@ -0,0 +1,2 @@
+ignored/**/*.js
+!ignored/not_ignored.js
diff --git a/spec/fixtures/eslintignore.git/git/HEAD b/spec/fixtures/eslintignore.git/git/HEAD
new file mode 100644 (file)
index 0000000..fac958f
--- /dev/null
@@ -0,0 +1 @@
+ref: refs/heads/eslintignore
diff --git a/spec/fixtures/eslintignore.git/git/config b/spec/fixtures/eslintignore.git/git/config
new file mode 100644 (file)
index 0000000..6c9406b
--- /dev/null
@@ -0,0 +1,7 @@
+[core]
+       repositoryformatversion = 0
+       filemode = true
+       bare = false
+       logallrefupdates = true
+       ignorecase = true
+       precomposeunicode = true
diff --git a/spec/fixtures/eslintignore.git/git/index b/spec/fixtures/eslintignore.git/git/index
new file mode 100644 (file)
index 0000000..8c5575c
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/index differ
diff --git a/spec/fixtures/eslintignore.git/git/logs/HEAD b/spec/fixtures/eslintignore.git/git/logs/HEAD
new file mode 100644 (file)
index 0000000..8750baa
--- /dev/null
@@ -0,0 +1,3 @@
+0000000000000000000000000000000000000000 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits <markus.doits@stellenticket.de> 1469450052 +0200        commit (initial): empty init commit
+d43de0396c81608113f99f7afd00f7f363e1cae8 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits <markus.doits@stellenticket.de> 1469450062 +0200        checkout: moving from master to eslintignore
+d43de0396c81608113f99f7afd00f7f363e1cae8 5ffc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 Markus Doits <markus.doits@stellenticket.de> 1469450067 +0200        commit: add .eslintignore and files with errors
diff --git a/spec/fixtures/eslintignore.git/git/logs/refs/heads/eslintignore b/spec/fixtures/eslintignore.git/git/logs/refs/heads/eslintignore
new file mode 100644 (file)
index 0000000..3467ecc
--- /dev/null
@@ -0,0 +1,2 @@
+0000000000000000000000000000000000000000 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits <markus.doits@stellenticket.de> 1469450062 +0200        branch: Created from HEAD
+d43de0396c81608113f99f7afd00f7f363e1cae8 5ffc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 Markus Doits <markus.doits@stellenticket.de> 1469450067 +0200        commit: add .eslintignore and files with errors
diff --git a/spec/fixtures/eslintignore.git/git/logs/refs/heads/master b/spec/fixtures/eslintignore.git/git/logs/refs/heads/master
new file mode 100644 (file)
index 0000000..8105749
--- /dev/null
@@ -0,0 +1 @@
+0000000000000000000000000000000000000000 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits <markus.doits@stellenticket.de> 1469450052 +0200        commit (initial): empty init commit
diff --git a/spec/fixtures/eslintignore.git/git/objects/2d/1b9966feb19d665a9e5bb6a5bde410203a68f7 b/spec/fixtures/eslintignore.git/git/objects/2d/1b9966feb19d665a9e5bb6a5bde410203a68f7
new file mode 100644 (file)
index 0000000..f5fbdc5
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/objects/2d/1b9966feb19d665a9e5bb6a5bde410203a68f7 differ
diff --git a/spec/fixtures/eslintignore.git/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/spec/fixtures/eslintignore.git/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904
new file mode 100644 (file)
index 0000000..adf6411
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 differ
diff --git a/spec/fixtures/eslintignore.git/git/objects/5f/fc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 b/spec/fixtures/eslintignore.git/git/objects/5f/fc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53
new file mode 100644 (file)
index 0000000..832698f
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/objects/5f/fc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 differ
diff --git a/spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 b/spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3
new file mode 100644 (file)
index 0000000..c23a96f
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 differ
diff --git a/spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8 b/spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8
new file mode 100644 (file)
index 0000000..e3c829c
--- /dev/null
@@ -0,0 +1,2 @@
+x\ 1¥\8fK
+Â0\10@]ç\14³\17Ê\18&!\ 1\11\17n=D>S\fmZI¦\vo¯Ö#¸|\9b÷xi­µ\b\aiÌ@Ñi\93\93%\9d¢åè\ 3ZdCqtÙzíÜ\18\99É#©°Écmp\ fmÚ:ÜÖ"\1dÎu§!\7féÚ\85ç\99\17)ib\192_àDÖ\93A4\1a\8e¨\11UÚëÂÿz\14ק¼ ,\9f\95\9f\ 1\85ïEµ
\ No newline at end of file
diff --git a/spec/fixtures/eslintignore.git/git/objects/d8/5195b5ff74cc05d121b758f00da43018f53b6f b/spec/fixtures/eslintignore.git/git/objects/d8/5195b5ff74cc05d121b758f00da43018f53b6f
new file mode 100644 (file)
index 0000000..ee9657e
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/objects/d8/5195b5ff74cc05d121b758f00da43018f53b6f differ
diff --git a/spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46 b/spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46
new file mode 100644 (file)
index 0000000..9a642e0
Binary files /dev/null and b/spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46 differ
diff --git a/spec/fixtures/eslintignore.git/git/refs/heads/eslintignore b/spec/fixtures/eslintignore.git/git/refs/heads/eslintignore
new file mode 100644 (file)
index 0000000..d02562a
--- /dev/null
@@ -0,0 +1 @@
+5ffc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53
diff --git a/spec/fixtures/eslintignore.git/git/refs/heads/master b/spec/fixtures/eslintignore.git/git/refs/heads/master
new file mode 100644 (file)
index 0000000..5dcf161
--- /dev/null
@@ -0,0 +1 @@
+d43de0396c81608113f99f7afd00f7f363e1cae8
diff --git a/spec/fixtures/eslintignore.git/ignored/ignored.js b/spec/fixtures/eslintignore.git/ignored/ignored.js
new file mode 100644 (file)
index 0000000..2d1b996
--- /dev/null
@@ -0,0 +1,5 @@
+function HelloWorld(name)
+{
+    if (foo) foo++;
+    alert(name);
+}
diff --git a/spec/fixtures/eslintignore.git/ignored/not_ignored.js b/spec/fixtures/eslintignore.git/ignored/not_ignored.js
new file mode 100644 (file)
index 0000000..2d1b996
--- /dev/null
@@ -0,0 +1,5 @@
+function HelloWorld(name)
+{
+    if (foo) foo++;
+    alert(name);
+}
index 7af0aa25f20030a5ed97e7e0da8812f74bfd208b..1117e15f1ed4372a9f565e7efd61e0e22ab1cc11 100644 (file)
@@ -25,6 +25,15 @@ module Pronto
         its(:count) { should == 9 }
         its(:'first.msg') { should == "Expected { after 'if' condition." }
       end
+
+      context 'repo with ignored and not ignored file, each with five warnings' do
+        include_context 'eslintignore repo'
+
+        let(:patches) { repo.diff('master') }
+
+        its(:count) { should == 5 }
+        its(:'first.msg') { should == "Use the function form of 'use strict'." }
+      end
     end
   end
 end
index 4d1ff71e844417d065f1b5dc2d75e1c8d3f01555..5769c2d4a29159c9c5f84ff18c3392e419058ec4 100644 (file)
@@ -2,13 +2,15 @@ require 'rspec'
 require 'rspec/its'
 require 'pronto/eslint'
 
-RSpec.shared_context 'test repo' do
-  let(:git) { 'spec/fixtures/test.git/git' }
-  let(:dot_git) { 'spec/fixtures/test.git/.git' }
+%w(test eslintignore).each do |repo_name|
+  RSpec.shared_context "#{repo_name} repo" do
+    let(:git) { "spec/fixtures/#{repo_name}.git/git" }
+    let(:dot_git) { "spec/fixtures/#{repo_name}.git/.git" }
 
-  before { FileUtils.mv(git, dot_git) }
-  let(:repo) { Pronto::Git::Repository.new('spec/fixtures/test.git') }
-  after { FileUtils.mv(dot_git, git) }
+    before { FileUtils.mv(git, dot_git) }
+    let(:repo) { Pronto::Git::Repository.new("spec/fixtures/#{repo_name}.git") }
+    after { FileUtils.mv(dot_git, git) }
+  end
 end
 
 RSpec.configure do |config|