1 | var acorn = require("../acorn.js");
|
2 | var walk = require("./walk.js");
|
3 | require("../test/codemirror-string.js");
|
4 |
|
5 | var parsed = acorn.parse(codemirror30, {
|
6 | locations: true,
|
7 | ecmaVersion: 3,
|
8 | strictSemicolons: true,
|
9 | forbidReserved: true
|
10 | });
|
11 |
|
12 | walk.simple(parsed, {
|
13 | ScopeBody: function(node, scope) {
|
14 | node.scope = scope;
|
15 | }
|
16 | }, walk.scopeVisitor);
|
17 |
|
18 | var scopePasser = walk.make({
|
19 | ScopeBody: function(node, prev, c) { c(node, node.scope); }
|
20 | });
|
21 |
|
22 | var ignoredGlobals = Object.create(null);
|
23 | "arguments window document navigator \
|
24 | Array Math String Number RegExp Boolean Error Date Object \
|
25 | setTimeout clearTimeout setInterval clearInterval \
|
26 | Infinity NaN undefined JSON FileReader".split(" ").forEach(function(ignore) {
|
27 | ignoredGlobals[ignore] = true;
|
28 | });
|
29 | var hop = Object.prototype.hasOwnProperty;
|
30 |
|
31 | function inScope(name, scope) {
|
32 | for (var cur = scope; cur; cur = cur.prev)
|
33 | if (hop.call(cur.vars, name)) return true;
|
34 | }
|
35 | function 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 | }
|
43 | function 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 |
|
51 | walk.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);
|