Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/test/lint/lint.js
1294 views
1
/*
2
Simple linter, based on the Acorn [1] parser module
3
4
All of the existing linters either cramp my style or have huge
5
dependencies (Closure). So here's a very simple, non-invasive one
6
that only spots
7
8
- missing semicolons and trailing commas
9
- variables or properties that are reserved words
10
- assigning to a variable you didn't declare
11
- access to non-whitelisted globals
12
(use a '// declare global: foo, bar' comment to declare extra
13
globals in a file)
14
15
[1]: https://github.com/marijnh/acorn/
16
*/
17
18
var topAllowedGlobals = Object.create(null);
19
("Error RegExp Number String Array Function Object Math Date undefined " +
20
"parseInt parseFloat Infinity NaN isNaN " +
21
"window document navigator prompt alert confirm console " +
22
"FileReader Worker postMessage importScripts " +
23
"setInterval clearInterval setTimeout clearTimeout " +
24
"CodeMirror test")
25
.split(" ").forEach(function(n) { topAllowedGlobals[n] = true; });
26
27
var fs = require("fs"), acorn = require("./acorn.js"), walk = require("./walk.js");
28
29
var scopePasser = walk.make({
30
ScopeBody: function(node, prev, c) { c(node, node.scope); }
31
});
32
33
function checkFile(fileName) {
34
var file = fs.readFileSync(fileName, "utf8"), notAllowed;
35
if (notAllowed = file.match(/[\x00-\x08\x0b\x0c\x0e-\x19\uFEFF\t]|[ \t]\n/)) {
36
var msg;
37
if (notAllowed[0] == "\t") msg = "Found tab character";
38
else if (notAllowed[0].indexOf("\n") > -1) msg = "Trailing whitespace";
39
else msg = "Undesirable character " + notAllowed[0].charCodeAt(0);
40
var info = acorn.getLineInfo(file, notAllowed.index);
41
fail(msg + " at line " + info.line + ", column " + info.column, {source: fileName});
42
}
43
44
var globalsSeen = Object.create(null);
45
46
try {
47
var parsed = acorn.parse(file, {
48
locations: true,
49
ecmaVersion: 3,
50
strictSemicolons: true,
51
allowTrailingCommas: false,
52
forbidReserved: true,
53
sourceFile: fileName
54
});
55
} catch (e) {
56
fail(e.message, {source: fileName});
57
return;
58
}
59
60
var scopes = [];
61
62
walk.simple(parsed, {
63
ScopeBody: function(node, scope) {
64
node.scope = scope;
65
scopes.push(scope);
66
}
67
}, walk.scopeVisitor, {vars: Object.create(null)});
68
69
var ignoredGlobals = Object.create(null);
70
71
function inScope(name, scope) {
72
for (var cur = scope; cur; cur = cur.prev)
73
if (name in cur.vars) return true;
74
}
75
function checkLHS(node, scope) {
76
if (node.type == "Identifier" && !(node.name in ignoredGlobals) &&
77
!inScope(node.name, scope)) {
78
ignoredGlobals[node.name] = true;
79
fail("Assignment to global variable", node.loc);
80
}
81
}
82
83
walk.simple(parsed, {
84
UpdateExpression: function(node, scope) {checkLHS(node.argument, scope);},
85
AssignmentExpression: function(node, scope) {checkLHS(node.left, scope);},
86
Identifier: function(node, scope) {
87
if (node.name == "arguments") return;
88
// Mark used identifiers
89
for (var cur = scope; cur; cur = cur.prev)
90
if (node.name in cur.vars) {
91
cur.vars[node.name].used = true;
92
return;
93
}
94
globalsSeen[node.name] = node.loc;
95
},
96
FunctionExpression: function(node) {
97
if (node.id) fail("Named function expression", node.loc);
98
}
99
}, scopePasser);
100
101
if (!globalsSeen.exports) {
102
var allowedGlobals = Object.create(topAllowedGlobals), m;
103
if (m = file.match(/\/\/ declare global:\s+(.*)/))
104
m[1].split(/,\s*/g).forEach(function(n) { allowedGlobals[n] = true; });
105
for (var glob in globalsSeen)
106
if (!(glob in allowedGlobals))
107
fail("Access to global variable " + glob + ". Add a '// declare global: " + glob +
108
"' comment or add this variable in test/lint/lint.js.", globalsSeen[glob]);
109
}
110
111
112
for (var i = 0; i < scopes.length; ++i) {
113
var scope = scopes[i];
114
for (var name in scope.vars) {
115
var info = scope.vars[name];
116
if (!info.used && info.type != "catch clause" && info.type != "function name" && name.charAt(0) != "_")
117
fail("Unused " + info.type + " " + name, info.node.loc);
118
}
119
}
120
}
121
122
var failed = false;
123
function fail(msg, pos) {
124
if (pos.start) msg += " (" + pos.start.line + ":" + pos.start.column + ")";
125
console.log(pos.source + ": " + msg);
126
failed = true;
127
}
128
129
function checkDir(dir) {
130
fs.readdirSync(dir).forEach(function(file) {
131
var fname = dir + "/" + file;
132
if (/\.js$/.test(file)) checkFile(fname);
133
else if (file != "dep" && fs.lstatSync(fname).isDirectory()) checkDir(fname);
134
});
135
}
136
137
exports.checkDir = checkDir;
138
exports.checkFile = checkFile;
139
exports.success = function() { return !failed; };
140
141