-
Notifications
You must be signed in to change notification settings - Fork 100
/
nodelint.js
80 lines (61 loc) · 1.77 KB
/
nodelint.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
var con = console,
vm = require("vm"),
fs = require("fs"),
path = require('path'),
linter = require('./linter.js'),
main = require('./main.js'),
LintStream = require('./lintstream.js');
exports.LintStream = LintStream;
exports.linter = linter;
exports.runMain = main.runMain;
exports.setConsole = function (c) {
'use strict';
con = c;
};
function looksLikeFileName(edition) {
'use strict';
// contains .js or a path separator character '/' or '\'
return (/\.js|\/|\\/).test(edition);
}
exports.looksLikeFileName = looksLikeFileName;
exports.load = function (edition) {
'use strict';
var ctx = vm.createContext(),
fileName,
jslintSource;
function makePathFromName(name) {
return path.join(__dirname, name) + ".js";
}
function makePathFromEdition(edition) {
return makePathFromName("jslint-" + edition);
}
function read(name) {
return fs.readFileSync(name);
}
if (edition) {
if (looksLikeFileName(edition)) {
fileName = edition;
} else {
fileName = makePathFromEdition(edition);
}
try {
jslintSource = read(fileName);
} catch (err) {
con.warn("Unable to load edition " + edition + ", reverting to default. " + err);
}
}
if (!jslintSource) {
jslintSource = read(makePathFromName("jslint"));
}
vm.runInContext(jslintSource, ctx);
if (!ctx.JSLINT) {
ctx.JSLINT = function JSLINT(script, options) {
var data = ctx.jslint(script, options, options.predef);
ctx.JSLINT.data = function () {
return data;
};
};
ctx.JSLINT.edition = ctx.jslint('').edition;
}
return ctx.JSLINT;
};