UNPKG

1.77 kBJavaScriptView Raw
1var acorn = require("../acorn.js");
2var walk = require("./walk.js");
3require("../test/codemirror-string.js");
4
5var parsed = acorn.parse(codemirror30, {
6 locations: true,
7 ecmaVersion: 3,
8 strictSemicolons: true,
9 forbidReserved: true
10});
11
12walk.simple(parsed, {
13 ScopeBody: function(node, scope) {
14 node.scope = scope;
15 }
16}, walk.scopeVisitor);
17
18var scopePasser = walk.make({
19 ScopeBody: function(node, prev, c) { c(node, node.scope); }
20});
21
22var ignoredGlobals = Object.create(null);
23"arguments window document navigator \
24Array Math String Number RegExp Boolean Error Date Object \
25setTimeout clearTimeout setInterval clearInterval \
26Infinity NaN undefined JSON FileReader".split(" ").forEach(function(ignore) {
27 ignoredGlobals[ignore] = true;
28});
29var hop = Object.prototype.hasOwnProperty;
30
31function inScope(name, scope) {
32 for (var cur = scope; cur; cur = cur.prev)
33 if (hop.call(cur.vars, name)) return true;
34}
35function checkLHS(node, scope) {
36 if (node.type == "Identifier" && !hop.call(ignoredGlobals, node.name) &&
37 !inScope(node.name, scope)) {
38 ignoredGlobals[node.name] = true;
39 console.log("Assignment to global variable " + node.name +
40 " (" + node.loc.start.line + ":" + node.loc.start.column + ")");
41 }
42}
43function checkInScope(node, scope) {
44 if (!hop.call(ignoredGlobals, node.name) && !inScope(node.name, scope)) {
45 ignoredGlobals[node.name] = true;
46 console.log("Using global variable " + node.name +
47 " (" + node.loc.start.line + ":" + node.loc.start.column + ")");
48 }
49}
50
51walk.simple(parsed, {
52 UpdateExpression: function(node, scope) {checkLHS(node.argument, scope);},
53 AssignmentExpression: function(node, scope) {checkLHS(node.left, scope);},
54 Identifier: checkInScope
55}, scopePasser);