diff options
-rw-r--r-- | README.md | 23 | ||||
-rw-r--r-- | lib/pronto/eslint_npm.rb | 44 | ||||
-rw-r--r-- | pronto-eslint_npm.gemspec | 1 | ||||
-rwxr-xr-x | spec/fixtures/test.git/custom_eslint.sh | 1 | ||||
-rw-r--r-- | spec/pronto/eslint_spec.rb | 36 | ||||
-rw-r--r-- | spec/spec_helper.rb | 10 |
6 files changed, 96 insertions, 19 deletions
@@ -17,9 +17,30 @@ You'll need to install [eslint by yourself with npm][eslint-install]. | |||
17 | 17 | ||
18 | [eslint-install]: http://eslint.org/docs/user-guide/getting-started | 18 | [eslint-install]: http://eslint.org/docs/user-guide/getting-started |
19 | 19 | ||
20 | ## Configuration | 20 | ## Configuration of ESLint |
21 | 21 | ||
22 | Configuring ESLint via [.eslintrc and consorts][eslintrc] and excludes via [.eslintignore][eslintignore] will work just fine with pronto-eslint. | 22 | Configuring ESLint via [.eslintrc and consorts][eslintrc] and excludes via [.eslintignore][eslintignore] will work just fine with pronto-eslint. |
23 | 23 | ||
24 | [eslintrc]: http://eslint.org/docs/user-guide/configuring#configuration-file-formats | 24 | [eslintrc]: http://eslint.org/docs/user-guide/configuring#configuration-file-formats |
25 | |||
25 | [eslintignore]: http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories | 26 | [eslintignore]: http://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories |
27 | |||
28 | ## Configuration of ESLintNPM | ||
29 | |||
30 | Pronto::ESLintNPM can be configured by placing a `.pronto_eslint_npm.yml` inside the directory | ||
31 | where pronto is run. | ||
32 | |||
33 | Following options are available: | ||
34 | |||
35 | | Option | Meaning | Default | | ||
36 | | ----------------- | --------------------------------------------------------------------------------- | ----------------------------------- | | ||
37 | | eslint_executable | ESLint executable to call | `eslint` (calls `eslint` in `PATH`) | | ||
38 | | files_to_lint | What files to lint. Absolute path of the file will be matched against this regexp | `(\.js|\.es6)$` | | ||
39 | |||
40 | Example configuration to call custom eslint executable and only lint files ending with `.my_custom_extension`: | ||
41 | |||
42 | ```yaml | ||
43 | # .pronto_eslint_npm.yaml | ||
44 | eslint_executable: '/my/cusom/node/path/.bin/eslint' | ||
45 | files_to_lint: '\.my_custom_extension$' | ||
46 | ``` | ||
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 | ||
4 | module Pronto | 4 | module 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 |
diff --git a/pronto-eslint_npm.gemspec b/pronto-eslint_npm.gemspec index f51cbbb..5b62845 100644 --- a/pronto-eslint_npm.gemspec +++ b/pronto-eslint_npm.gemspec | |||
@@ -27,5 +27,4 @@ Gem::Specification.new do |s| | |||
27 | s.add_dependency('pronto', '~> 0.7.0') | 27 | s.add_dependency('pronto', '~> 0.7.0') |
28 | s.add_development_dependency('rake', '~> 11.0') | 28 | s.add_development_dependency('rake', '~> 11.0') |
29 | s.add_development_dependency('rspec', '~> 3.4') | 29 | s.add_development_dependency('rspec', '~> 3.4') |
30 | s.add_development_dependency('rspec-its', '~> 1.2') | ||
31 | end | 30 | end |
diff --git a/spec/fixtures/test.git/custom_eslint.sh b/spec/fixtures/test.git/custom_eslint.sh new file mode 100755 index 0000000..54fafdc --- /dev/null +++ b/spec/fixtures/test.git/custom_eslint.sh | |||
@@ -0,0 +1 @@ | |||
printf 'custom eslint called' | |||
diff --git a/spec/pronto/eslint_spec.rb b/spec/pronto/eslint_spec.rb index 7bbc77a..6358621 100644 --- a/spec/pronto/eslint_spec.rb +++ b/spec/pronto/eslint_spec.rb | |||
@@ -3,6 +3,7 @@ require 'spec_helper' | |||
3 | module Pronto | 3 | module Pronto |
4 | describe ESLintNpm do | 4 | describe ESLintNpm do |
5 | let(:eslint) { ESLintNpm.new(patches) } | 5 | let(:eslint) { ESLintNpm.new(patches) } |
6 | let(:patches) { [] } | ||
6 | 7 | ||
7 | describe '#run' do | 8 | describe '#run' do |
8 | subject(:run) { eslint.run } | 9 | subject(:run) { eslint.run } |
@@ -35,6 +36,33 @@ module Pronto | |||
35 | it 'has correct first message' do | 36 | it 'has correct first message' do |
36 | expect(run.first.msg).to eql("'foo' is not defined.") | 37 | expect(run.first.msg).to eql("'foo' is not defined.") |
37 | end | 38 | end |
39 | |||
40 | context( | ||
41 | 'with files to lint config that never matches', | ||
42 | config: { files_to_lint: 'will never match' } | ||
43 | ) do | ||
44 | it 'returns zero errors' do | ||
45 | expect(run.count).to eql(0) | ||
46 | end | ||
47 | end | ||
48 | |||
49 | context( | ||
50 | 'with files to lint config that matches only .js', | ||
51 | config: { files_to_lint: /\.js/ } | ||
52 | ) do | ||
53 | it 'returns correct amount of errors' do | ||
54 | expect(run.count).to eql(2) | ||
55 | end | ||
56 | end | ||
57 | |||
58 | context( | ||
59 | 'with different eslint executable', | ||
60 | config: { eslint_executable: './custom_eslint.sh' } | ||
61 | ) do | ||
62 | it 'calls the custom eslint eslint_executable' do | ||
63 | expect { run }.to raise_error(JSON::ParserError, /custom eslint called/) | ||
64 | end | ||
65 | end | ||
38 | end | 66 | end |
39 | 67 | ||
40 | context 'repo with ignored and not ignored file, each with three warnings' do | 68 | context 'repo with ignored and not ignored file, each with three warnings' do |
@@ -52,8 +80,8 @@ module Pronto | |||
52 | end | 80 | end |
53 | end | 81 | end |
54 | 82 | ||
55 | describe '.files_to_lint' do | 83 | describe '#files_to_lint' do |
56 | subject(:files_to_lint) { ESLintNpm.files_to_lint } | 84 | subject(:files_to_lint) { eslint.files_to_lint } |
57 | 85 | ||
58 | it 'matches .js by default' do | 86 | it 'matches .js by default' do |
59 | expect(files_to_lint).to match('my_js.js') | 87 | expect(files_to_lint).to match('my_js.js') |
@@ -64,8 +92,8 @@ module Pronto | |||
64 | end | 92 | end |
65 | end | 93 | end |
66 | 94 | ||
67 | describe '.eslint_executable' do | 95 | describe '#eslint_executable' do |
68 | subject(:eslint_executable) { ESLintNpm.eslint_executable } | 96 | subject(:eslint_executable) { eslint.eslint_executable } |
69 | 97 | ||
70 | it 'is `eslint` by default' do | 98 | it 'is `eslint` by default' do |
71 | expect(eslint_executable).to eql('eslint') | 99 | expect(eslint_executable).to eql('eslint') |
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 72a128b..da4fc00 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb | |||
@@ -1,6 +1,5 @@ | |||
1 | require 'fileutils' | 1 | require 'fileutils' |
2 | require 'rspec' | 2 | require 'rspec' |
3 | require 'rspec/its' | ||
4 | require 'pronto/eslint_npm' | 3 | require 'pronto/eslint_npm' |
5 | 4 | ||
6 | %w(test eslintignore).each do |repo_name| | 5 | %w(test eslintignore).each do |repo_name| |
@@ -13,3 +12,12 @@ require 'pronto/eslint_npm' | |||
13 | after { FileUtils.mv(dot_git, git) } | 12 | after { FileUtils.mv(dot_git, git) } |
14 | end | 13 | end |
15 | end | 14 | end |
15 | |||
16 | RSpec.shared_context 'with config', config: true do | ||
17 | requested_config = metadata[:config].to_yaml | ||
18 | |||
19 | let(:config_path) { File.join(repo.path.to_s, Pronto::ESLintNpm::CONFIG_FILE) } | ||
20 | |||
21 | before(:each) { File.write(config_path, requested_config) } | ||
22 | after(:each) { FileUtils.rm(config_path) } | ||
23 | end | ||