forked from uipoet/sublime-jshint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjshint.wsh.js
170 lines (128 loc) · 4.15 KB
/
jshint.wsh.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*jshint evil: true, shadow: true, wsh: true */
/*global JSHINT: false */
(function() {
function readFile(path) {
try {
return new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(path, 1).ReadAll();
} catch (ex) {
return null;
}
}
var formatters = {
errors: function(errors, lines) {
for (var i = 0; i < errors.length; i++) {
var error = errors[i];
if (!error) continue;
if (i) lines.push("");
lines.push("Line " + error.line + " character " + error.character + ": " + error.reason);
if (error.evidence) lines.push(" " + error.evidence.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, "$1"));
}
},
implieds: function(implieds, lines) {
lines.push("Implied globals:");
var globals = {};
for (var i = 0; i < implieds.length; i++) {
var item = implieds[i];
if (!(item.name in globals)) globals[item.name] = [];
globals[item.name].push(item.line);
}
for (var name in globals) {
lines.push(" " + name + ": " + globals[name].join(", "));
}
},
unused: function(unused, lines) {
lines.push("Unused variables:");
var func, names = {};
for (var i = 0; i < unused.length; i++) {
var item = unused[i];
func = item["function"];
if (!(func in names)) names[func] = [];
names[func].push(item.name + " (" + item.line + ")");
}
for (func in names) {
lines.push(" " + func + ": " + names[func].join(", "));
}
}
};
var scriptName = WScript.ScriptName;
var scriptPath = WScript.ScriptFullName;
scriptPath = scriptPath.substr(0, scriptPath.length - scriptName.length);
// load JSHint if the two scripts have not been concatenated
if (typeof JSHINT === "undefined") {
eval(readFile(scriptPath + "jshint.js"));
if (typeof JSHINT === "undefined") {
WScript.StdOut.WriteLine("ERROR: Could not find 'jshint.js'.");
WScript.Quit(-2);
}
}
var globals = {};
var options = {};
var named = WScript.Arguments.Named;
var unnamed = WScript.Arguments.Unnamed;
if (unnamed.length !== 1) {
WScript.StdOut.WriteLine(" usage: cscript " + scriptName + " [options] <script>");
WScript.StdOut.WriteLine("");
WScript.StdOut.WriteLine("Scans the specified script with JSHint and reports any errors encountered. If");
WScript.StdOut.WriteLine("the script name is \"-\", it will be read from standard input instead.");
WScript.StdOut.WriteLine("");
WScript.StdOut.WriteLine("JSHint configuration options can be passed in via optional, Windows-style");
WScript.StdOut.WriteLine("arguments. For example:");
WScript.StdOut.WriteLine(" cscript " + scriptName + " /jquery:true myscript.js");
WScript.StdOut.WriteLine(" cscript " + scriptName + " /globals:QUnit:false,_:false,foo:true foo.js");
WScript.Quit(-1);
}
var script = unnamed(0);
if (script === "-") {
try {
script = WScript.StdIn.ReadAll();
} catch (ex) {
script = null;
}
} else {
var path = script
script = readFile(script);
}
if (script === null) {
WScript.StdOut.WriteLine("ERROR: Could not read target script.");
WScript.Quit(2);
}
for (var etor = new Enumerator(named); !etor.atEnd(); etor.moveNext()) {
var option = etor.item();
var value = named(option);
if (option === "global") {
value = value.split(",");
for (var i = 0; i < value.length; i++) {
var name = value[i].split(":");
if (name.length === 1 || name[1] === "false") {
globals[name[0]] = false;
} else if (name[1] === "true") {
globals[name[0]] = true;
} else {
WScript.StdOut.WriteLine("Unrecognized value for global: " + name[0]);
WScript.StdOut.WriteLine("Must be \"true\", \"false\", or omitted.");
WScript.Quit(-1);
}
}
} else {
options[option] = value === "true" ? true : value === "false" ? false : value;
}
}
JSHINT(script, options, globals);
var data = JSHINT.data();
var lines = [];
lines.push('File "' + path + '"');
for (var formatter in formatters) {
if (data[formatter]) {
if (lines.length) lines.push("");
formatters[formatter](data[formatter], lines);
}
}
if (lines.length) {
for (var i = 0; i < lines.length; i++) {
WScript.StdOut.WriteLine(lines[i]);
}
WScript.Quit(1);
} else {
WScript.Quit(0);
}
}());