pre-commit 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ require 'pre-commit/checks/migration_check'
10
10
 
11
11
  class PreCommit
12
12
 
13
- WhiteSpace = lambda {
13
+ WhiteSpace = lambda {
14
14
  WhiteSpaceChecker.check
15
15
  }
16
16
 
@@ -24,38 +24,6 @@ class PreCommit
24
24
  end
25
25
  }
26
26
 
27
- JSLintCheck = lambda { |files|
28
- errors = []
29
- files.each do |file|
30
- errors << JSLint.lint_file(file)
31
- end
32
-
33
- # JSLint.lint_file returns an array.
34
- # Therefore no errors looks like this: [[]]
35
- # And errors.empty? returns false
36
- errors.flatten!
37
-
38
- if errors.empty?
39
- true
40
- else
41
- $stderr.puts errors.join("\n")
42
- $stderr.puts
43
- $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
44
- $stderr.puts
45
- false
46
- end
47
- }
48
-
49
- JSLintNew = lambda {
50
- new_js_files = Utils.new_files('.').split(" ").reject {|f| f !~ /\.js$/}
51
- JSLintCheck[new_js_files]
52
- }
53
-
54
- JSLintAll = lambda {
55
- staged_js_files = Utils.staged_files('.').split(" ").reject {|f| f !~ /\.js$/}
56
- JSLintCheck[staged_js_files]
57
- }
58
-
59
27
  Checks = {
60
28
  :white_space => WhiteSpace,
61
29
  :console_log => ConsoleLog,
@@ -0,0 +1,59 @@
1
+ require 'pre-commit/base'
2
+ require 'pre-commit/utils'
3
+ require 'execjs'
4
+
5
+ class PreCommit
6
+ class JsCheck
7
+
8
+ def call
9
+ js_files = reject_non_js(files_to_check)
10
+ if should_run?(js_files)
11
+ run(js_files)
12
+ else
13
+ # pretend the check passed and move on
14
+ true
15
+ end
16
+ end
17
+
18
+ def run(js_files)
19
+ errors = []
20
+
21
+ js_files.each do |file|
22
+ error_list = run_check(file)
23
+ error_list.each { |error_object| errors << display_error(error_object, file) }
24
+ end
25
+
26
+ if errors.empty?
27
+ true
28
+ else
29
+ $stderr.puts errors.join("\n")
30
+ $stderr.puts
31
+ $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
32
+ $stderr.puts
33
+ false
34
+ end
35
+ end
36
+
37
+ def should_run?(js_files)
38
+ js_files.any?
39
+ end
40
+
41
+ def reject_non_js(staged_files)
42
+ staged_files.select { |f| f =~ /\.js$/ }
43
+ end
44
+
45
+ def check_name
46
+ raise "Must be defined by subclass"
47
+ end
48
+
49
+ def linter_src
50
+ raise "Must be defined by subclass"
51
+ end
52
+
53
+ def display_error(error_object, file)
54
+ line = error_object['line'].to_i + 1
55
+ "pre-commit: #{check_name.upcase} #{error_object['reason']}\n#{file}:#{line} #{error_object['evidence']}"
56
+ end
57
+
58
+ end
59
+ end
@@ -1,87 +1,27 @@
1
- require 'pre-commit/base'
2
- require 'pre-commit/utils'
1
+ require 'pre-commit/checks/js_check'
3
2
 
4
3
  class PreCommit
5
- class JshintCheck
4
+ class JshintCheck < JsCheck
6
5
 
7
- attr_accessor :therubyracer_installed
8
-
9
- def initialize
10
- @therubyracer_installed = ruby_racer_installed?
11
- end
12
-
13
- def call
14
- js_files = reject_non_js(load_staged_files)
15
-
16
- if should_run?(js_files)
17
- run(js_files)
18
- else
19
- $stderr.puts 'pre-commit: Skipping JSHint check (to run it: `gem install therubyracer`)' if !ruby_racer_installed?
20
- # pretend the check passed and move on
21
- true
22
- end
6
+ def check_name
7
+ "JSHint"
23
8
  end
24
9
 
25
- def run(js_files)
26
- errors = []
27
-
28
- js_files.each do |file|
29
- V8::Context.new do |context|
30
- context.load(jshint_src)
31
- context['source'] = lambda { File.read(file) }
32
- context['report'] = lambda do |array|
33
- array.each { |error_object| errors << display_error(error_object, file) }
34
- end
35
-
36
- context.eval('var result = JSHINT(source());')
37
- context.eval('report(JSHINT.errors);')
38
- end
39
- end
40
-
41
- if errors.empty?
42
- true
43
- else
44
- $stderr.puts errors.join("\n")
45
- $stderr.puts
46
- $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`'
47
- $stderr.puts
48
- false
49
- end
50
- end
51
-
52
- def should_run?(js_files)
53
- ruby_racer_installed? && js_files.any?
10
+ def files_to_check
11
+ Utils.staged_files('.').split(" ")
54
12
  end
55
13
 
56
- def ruby_racer_installed?
57
- if instance_variable_defined?(:@therubyracer_installed)
58
- @therubyracer_installed
14
+ def run_check(file)
15
+ context = ExecJS.compile(File.read(linter_src))
16
+ if !(context.call('JSHINT', File.read(file)))
17
+ context.exec('return JSHINT.errors;')
59
18
  else
60
- begin
61
- require 'v8'
62
- @therubyracer_installed = true
63
- rescue LoadError
64
- @therubyracer_installed = false
65
- end
19
+ []
66
20
  end
67
21
  end
68
22
 
69
- def jshint_src
23
+ def linter_src
70
24
  File.join(PreCommit.root, 'lib', 'support', 'jshint', 'jshint.js')
71
25
  end
72
-
73
- def load_staged_files
74
- Utils.staged_files('.').split(" ")
75
- end
76
-
77
- def reject_non_js(staged_files)
78
- staged_files.select { |f| f =~ /\.js$/ }
79
- end
80
-
81
- def display_error(error_object, file)
82
- line = error_object['line'].to_i + 1
83
- "pre-commit: JSHINT #{error_object['reason']}\n#{file}:#{line} #{error_object['evidence']}"
84
- end
85
-
86
26
  end
87
27
  end
@@ -1,44 +1,40 @@
1
+ require 'pre-commit/checks/js_check'
2
+
1
3
  # This (PreCommit) should be a module
2
4
  # ... it should also have a cooler name :P
3
5
  class PreCommit
4
- class JslintCheck
6
+ class JslintCheck < JsCheck
5
7
 
6
- attr_accessor :therubyracer_installed, :type
8
+ attr_accessor :type
7
9
 
8
- def initialize(type = :all)
9
- @type = type
10
- @therubyracer_installed = check_for_therubyracer_install
10
+ def check_name
11
+ "JSLint"
11
12
  end
12
13
 
13
- def should_run?
14
- @therubyracer_installed
14
+ def initialize(type = :all)
15
+ @type = type
15
16
  end
16
17
 
17
- def call
18
- if should_run?
19
- run
18
+ def files_to_check
19
+ case @type
20
+ when :new
21
+ Utils.new_files('.').split(" ")
20
22
  else
21
- $stderr.puts 'pre-commit: Skipping JSLint check (to run it: `gem install therubyracer`)'
22
- # pretend the check passed and move on
23
- true
23
+ Utils.staged_files('.').split(" ")
24
24
  end
25
25
  end
26
26
 
27
- def run
28
- errors = check_to_run.call
29
- end
30
-
31
- def check_to_run
32
- @type == :all ? JSLintAll : JSLintNew
27
+ def run_check(file)
28
+ context = ExecJS.compile(File.read(linter_src))
29
+ if !(context.call('JSLINT', File.read(file)))
30
+ context.exec('return JSLINT.errors;')
31
+ else
32
+ []
33
+ end
33
34
  end
34
35
 
35
- private
36
-
37
- def check_for_therubyracer_install
38
- require 'v8'
39
- @therubyracer_installed = true
40
- rescue LoadError
41
- @therubyracer_installed = false
36
+ def linter_src
37
+ File.join(PreCommit.root, 'lib', 'support', 'jslint', 'lint.js')
42
38
  end
43
39
 
44
40
  end
data/lib/support/all.rb CHANGED
@@ -1,3 +1,2 @@
1
- require 'support/jslint/jslint_checker'
2
1
  require 'support/closure/closure_checker'
3
2
  require 'support/whitespace/whitespace_checker'
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pre-commit
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 3
4
5
  prerelease:
5
- version: 0.1.11
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 12
10
+ version: 0.1.12
6
11
  platform: ruby
7
12
  authors:
8
13
  - Shajith Chacko, Josh Lubaway
@@ -10,11 +15,24 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-05-17 00:00:00 -07:00
18
+ date: 2011-06-03 00:00:00 -07:00
14
19
  default_executable: pre-commit
15
- dependencies: []
16
-
17
- description:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: execjs
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A git pre-commit hook written in ruby with a few more tricks up it's sleeve
18
36
  email: dontneedmoreemail@example.com
19
37
  executables:
20
38
  - pre-commit
@@ -26,6 +44,7 @@ files:
26
44
  - lib/pre-commit/base.rb
27
45
  - lib/pre-commit/checks/console_log.rb
28
46
  - lib/pre-commit/checks/debugger_check.rb
47
+ - lib/pre-commit/checks/js_check.rb
29
48
  - lib/pre-commit/checks/jshint_check.rb
30
49
  - lib/pre-commit/checks/jslint_check.rb
31
50
  - lib/pre-commit/checks/merge_conflict.rb
@@ -39,7 +58,6 @@ files:
39
58
  - lib/support/closure/closure_checker.rb
40
59
  - lib/support/closure/compiler.jar
41
60
  - lib/support/jshint/jshint.js
42
- - lib/support/jslint/jslint_checker.rb
43
61
  - lib/support/jslint/lint.js
44
62
  - lib/support/whitespace/whitespace
45
63
  - lib/support/whitespace/whitespace_checker.rb
@@ -60,12 +78,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
78
  requirements:
61
79
  - - ">="
62
80
  - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
63
84
  version: "0"
64
85
  required_rubygems_version: !ruby/object:Gem::Requirement
65
86
  none: false
66
87
  requirements:
67
88
  - - ">="
68
89
  - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
69
93
  version: "0"
70
94
  requirements: []
71
95
 
@@ -73,6 +97,6 @@ rubyforge_project:
73
97
  rubygems_version: 1.5.2
74
98
  signing_key:
75
99
  specification_version: 3
76
- summary: What this thing does
100
+ summary: A slightly better git pre-commit hook
77
101
  test_files: []
78
102
 
@@ -1,43 +0,0 @@
1
-
2
- class JSLint
3
- OK_REASONS = [ "Expected an identifier and instead saw 'undefined' (a reserved word).",
4
- "Use '===' to compare with 'null'.",
5
- "Use '!==' to compare with 'null'.",
6
- "Expected an assignment or function call and instead saw an expression.",
7
- "Expected a 'break' statement before 'case'.",
8
- "'e' is already defined." ]
9
-
10
- LINT_PATH = File.join(File.dirname(__FILE__), "lint.js")
11
-
12
- def self.lint_file(file)
13
- errors = []
14
- V8::Context.new do |context|
15
- context.load(LINT_PATH)
16
- context['input'] = lambda{
17
- File.read(file)
18
- }
19
-
20
- context['reportErrors'] = lambda{|js_errors|
21
- js_errors.each do |e|
22
- next if e.nil? || OK_REASONS.include?(e.reason)
23
-
24
- errors << "\n\e[1;31mJSLINT: #{file}\e[0m"
25
- errors << " Error at line #{e['line'].to_i + 1} " +
26
- "character #{e['character'].to_i + 1}: \e[1;33m#{e['reason']}\e[0m"
27
- errors << "#{e['evidence']}"
28
- end
29
- }
30
-
31
- context.eval %{
32
- JSLINT(input(), { evil: true, forin: true, maxerr: 100 });
33
- reportErrors(JSLINT.errors);
34
- }
35
- end
36
-
37
- return errors
38
- end
39
- end
40
-
41
- if __FILE__ == $0
42
- puts JSLint.lint_file(ARGV[0]).join("\n")
43
- end