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