Class: PreCommit::JshintCheck
- Inherits:
-
Object
- Object
- PreCommit::JshintCheck
- Defined in:
- lib/pre-commit/checks/jshint_check.rb
Instance Attribute Summary collapse
-
#therubyracer_installed ⇒ Object
Returns the value of attribute therubyracer_installed.
Instance Method Summary collapse
- #call ⇒ Object
- #display_error(error_object, file) ⇒ Object
-
#initialize ⇒ JshintCheck
constructor
A new instance of JshintCheck.
- #jshint_src ⇒ Object
- #load_staged_files ⇒ Object
- #reject_non_js(staged_files) ⇒ Object
- #ruby_racer_installed? ⇒ Boolean
- #run(js_files) ⇒ Object
- #should_run?(js_files) ⇒ Boolean
Constructor Details
#initialize ⇒ JshintCheck
Returns a new instance of JshintCheck.
9 10 11 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 9 def initialize @therubyracer_installed = ruby_racer_installed? end |
Instance Attribute Details
#therubyracer_installed ⇒ Object
Returns the value of attribute therubyracer_installed.
7 8 9 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 7 def therubyracer_installed @therubyracer_installed end |
Instance Method Details
#call ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 13 def call js_files = reject_non_js(load_staged_files) if should_run?(js_files) run(js_files) else $stderr.puts 'pre-commit: Skipping JSHint check (to run it: `gem install therubyracer`)' # pretend the check passed and move on true end end |
#display_error(error_object, file) ⇒ Object
81 82 83 84 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 81 def display_error(error_object, file) line = error_object['line'].to_i + 1 "pre-commit: JSHINT #{error_object['reason']}\n#{file}:#{line} #{error_object['evidence']}" end |
#jshint_src ⇒ Object
69 70 71 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 69 def jshint_src File.join(PreCommit.root, 'lib', 'support', 'jshint', 'jshint.js') end |
#load_staged_files ⇒ Object
73 74 75 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 73 def load_staged_files Utils.staged_files('.').split(" ") end |
#reject_non_js(staged_files) ⇒ Object
77 78 79 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 77 def reject_non_js(staged_files) staged_files.reject! { |f| f !~ /\.js$/ } end |
#ruby_racer_installed? ⇒ Boolean
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 56 def ruby_racer_installed? if instance_variable_defined?(:@therubyracer_installed) @therubyracer_installed else begin require 'v8' @therubyracer_installed = true rescue LoadError @therubyracer_installed = false end end end |
#run(js_files) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 25 def run(js_files) errors = [] js_files.each do |file| V8::Context.new do |context| context.load(jshint_src) context['source'] = lambda { File.read(file) } context['report'] = lambda do |array| array.each { |error_object| errors << display_error(error_object, file) } end context.eval('var result = JSHINT(source());') context.eval('report(JSHINT.errors);') end end if errors.empty? true else $stderr.puts errors.join("\n") $stderr.puts $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`' $stderr.puts false end end |
#should_run?(js_files) ⇒ Boolean
52 53 54 |
# File 'lib/pre-commit/checks/jshint_check.rb', line 52 def should_run?(js_files) ruby_racer_installed? && js_files.any? end |