From fb94b0e6df4da3026b9fb4714ee7ceab6bbfa9bc Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Mon, 25 Jul 2016 14:36:23 +0200 Subject: [PATCH] Respect `.eslintignore` 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.). --- lib/pronto/eslint.rb | 22 +++++++++++++++++- pronto-eslint.gemspec | 1 + spec/fixtures/eslintignore.git/.eslintignore | 2 ++ spec/fixtures/eslintignore.git/git/HEAD | 1 + spec/fixtures/eslintignore.git/git/config | 7 ++++++ spec/fixtures/eslintignore.git/git/index | Bin 0 -> 353 bytes spec/fixtures/eslintignore.git/git/logs/HEAD | 3 +++ .../git/logs/refs/heads/eslintignore | 2 ++ .../git/logs/refs/heads/master | 1 + .../2d/1b9966feb19d665a9e5bb6a5bde410203a68f7 | Bin 0 -> 73 bytes .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../5f/fc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 | Bin 0 -> 185 bytes .../69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 | Bin 0 -> 88 bytes .../d4/3de0396c81608113f99f7afd00f7f363e1cae8 | 2 ++ .../d8/5195b5ff74cc05d121b758f00da43018f53b6f | Bin 0 -> 42 bytes .../ec/7b4537cbd327a580ee44b74ff02fffc74feb46 | Bin 0 -> 63 bytes .../git/refs/heads/eslintignore | 1 + .../eslintignore.git/git/refs/heads/master | 1 + .../eslintignore.git/ignored/ignored.js | 5 ++++ .../eslintignore.git/ignored/not_ignored.js | 5 ++++ spec/pronto/eslint_spec.rb | 9 +++++++ spec/spec_helper.rb | 14 ++++++----- 22 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 spec/fixtures/eslintignore.git/.eslintignore create mode 100644 spec/fixtures/eslintignore.git/git/HEAD create mode 100644 spec/fixtures/eslintignore.git/git/config create mode 100644 spec/fixtures/eslintignore.git/git/index create mode 100644 spec/fixtures/eslintignore.git/git/logs/HEAD create mode 100644 spec/fixtures/eslintignore.git/git/logs/refs/heads/eslintignore create mode 100644 spec/fixtures/eslintignore.git/git/logs/refs/heads/master create mode 100644 spec/fixtures/eslintignore.git/git/objects/2d/1b9966feb19d665a9e5bb6a5bde410203a68f7 create mode 100644 spec/fixtures/eslintignore.git/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 spec/fixtures/eslintignore.git/git/objects/5f/fc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 create mode 100644 spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 create mode 100644 spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8 create mode 100644 spec/fixtures/eslintignore.git/git/objects/d8/5195b5ff74cc05d121b758f00da43018f53b6f create mode 100644 spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46 create mode 100644 spec/fixtures/eslintignore.git/git/refs/heads/eslintignore create mode 100644 spec/fixtures/eslintignore.git/git/refs/heads/master create mode 100644 spec/fixtures/eslintignore.git/ignored/ignored.js create mode 100644 spec/fixtures/eslintignore.git/ignored/not_ignored.js diff --git a/lib/pronto/eslint.rb b/lib/pronto/eslint.rb index 498df63..990081e 100644 --- a/lib/pronto/eslint.rb +++ b/lib/pronto/eslint.rb @@ -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 diff --git a/pronto-eslint.gemspec b/pronto-eslint.gemspec index 77da780..9ad00ec 100644 --- a/pronto-eslint.gemspec +++ b/pronto-eslint.gemspec @@ -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 index 0000000..d85195b --- /dev/null +++ b/spec/fixtures/eslintignore.git/.eslintignore @@ -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 index 0000000..fac958f --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/HEAD @@ -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 index 0000000..6c9406b --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/config @@ -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 index 0000000000000000000000000000000000000000..8c5575c45a900a3003f8de90de5049d489f859bb GIT binary patch literal 353 zcmZ?q402{*U|<4b=J083Wk8w%Ml&)nurMV%9$;W-T*AP>_!TH60>m0O0;g{MUvh@^ zqT=?554=kZB)(ebGw|xA7UyKa)fGNQ~CgPEI`UlNaMc1Vz`E6`j^ z24e+7uFU$sHktP4X6@NgxN}RLu3%o0<7KFF1|tOnt~b@L=BF>KFKu||vfclK{{Q3t cuiXL<7-n2Krf|Bm$hdd|`@fbwuE&l50Lqtb-2eap literal 0 HcmV?d00001 diff --git a/spec/fixtures/eslintignore.git/git/logs/HEAD b/spec/fixtures/eslintignore.git/git/logs/HEAD new file mode 100644 index 0000000..8750baa --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/logs/HEAD @@ -0,0 +1,3 @@ +0000000000000000000000000000000000000000 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits 1469450052 +0200 commit (initial): empty init commit +d43de0396c81608113f99f7afd00f7f363e1cae8 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits 1469450062 +0200 checkout: moving from master to eslintignore +d43de0396c81608113f99f7afd00f7f363e1cae8 5ffc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 Markus Doits 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 index 0000000..3467ecc --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/logs/refs/heads/eslintignore @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits 1469450062 +0200 branch: Created from HEAD +d43de0396c81608113f99f7afd00f7f363e1cae8 5ffc9b3c7f8e8d8f2e8c91cb69c9698405d1ca53 Markus Doits 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 index 0000000..8105749 --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 d43de0396c81608113f99f7afd00f7f363e1cae8 Markus Doits 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 index 0000000000000000000000000000000000000000..f5fbdc534df59a48a106d6141541e9cee9d560bf GIT binary patch literal 73 zcmV-P0Ji^l0ZYosPf{>5XGkl}OD@UG&r|S7&B@6R&o9bJ(a1~8P1WS8=28HH%rpg! fwETQc1t6iVZ4DMn%t5j#GvtFn>c#`^RWHE3w?51RMtxhQoHt)? literal 0 HcmV?d00001 diff --git a/spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 b/spec/fixtures/eslintignore.git/git/objects/69/7ffd3c693fce9abcb871b9b47e2d116e6241d3 new file mode 100644 index 0000000000000000000000000000000000000000..c23a96f6f45708f419e891b26c9acf506ed9c567 GIT binary patch literal 88 zcmV-e0H^AWiT`_Ff%bx&`T}O$;>OsOwY?NN@chaICbm)k~6Fq6}Lxx u;9X)M@zpxt!~h5sAPQ0#-c-AqpT4ZVwBeo0cK;9h|Bw5>b^`zk(js$3YA5CZ literal 0 HcmV?d00001 diff --git a/spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8 b/spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8 new file mode 100644 index 0000000..e3c829c --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/objects/d4/3de0396c81608113f99f7afd00f7f363e1cae8 @@ -0,0 +1,2 @@ +x¥K +Â0@]ç³Ê&!n=D>S mZI¦ o¯Ö#¸|›÷xi­µh´iÌ@Ñi““%¢åèZdCqtÙzíÜ™É#©°ÉcmpmÚ:ÜÖ"Îu§!éڅç™)ib2_àD֓A4Ž¨UÚëÂÿzק¼ ,Ÿ•ŸS½…ï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 index 0000000000000000000000000000000000000000..ee9657e79930394355b20b26de891cef24308244 GIT binary patch literal 42 zcmV+_0M-9^0ZYosPf{>3V8~3*%P&ez(bv+_*V4->=2CCy+ AHUIzs literal 0 HcmV?d00001 diff --git a/spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46 b/spec/fixtures/eslintignore.git/git/objects/ec/7b4537cbd327a580ee44b74ff02fffc74feb46 new file mode 100644 index 0000000000000000000000000000000000000000..9a642e0fb02cc0638938d8cba7277dc7562e9bea GIT binary patch literal 63 zcmV-F0Korv0V^p=O;xZkU@$Z=Ff%bx$V|`6FG@|(%PMBjm7baQZ{yswsCm)bmhOEb VpkS5p9jYoXza*Z3N&tLVB9CO&9PI!A literal 0 HcmV?d00001 diff --git a/spec/fixtures/eslintignore.git/git/refs/heads/eslintignore b/spec/fixtures/eslintignore.git/git/refs/heads/eslintignore new file mode 100644 index 0000000..d02562a --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/refs/heads/eslintignore @@ -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 index 0000000..5dcf161 --- /dev/null +++ b/spec/fixtures/eslintignore.git/git/refs/heads/master @@ -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 index 0000000..2d1b996 --- /dev/null +++ b/spec/fixtures/eslintignore.git/ignored/ignored.js @@ -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 index 0000000..2d1b996 --- /dev/null +++ b/spec/fixtures/eslintignore.git/ignored/not_ignored.js @@ -0,0 +1,5 @@ +function HelloWorld(name) +{ + if (foo) foo++; + alert(name); +} diff --git a/spec/pronto/eslint_spec.rb b/spec/pronto/eslint_spec.rb index 7af0aa2..1117e15 100644 --- a/spec/pronto/eslint_spec.rb +++ b/spec/pronto/eslint_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1ff71..5769c2d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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| -- 2.41.0