forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffee_lint.rb
40 lines (36 loc) · 1.16 KB
/
coffee_lint.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Overcommit::Hook::PreCommit
# Runs `coffeelint` against any modified CoffeeScript files.
#
# @see http://www.coffeelint.org/
class CoffeeLint < Base
def run
result = execute(command + applicable_files)
begin
parse_json_messages(result.stdout)
rescue JSON::ParserError => e
[:fail, "Error parsing coffeelint output: #{e.message}"]
end
end
private
def parse_json_messages(output)
JSON.parse(output).collect do |file, messages|
messages.collect { |msg| extract_message(file, msg) }
end.flatten
end
def extract_message(file, message_hash)
type = message_hash['level'].include?('w') ? :warning : :error
line = message_hash['lineNumber']
rule = message_hash['rule']
msg = message_hash['message']
text =
if rule == 'coffeescript_error'
# Syntax errors are output in different format.
# Splice in the file name and grab the first line.
msg.sub('[stdin]', file).split("\n")[0]
else
"#{file}:#{line}: #{msg} (#{rule})"
end
Overcommit::Hook::Message.new(type, file, line, text)
end
end
end