-
Notifications
You must be signed in to change notification settings - Fork 7
/
wrapper.js
219 lines (195 loc) · 4.66 KB
/
wrapper.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*jslint vars: true, nomen: true, node: true, white: true */
"use strict";
var fs = require("fs"),
path = require("path"),
vm = require("vm"),
util = require("util");
var VERSION = "1.4.0";
var LINT = {
name: "JSLint",
filename: "jslint.js",
root: "jslint",
source: {
scheme: "https",
host: "raw.githubusercontent.com",
path: "/douglascrockford/JSLint/master/jslint.js"
}
};
var getJSLint, formatOutput, transformWarning, parseOptions, supplant, exit;
var main = function(args) {
args = args.slice(2); // ignore Node command and script file
var opts = parseOptions(args);
var anon = opts.anon;
opts = opts.opts;
LINT.path = path.join(__dirname, LINT.filename);
var verbose = false;
if(opts.verbose) {
delete opts.verbose;
verbose = true;
}
if(opts.help || args.length === 0) {
var readme = fs.readFileSync(path.join(__dirname, "README"), "utf-8");
exit(args.length > 0, readme);
}
if(opts.upgrade) {
getJSLint(function(contents) {
fs.writeFileSync(LINT.path, contents);
main([null, null, "--version"]); // XXX: hacky!?
exit(true);
});
return;
}
var jslint;
try {
jslint = fs.readFileSync(LINT.path, "utf-8");
} catch(exc) {
exit(false, "ERROR: " + LINT.path + " not found - " +
"use `--upgrade` to initialize");
}
if(opts.version) {
var sandbox = {};
vm.runInNewContext(jslint, sandbox);
exit(true, "JSLint Reporter v" + VERSION + "\n" +
LINT.name + " v" + sandbox[LINT.root]().edition);
}
if(verbose) {
process.stderr.write("JSLint options: " + util.inspect(opts) + "\n");
}
var doLint = function(filepath) {
var src = fs.readFileSync(filepath, "utf-8");
var sandbox = {
SRC: src,
OPTS: opts
};
var code = "var data = " + LINT.root + "(SRC, OPTS);";
vm.runInNewContext(jslint + "\n" + code, sandbox);
return sandbox.data.warnings.map(transformWarning);
};
var errors = [];
var i;
for(i = 0; i < anon.length; i += 1) {
var filepath = anon[i];
var err = doLint(filepath);
err = formatOutput(err, filepath, opts.format);
errors = errors.concat(err);
}
var pass = errors.length === 0;
if(!pass) {
console.log(errors.join("\n") + "\n");
if(verbose) {
process.stderr.write(String(errors.length) + " errors\n");
}
}
exit(pass);
};
getJSLint = function(callback) {
var https = require(LINT.source.scheme);
var options = {
host: LINT.source.host,
path: LINT.source.path
};
https.get(options, function(response) {
if(response.statusCode !== 200) {
exit(false, "failed to retrieve JSLint file");
}
response.setEncoding("utf8");
var body = [];
response.on("data", function(chunk) {
body.push(chunk);
});
response.on("end", function() {
callback(body.join(""));
});
});
};
// `format` is an optional string with the following placeholders:
// {f}: file
// {l}: line
// {c}: character
// {m}: message
formatOutput = function(errors, filepath, format) {
format = format || "{f}:{l}:{c}:{m}";
var lines = [];
var i;
for(i = 0; i < errors.length; i += 1) {
var error = errors[i];
if(error) { // last item might be null (if linting was aborted)
var line = supplant(format, {
f: filepath,
l: error.line,
c: error.character,
m: error.reason
});
lines.push(line);
}
}
return lines;
};
// generate an error from a warning
transformWarning = function(item) {
return {
line: item.line,
character: item.column,
reason: item.message
};
};
parseOptions = function(args) {
var opts = {};
var anon = [];
var i;
for(i = 0; i < args.length; i += 1) {
var arg = args[i];
if(arg.indexOf("--") === 0) {
arg = arg.substr(2);
if(arg.indexOf("=") === -1) {
opts[arg] = true;
} else {
var pair = arg.split("="); // NB: assumes exactly one "="
var name = pair[0];
var value = pair[1];
// infer value type
if(value === "false") {
value = false;
}
switch(name) { // XXX: special-casing JSLint-specifics
case "indent":
case "maxerr":
case "maxlen":
value = parseInt(value, 10);
break;
case "predef":
value = value.split(",");
break;
default:
break;
}
opts[name] = value;
}
} else {
anon.push(arg);
}
}
return {
opts: opts,
anon: anon
};
};
// adapted from Crockford:
// http://javascript.crockford.com/remedial.html
supplant = function(str, params) {
return str.replace(/\{([^{}]*)\}/g, function(placeholder, key) {
var sub = params[key];
return (typeof sub === "string" || typeof sub === "number") ? sub :
placeholder;
});
};
exit = function(status, msg) {
if(msg) {
process.stderr.write(msg + "\n");
}
try {
process.stdout.flush(); // required for legacy support (Node <0.5)?
} catch(exc) {}
process.exit(status ? 0 : 1);
};
main(process.argv);