1 |
|
2 |
|
3 | (function(mod) {
|
4 | if (typeof exports == "object" && typeof module == "object") return mod(exports);
|
5 | if (typeof define == "function" && define.amd) return define(["exports"], mod);
|
6 | mod((this.acorn || (this.acorn = {})).walk = {});
|
7 | })(function(exports) {
|
8 | "use strict";
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
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 |
|
36 |
|
37 |
|
38 |
|
39 |
|
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 |
|
60 |
|
61 |
|
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 |
|
84 |
|
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 |
|
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 |
|
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 |
|
137 |
|
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 |
|
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 |
|
274 |
|
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 | });
|