UNPKG

10.7 kBJavaScriptView Raw
1// AST walker module for Mozilla Parser API compatible trees
2
3(function(mod) {
4 if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
5 if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
6 mod((this.acorn || (this.acorn = {})).walk = {}); // Plain browser env
7})(function(exports) {
8 "use strict";
9
10 // A simple walk is one where you simply specify callbacks to be
11 // called on specific nodes. The last two arguments are optional. A
12 // simple use would be
13 //
14 // walk.simple(myTree, {
15 // Expression: function(node) { ... }
16 // });
17 //
18 // to do something with all expressions. All Parser API node types
19 // can be used to identify node types, as well as Expression,
20 // Statement, and ScopeBody, which denote categories of nodes.
21 //
22 // The base argument can be used to pass a custom (recursive)
23 // walker, and state can be used to give this walked an initial
24 // state.
25 exports.simple = function(node, visitors, base, state) {
26 if (!base) base = exports.base;
27 function c(node, st, override) {
28 var type = override || node.type, found = visitors[type];
29 base[type](node, st, c);
30 if (found) found(node, st);
31 }
32 c(node, state);
33 };
34
35 // A recursive walk is one where your functions override the default
36 // walkers. They can modify and replace the state parameter that's
37 // threaded through the walk, and can opt how and whether to walk
38 // their child nodes (by calling their third argument on these
39 // nodes).
40 exports.recursive = function(node, state, funcs, base) {
41 var visitor = funcs ? exports.make(funcs, base) : base;
42 function c(node, st, override) {
43 visitor[override || node.type](node, st, c);
44 }
45 c(node, state);
46 };
47
48 function makeTest(test) {
49 if (typeof test == "string")
50 return function(type) { return type == test; };
51 else if (!test)
52 return function() { return true; };
53 else
54 return test;
55 }
56
57 function Found(node, state) { this.node = node; this.state = state; }
58
59 // Find a node with a given start, end, and type (all are optional,
60 // null can be used as wildcard). Returns a {node, state} object, or
61 // undefined when it doesn't find a matching node.
62 exports.findNodeAt = function(node, start, end, test, base, state) {
63 test = makeTest(test);
64 try {
65 if (!base) base = exports.base;
66 var c = function(node, st, override) {
67 var type = override || node.type;
68 if ((start == null || node.start <= start) &&
69 (end == null || node.end >= end))
70 base[type](node, st, c);
71 if (test(type, node) &&
72 (start == null || node.start == start) &&
73 (end == null || node.end == end))
74 throw new Found(node, st);
75 };
76 c(node, state);
77 } catch (e) {
78 if (e instanceof Found) return e;
79 throw e;
80 }
81 };
82
83 // Find the innermost node of a given type that contains the given
84 // position. Interface similar to findNodeAt.
85 exports.findNodeAround = function(node, pos, test, base, state) {
86 test = makeTest(test);
87 try {
88 if (!base) base = exports.base;
89 var c = function(node, st, override) {
90 var type = override || node.type;
91 if (node.start > pos || node.end < pos) return;
92 base[type](node, st, c);
93 if (test(type, node)) throw new Found(node, st);
94 };
95 c(node, state);
96 } catch (e) {
97 if (e instanceof Found) return e;
98 throw e;
99 }
100 };
101
102 // Find the outermost matching node after a given position.
103 exports.findNodeAfter = function(node, pos, test, base, state) {
104 test = makeTest(test);
105 try {
106 if (!base) base = exports.base;
107 var c = function(node, st, override) {
108 if (node.end < pos) return;
109 var type = override || node.type;
110 if (node.start >= pos && test(type, node)) throw new Found(node, st);
111 base[type](node, st, c);
112 };
113 c(node, state);
114 } catch (e) {
115 if (e instanceof Found) return e;
116 throw e;
117 }
118 };
119
120 // Find the outermost matching node before a given position.
121 exports.findNodeBefore = function(node, pos, test, base, state) {
122 test = makeTest(test);
123 if (!base) base = exports.base;
124 var max;
125 var c = function(node, st, override) {
126 if (node.start > pos) return;
127 var type = override || node.type;
128 if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
129 max = new Found(node, st);
130 base[type](node, st, c);
131 };
132 c(node, state);
133 return max;
134 };
135
136 // Used to create a custom walker. Will fill in all missing node
137 // type properties with the defaults.
138 exports.make = function(funcs, base) {
139 if (!base) base = exports.base;
140 var visitor = {};
141 for (var type in base) visitor[type] = base[type];
142 for (var type in funcs) visitor[type] = funcs[type];
143 return visitor;
144 };
145
146 function skipThrough(node, st, c) { c(node, st); }
147 function ignore(node, st, c) {}
148
149 // Node walkers.
150
151 var base = exports.base = {};
152 base.Program = base.BlockStatement = function(node, st, c) {
153 for (var i = 0; i < node.body.length; ++i)
154 c(node.body[i], st, "Statement");
155 };
156 base.Statement = skipThrough;
157 base.EmptyStatement = ignore;
158 base.ExpressionStatement = function(node, st, c) {
159 c(node.expression, st, "Expression");
160 };
161 base.IfStatement = function(node, st, c) {
162 c(node.test, st, "Expression");
163 c(node.consequent, st, "Statement");
164 if (node.alternate) c(node.alternate, st, "Statement");
165 };
166 base.LabeledStatement = function(node, st, c) {
167 c(node.body, st, "Statement");
168 };
169 base.BreakStatement = base.ContinueStatement = ignore;
170 base.WithStatement = function(node, st, c) {
171 c(node.object, st, "Expression");
172 c(node.body, st, "Statement");
173 };
174 base.SwitchStatement = function(node, st, c) {
175 c(node.discriminant, st, "Expression");
176 for (var i = 0; i < node.cases.length; ++i) {
177 var cs = node.cases[i];
178 if (cs.test) c(cs.test, st, "Expression");
179 for (var j = 0; j < cs.consequent.length; ++j)
180 c(cs.consequent[j], st, "Statement");
181 }
182 };
183 base.ReturnStatement = function(node, st, c) {
184 if (node.argument) c(node.argument, st, "Expression");
185 };
186 base.ThrowStatement = function(node, st, c) {
187 c(node.argument, st, "Expression");
188 };
189 base.TryStatement = function(node, st, c) {
190 c(node.block, st, "Statement");
191 if (node.handler) c(node.handler.body, st, "ScopeBody");
192 if (node.finalizer) c(node.finalizer, st, "Statement");
193 };
194 base.WhileStatement = function(node, st, c) {
195 c(node.test, st, "Expression");
196 c(node.body, st, "Statement");
197 };
198 base.DoWhileStatement = base.WhileStatement;
199 base.ForStatement = function(node, st, c) {
200 if (node.init) c(node.init, st, "ForInit");
201 if (node.test) c(node.test, st, "Expression");
202 if (node.update) c(node.update, st, "Expression");
203 c(node.body, st, "Statement");
204 };
205 base.ForInStatement = function(node, st, c) {
206 c(node.left, st, "ForInit");
207 c(node.right, st, "Expression");
208 c(node.body, st, "Statement");
209 };
210 base.ForInit = function(node, st, c) {
211 if (node.type == "VariableDeclaration") c(node, st);
212 else c(node, st, "Expression");
213 };
214 base.DebuggerStatement = ignore;
215
216 base.FunctionDeclaration = function(node, st, c) {
217 c(node, st, "Function");
218 };
219 base.VariableDeclaration = function(node, st, c) {
220 for (var i = 0; i < node.declarations.length; ++i) {
221 var decl = node.declarations[i];
222 if (decl.init) c(decl.init, st, "Expression");
223 }
224 };
225
226 base.Function = function(node, st, c) {
227 c(node.body, st, "ScopeBody");
228 };
229 base.ScopeBody = function(node, st, c) {
230 c(node, st, "Statement");
231 };
232
233 base.Expression = skipThrough;
234 base.ThisExpression = ignore;
235 base.ArrayExpression = function(node, st, c) {
236 for (var i = 0; i < node.elements.length; ++i) {
237 var elt = node.elements[i];
238 if (elt) c(elt, st, "Expression");
239 }
240 };
241 base.ObjectExpression = function(node, st, c) {
242 for (var i = 0; i < node.properties.length; ++i)
243 c(node.properties[i].value, st, "Expression");
244 };
245 base.FunctionExpression = base.FunctionDeclaration;
246 base.SequenceExpression = function(node, st, c) {
247 for (var i = 0; i < node.expressions.length; ++i)
248 c(node.expressions[i], st, "Expression");
249 };
250 base.UnaryExpression = base.UpdateExpression = function(node, st, c) {
251 c(node.argument, st, "Expression");
252 };
253 base.BinaryExpression = base.AssignmentExpression = base.LogicalExpression = function(node, st, c) {
254 c(node.left, st, "Expression");
255 c(node.right, st, "Expression");
256 };
257 base.ConditionalExpression = function(node, st, c) {
258 c(node.test, st, "Expression");
259 c(node.consequent, st, "Expression");
260 c(node.alternate, st, "Expression");
261 };
262 base.NewExpression = base.CallExpression = function(node, st, c) {
263 c(node.callee, st, "Expression");
264 if (node.arguments) for (var i = 0; i < node.arguments.length; ++i)
265 c(node.arguments[i], st, "Expression");
266 };
267 base.MemberExpression = function(node, st, c) {
268 c(node.object, st, "Expression");
269 if (node.computed) c(node.property, st, "Expression");
270 };
271 base.Identifier = base.Literal = ignore;
272
273 // A custom walker that keeps track of the scope chain and the
274 // variables defined in it.
275 function makeScope(prev) {
276 return {vars: Object.create(null), prev: prev};
277 }
278 exports.scopeVisitor = exports.make({
279 Function: function(node, scope, c) {
280 var inner = makeScope(scope);
281 for (var i = 0; i < node.params.length; ++i)
282 inner.vars[node.params[i].name] = {type: "argument", node: node.params[i]};
283 if (node.id) {
284 var decl = node.type == "FunctionDeclaration";
285 (decl ? scope : inner).vars[node.id.name] =
286 {type: decl ? "function" : "function name", node: node.id};
287 }
288 c(node.body, inner, "ScopeBody");
289 },
290 TryStatement: function(node, scope, c) {
291 c(node.block, scope, "Statement");
292 if (node.handler) {
293 var inner = makeScope(scope);
294 inner.vars[node.handler.param.name] = {type: "catch clause", node: node.handler.param};
295 c(node.handler.body, inner, "ScopeBody");
296 }
297 if (node.finalizer) c(node.finalizer, scope, "Statement");
298 },
299 VariableDeclaration: function(node, scope, c) {
300 for (var i = 0; i < node.declarations.length; ++i) {
301 var decl = node.declarations[i];
302 scope.vars[decl.id.name] = {type: "var", node: decl.id};
303 if (decl.init) c(decl.init, scope, "Expression");
304 }
305 }
306 });
307
308});