-
Notifications
You must be signed in to change notification settings - Fork 1
/
rake-jslint.rb
142 lines (120 loc) · 4.06 KB
/
rake-jslint.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require "rake"
require "albacore"
require "rexml/document"
require "runcommandwithfail"
require "rake-nodejs"
require "rake-support"
require "colorize"
class JSLint
include Albacore::Task
include RunCommandWithFail
include Rake::DSL
# base location of the web project
# (default ".")
attr_accessor :base
# location where lint results will be stored (relative to base)
# (default "buildreports/")
attr_accessor :reports
# array of base location(s) of js source code to be linted
# (default ".")
attr_accessor :source
# true to run Checkstyle report, false to run JSLint
# (default false)
attr_accessor :checkstyle
def initialize
@base = "."
@reports = "buildreports"
@raketasks = File.dirname(__FILE__)
@jshint = File.join(@raketasks, "node_modules/.bin", "jshint.cmd")
unless @jshint.nil?
@jshint = File.expand_path(@jshint) if File.exists?(@jshint)
end
npm do |npm|
npm.base = @raketasks
end
Rake::Task[:npm].execute
Rake::Task[:npm].clear
super()
end
def execute
reports = File.expand_path(File.join(@base, @reports))
FileSystem.EnsurePath(reports)
source = @source
source = "." if @source.nil? || @source.length == 0
@command = @jshint
@working_directory = @base
params = ["--jslint-reporter"] unless @checkstyle
params = ["--checkstyle-reporter"] if @checkstyle
params << "--config" << "#{@raketasks}/.jshintrc"
params << source
report = File.join(reports, "jslint.xml") unless @checkstyle
report = File.join(reports, "checkstyle-jshint.xml") if @checkstyle
FileSystem.CaptureOutput(report) do
run_command("JSHint", params, false)
end
end
end
class JSLintOutput
include Albacore::Task
# base location of the web project
# (default ".")
attr_accessor :base
# location where lint results can be found (relative to base)
# (default "buildreports/")
attr_accessor :reports
def initialize
@base = "."
@reports = "buildreports"
super()
end
def getColorForSeverity(severity)
severityColors = {
"error" => :red,
"E" => :red,
"warning" => :yellow,
"W" => :yellow
}
if severityColors.keys.index(severity) == nil
return :magenta
end
return severityColors[severity]
end
def getNodeSpec(doc)
test = "/checkstyle"
doc.elements.each(test) do |el|
return test + "/file", "error", "column"
end
test = "/jslint"
doc.elements.each(test) do |el|
return test + "/file", "issue", "char"
end
raise "Can't determine node to look for ):"
end
def execute
reports = File.expand_path(File.join(@base, @reports))
reportFilePath = File.join(reports, "jslint.xml") unless @checkstyle
if not File.exists?(reportFilePath)
puts red("Lint output to console reporter: Unable to find lint report at: %s " % [ reportFilePath ])
return
end
doc = REXML::Document.new(File.new(reportFilePath))
nodeSpec, subEl, colAttribute = getNodeSpec(doc)
doc.elements.each(nodeSpec) do |fileElement|
if not fileElement.has_elements?
next
end
puts fileElement.attributes["name"].colorize("cyan")
fileElement.elements.each(subEl) do |errorElement|
color = getColorForSeverity(errorElement.attributes["severity"])
col = errorElement.attributes[colAttribute]
line = errorElement.attributes["line"]
msg = errorElement.attributes["message"]
if msg.nil?
msg = errorElement.attributes["reason"]
end
logLine = " [%s:%s] %s" % [line, col, msg]
puts logLine.colorize(color)
end
end
end
end