Class: PreCommit::JsCheck

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

Direct Known Subclasses

JshintCheck, JslintCheck

Instance Method Summary collapse

Instance Method Details

#callObject



8
9
10
11
12
13
14
15
16
# File 'lib/pre-commit/checks/js_check.rb', line 8

def call
  js_files = reject_non_js(files_to_check)
  if should_run?(js_files)
    run(js_files)
  else
    # pretend the check passed and move on
    true
  end
end

#check_nameObject



45
46
47
# File 'lib/pre-commit/checks/js_check.rb', line 45

def check_name
  raise "Must be defined by subclass"
end

#display_error(error_object, file) ⇒ Object



53
54
55
56
57
58
# File 'lib/pre-commit/checks/js_check.rb', line 53

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

  line = error_object['line'].to_i + 1
  "pre-commit: #{check_name.upcase} #{error_object['reason']}\n#{file}:#{line} #{error_object['evidence']}"
end

#linter_srcObject



49
50
51
# File 'lib/pre-commit/checks/js_check.rb', line 49

def linter_src
  raise "Must be defined by subclass"
end

#reject_non_js(staged_files) ⇒ Object



41
42
43
# File 'lib/pre-commit/checks/js_check.rb', line 41

def reject_non_js(staged_files)
  staged_files.select { |f| f =~ /\.js$/ }
end

#run(js_files) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pre-commit/checks/js_check.rb', line 18

def run(js_files)
  errors = []

  js_files.each do |file|
    error_list = Array(run_check(file))
    error_list.each { |error_object| errors << display_error(error_object, file) }
  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



37
38
39
# File 'lib/pre-commit/checks/js_check.rb', line 37

def should_run?(js_files)
  js_files.any?
end