Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80552 views
1
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}(g.acorn || (g.acorn = {})).walk = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
2
"use strict";
3
4
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
5
6
// AST walker module for Mozilla Parser API compatible trees
7
8
// A simple walk is one where you simply specify callbacks to be
9
// called on specific nodes. The last two arguments are optional. A
10
// simple use would be
11
//
12
// walk.simple(myTree, {
13
// Expression: function(node) { ... }
14
// });
15
//
16
// to do something with all expressions. All Parser API node types
17
// can be used to identify node types, as well as Expression,
18
// Statement, and ScopeBody, which denote categories of nodes.
19
//
20
// The base argument can be used to pass a custom (recursive)
21
// walker, and state can be used to give this walked an initial
22
// state.
23
24
exports.simple = simple;
25
26
// An ancestor walk builds up an array of ancestor nodes (including
27
// the current node) and passes them to the callback as the state parameter.
28
exports.ancestor = ancestor;
29
30
// A recursive walk is one where your functions override the default
31
// walkers. They can modify and replace the state parameter that's
32
// threaded through the walk, and can opt how and whether to walk
33
// their child nodes (by calling their third argument on these
34
// nodes).
35
exports.recursive = recursive;
36
37
// Find a node with a given start, end, and type (all are optional,
38
// null can be used as wildcard). Returns a {node, state} object, or
39
// undefined when it doesn't find a matching node.
40
exports.findNodeAt = findNodeAt;
41
42
// Find the innermost node of a given type that contains the given
43
// position. Interface similar to findNodeAt.
44
exports.findNodeAround = findNodeAround;
45
46
// Find the outermost matching node after a given position.
47
exports.findNodeAfter = findNodeAfter;
48
49
// Find the outermost matching node before a given position.
50
exports.findNodeBefore = findNodeBefore;
51
52
// Used to create a custom walker. Will fill in all missing node
53
// type properties with the defaults.
54
exports.make = make;
55
exports.__esModule = true;
56
57
function simple(node, visitors, base, state) {
58
if (!base) base = exports.base;(function c(node, st, override) {
59
var type = override || node.type,
60
found = visitors[type];
61
base[type](node, st, c);
62
if (found) found(node, st);
63
})(node, state);
64
}
65
66
function ancestor(node, visitors, base, state) {
67
if (!base) base = exports.base;
68
if (!state) state = [];(function c(node, st, override) {
69
var type = override || node.type,
70
found = visitors[type];
71
if (node != st[st.length - 1]) {
72
st = st.slice();
73
st.push(node);
74
}
75
base[type](node, st, c);
76
if (found) found(node, st);
77
})(node, state);
78
}
79
80
function recursive(node, state, funcs, base) {
81
var visitor = funcs ? exports.make(funcs, base) : base;(function c(node, st, override) {
82
visitor[override || node.type](node, st, c);
83
})(node, state);
84
}
85
86
function makeTest(test) {
87
if (typeof test == "string") {
88
return function (type) {
89
return type == test;
90
};
91
} else if (!test) {
92
return function () {
93
return true;
94
};
95
} else {
96
return test;
97
}
98
}
99
100
var Found = function Found(node, state) {
101
_classCallCheck(this, Found);
102
103
this.node = node;this.state = state;
104
};
105
106
function findNodeAt(node, start, end, test, base, state) {
107
test = makeTest(test);
108
if (!base) base = exports.base;
109
try {
110
;(function c(node, st, override) {
111
var type = override || node.type;
112
if ((start == null || node.start <= start) && (end == null || node.end >= end)) base[type](node, st, c);
113
if (test(type, node) && (start == null || node.start == start) && (end == null || node.end == end)) throw new Found(node, st);
114
})(node, state);
115
} catch (e) {
116
if (e instanceof Found) {
117
return e;
118
}throw e;
119
}
120
}
121
122
function findNodeAround(node, pos, test, base, state) {
123
test = makeTest(test);
124
if (!base) base = exports.base;
125
try {
126
;(function c(node, st, override) {
127
var type = override || node.type;
128
if (node.start > pos || node.end < pos) {
129
return;
130
}base[type](node, st, c);
131
if (test(type, node)) throw new Found(node, st);
132
})(node, state);
133
} catch (e) {
134
if (e instanceof Found) {
135
return e;
136
}throw e;
137
}
138
}
139
140
function findNodeAfter(node, pos, test, base, state) {
141
test = makeTest(test);
142
if (!base) base = exports.base;
143
try {
144
;(function c(node, st, override) {
145
if (node.end < pos) {
146
return;
147
}var type = override || node.type;
148
if (node.start >= pos && test(type, node)) throw new Found(node, st);
149
base[type](node, st, c);
150
})(node, state);
151
} catch (e) {
152
if (e instanceof Found) {
153
return e;
154
}throw e;
155
}
156
}
157
158
function findNodeBefore(node, pos, test, base, state) {
159
test = makeTest(test);
160
if (!base) base = exports.base;
161
var max = undefined;(function c(node, st, override) {
162
if (node.start > pos) {
163
return;
164
}var type = override || node.type;
165
if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node)) max = new Found(node, st);
166
base[type](node, st, c);
167
})(node, state);
168
return max;
169
}
170
171
function make(funcs, base) {
172
if (!base) base = exports.base;
173
var visitor = {};
174
for (var type in base) visitor[type] = base[type];
175
for (var type in funcs) visitor[type] = funcs[type];
176
return visitor;
177
}
178
179
function skipThrough(node, st, c) {
180
c(node, st);
181
}
182
function ignore(_node, _st, _c) {}
183
184
// Node walkers.
185
186
var base = {};
187
188
exports.base = base;
189
base.Program = base.BlockStatement = function (node, st, c) {
190
for (var i = 0; i < node.body.length; ++i) {
191
c(node.body[i], st, "Statement");
192
}
193
};
194
base.Statement = skipThrough;
195
base.EmptyStatement = ignore;
196
base.ExpressionStatement = base.ParenthesizedExpression = function (node, st, c) {
197
return c(node.expression, st, "Expression");
198
};
199
base.IfStatement = function (node, st, c) {
200
c(node.test, st, "Expression");
201
c(node.consequent, st, "Statement");
202
if (node.alternate) c(node.alternate, st, "Statement");
203
};
204
base.LabeledStatement = function (node, st, c) {
205
return c(node.body, st, "Statement");
206
};
207
base.BreakStatement = base.ContinueStatement = ignore;
208
base.WithStatement = function (node, st, c) {
209
c(node.object, st, "Expression");
210
c(node.body, st, "Statement");
211
};
212
base.SwitchStatement = function (node, st, c) {
213
c(node.discriminant, st, "Expression");
214
for (var i = 0; i < node.cases.length; ++i) {
215
var cs = node.cases[i];
216
if (cs.test) c(cs.test, st, "Expression");
217
for (var j = 0; j < cs.consequent.length; ++j) {
218
c(cs.consequent[j], st, "Statement");
219
}
220
}
221
};
222
base.ReturnStatement = base.YieldExpression = function (node, st, c) {
223
if (node.argument) c(node.argument, st, "Expression");
224
};
225
base.ThrowStatement = base.SpreadElement = base.RestElement = function (node, st, c) {
226
return c(node.argument, st, "Expression");
227
};
228
base.TryStatement = function (node, st, c) {
229
c(node.block, st, "Statement");
230
if (node.handler) c(node.handler.body, st, "ScopeBody");
231
if (node.finalizer) c(node.finalizer, st, "Statement");
232
};
233
base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
234
c(node.test, st, "Expression");
235
c(node.body, st, "Statement");
236
};
237
base.ForStatement = function (node, st, c) {
238
if (node.init) c(node.init, st, "ForInit");
239
if (node.test) c(node.test, st, "Expression");
240
if (node.update) c(node.update, st, "Expression");
241
c(node.body, st, "Statement");
242
};
243
base.ForInStatement = base.ForOfStatement = function (node, st, c) {
244
c(node.left, st, "ForInit");
245
c(node.right, st, "Expression");
246
c(node.body, st, "Statement");
247
};
248
base.ForInit = function (node, st, c) {
249
if (node.type == "VariableDeclaration") c(node, st);else c(node, st, "Expression");
250
};
251
base.DebuggerStatement = ignore;
252
253
base.FunctionDeclaration = function (node, st, c) {
254
return c(node, st, "Function");
255
};
256
base.VariableDeclaration = function (node, st, c) {
257
for (var i = 0; i < node.declarations.length; ++i) {
258
var decl = node.declarations[i];
259
if (decl.init) c(decl.init, st, "Expression");
260
}
261
};
262
263
base.Function = function (node, st, c) {
264
return c(node.body, st, "ScopeBody");
265
};
266
base.ScopeBody = function (node, st, c) {
267
return c(node, st, "Statement");
268
};
269
270
base.Expression = skipThrough;
271
base.ThisExpression = base.Super = base.MetaProperty = ignore;
272
base.ArrayExpression = base.ArrayPattern = function (node, st, c) {
273
for (var i = 0; i < node.elements.length; ++i) {
274
var elt = node.elements[i];
275
if (elt) c(elt, st, "Expression");
276
}
277
};
278
base.ObjectExpression = base.ObjectPattern = function (node, st, c) {
279
for (var i = 0; i < node.properties.length; ++i) {
280
c(node.properties[i], st);
281
}
282
};
283
base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
284
base.SequenceExpression = base.TemplateLiteral = function (node, st, c) {
285
for (var i = 0; i < node.expressions.length; ++i) {
286
c(node.expressions[i], st, "Expression");
287
}
288
};
289
base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
290
c(node.argument, st, "Expression");
291
};
292
base.BinaryExpression = base.AssignmentExpression = base.AssignmentPattern = base.LogicalExpression = function (node, st, c) {
293
c(node.left, st, "Expression");
294
c(node.right, st, "Expression");
295
};
296
base.ConditionalExpression = function (node, st, c) {
297
c(node.test, st, "Expression");
298
c(node.consequent, st, "Expression");
299
c(node.alternate, st, "Expression");
300
};
301
base.NewExpression = base.CallExpression = function (node, st, c) {
302
c(node.callee, st, "Expression");
303
if (node.arguments) for (var i = 0; i < node.arguments.length; ++i) {
304
c(node.arguments[i], st, "Expression");
305
}
306
};
307
base.MemberExpression = function (node, st, c) {
308
c(node.object, st, "Expression");
309
if (node.computed) c(node.property, st, "Expression");
310
};
311
base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
312
return c(node.declaration, st);
313
};
314
base.ImportDeclaration = function (node, st, c) {
315
for (var i = 0; i < node.specifiers.length; i++) {
316
c(node.specifiers[i], st);
317
}
318
};
319
base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;
320
321
base.TaggedTemplateExpression = function (node, st, c) {
322
c(node.tag, st, "Expression");
323
c(node.quasi, st);
324
};
325
base.ClassDeclaration = base.ClassExpression = function (node, st, c) {
326
if (node.superClass) c(node.superClass, st, "Expression");
327
for (var i = 0; i < node.body.body.length; i++) {
328
c(node.body.body[i], st);
329
}
330
};
331
base.MethodDefinition = base.Property = function (node, st, c) {
332
if (node.computed) c(node.key, st, "Expression");
333
c(node.value, st, "Expression");
334
};
335
base.ComprehensionExpression = function (node, st, c) {
336
for (var i = 0; i < node.blocks.length; i++) {
337
c(node.blocks[i].right, st, "Expression");
338
}c(node.body, st, "Expression");
339
};
340
341
},{}]},{},[1])(1)
342
});
343