Class: PreCommit::JshintCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/pre-commit/checks/jshint_check.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJshintCheck

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_installedObject

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

#callObject



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_srcObject



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_filesObject



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

Returns:

  • (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

Returns:

  • (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