From 3403f9d12247884c18ffe7a1636fe12c3fb0f0da Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Sun, 11 Sep 2016 14:07:42 +0200 Subject: [PATCH] add class variables to prepare setting external config --- lib/pronto/eslint_npm.rb | 25 +++++++++++++++++++------ spec/pronto/eslint_spec.rb | 20 ++++++++++++++++++++ 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' module Pronto class ESLintNpm < Runner + class << self + attr_writer :eslint_executable, :files_to_lint + + def eslint_executable + @eslint_executable || 'eslint'.freeze + end + + def files_to_lint + @files_to_lint || /(\.js|\.es6)$/ + end + end + def run return [] unless @patches - @patches.select { |patch| patch.additions > 0 } + @patches + .select { |patch| patch.additions > 0 } .select { |patch| js_file?(patch.new_file_full_path) } .map { |patch| inspect(patch) } .flatten.compact end + private + def inspect(patch) @_repo_path ||= @patches.first.repo.path @@ -25,8 +40,6 @@ module Pronto end end - private - def new_message(offence, line) path = line.patch.delta.new_file[:path] level = :warning @@ -35,18 +48,18 @@ module Pronto end def js_file?(path) - %w(.js .es6 .js.es6).include?(File.extname(path)) + self.class.files_to_lint =~ path.to_s end def run_eslint(patch) Dir.chdir(@_repo_path) do + escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s) JSON.parse( - `eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json` + `#{self.class.eslint_executable} #{escaped_file_path} -f json` ) end end - # rubocop:disable Metrics/LineLength def clean_up_eslint_output(output) # 1. Filter out offences without a warning or error # 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 end end end + + describe '.files_to_lint' do + subject(:files_to_lint) { ESLintNpm.files_to_lint } + + it 'matches .js by default' do + expect(files_to_lint).to match('my_js.js') + end + + it 'matches .es6 by default' do + expect(files_to_lint).to match('my_js.es6') + end + end + + describe '.eslint_executable' do + subject(:eslint_executable) { ESLintNpm.eslint_executable } + + it 'is `eslint` by default' do + expect(eslint_executable).to eql('eslint') + end + end end end -- 2.41.0