Class: PreCommit::Checks::Js

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

Direct Known Subclasses

Csslint, Jshint, Jslint

Instance Attribute Summary

Attributes inherited from Plugin

#config, #pluginator

Instance Method Summary collapse

Methods inherited from Plugin

#initialize, #name

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Instance Method Details

#call(staged_files) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pre-commit/checks/js.rb', line 6

def call(staged_files)
  require 'execjs'
rescue RuntimeError, LoadError => e
  $stderr.puts "Could not load execjs: #{e}. You need to manually install execjs to use JavaScript plugins for security reasons."
else
  staged_files = files_filter(staged_files)
  return if staged_files.empty?

  errors = []
  staged_files.each do |file|
    error_list = Array(run_check(file))
    error_list.each { |error_object| errors << display_error(error_object, file) }
  end

  return if errors.empty?
  errors.join("\n")
end

#display_error(error_object, file) ⇒ Object



36
37
38
39
40
41
# File 'lib/pre-commit/checks/js.rb', line 36

def display_error(error_object, file)
  return "" unless error_object

  line = error_object['line'].to_i + 1
  "#{error_object[error_selector]}\n#{file}:#{line} #{error_object['evidence']}"
end

#error_selectorObject



32
33
34
# File 'lib/pre-commit/checks/js.rb', line 32

def error_selector
  'reason'
end

#files_filter(staged_files) ⇒ Object



28
29
30
# File 'lib/pre-commit/checks/js.rb', line 28

def files_filter(staged_files)
  staged_files.grep(/\.js$/)
end

#linter_srcObject



24
25
26
# File 'lib/pre-commit/checks/js.rb', line 24

def linter_src
  raise "Must be defined by subclass"
end