project-graph-mcp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,437 @@
1
+ // AST walker module for ESTree 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 ESTree node types
12
+ // can be used to identify node types, as well as Expression and
13
+ // Statement, 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
+ function simple(node, visitors, baseVisitor, state, override) {
20
+ if (!baseVisitor) { baseVisitor = base
21
+ ; }(function c(node, st, override) {
22
+ var type = override || node.type;
23
+ baseVisitor[type](node, st, c);
24
+ if (visitors[type]) { visitors[type](node, st); }
25
+ })(node, state, override);
26
+ }
27
+
28
+ // An ancestor walk keeps an array of ancestor nodes (including the
29
+ // current node) and passes them to the callback as third parameter
30
+ // (and also as state parameter when no other state is present).
31
+ function ancestor(node, visitors, baseVisitor, state, override) {
32
+ var ancestors = [];
33
+ if (!baseVisitor) { baseVisitor = base
34
+ ; }(function c(node, st, override) {
35
+ var type = override || node.type;
36
+ var isNew = node !== ancestors[ancestors.length - 1];
37
+ if (isNew) { ancestors.push(node); }
38
+ baseVisitor[type](node, st, c);
39
+ if (visitors[type]) { visitors[type](node, st || ancestors, ancestors); }
40
+ if (isNew) { ancestors.pop(); }
41
+ })(node, state, override);
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
+ function recursive(node, state, funcs, baseVisitor, override) {
50
+ var visitor = funcs ? make(funcs, baseVisitor || undefined) : baseVisitor
51
+ ;(function c(node, st, override) {
52
+ visitor[override || node.type](node, st, c);
53
+ })(node, state, override);
54
+ }
55
+
56
+ function makeTest(test) {
57
+ if (typeof test === "string")
58
+ { return function (type) { return type === test; } }
59
+ else if (!test)
60
+ { return function () { return true; } }
61
+ else
62
+ { return test }
63
+ }
64
+
65
+ var Found = function Found(node, state) { this.node = node; this.state = state; };
66
+
67
+ // A full walk triggers the callback on each node
68
+ function full(node, callback, baseVisitor, state, override) {
69
+ if (!baseVisitor) { baseVisitor = base; }
70
+ var last
71
+ ;(function c(node, st, override) {
72
+ var type = override || node.type;
73
+ baseVisitor[type](node, st, c);
74
+ if (last !== node) {
75
+ callback(node, st, type);
76
+ last = node;
77
+ }
78
+ })(node, state, override);
79
+ }
80
+
81
+ // An fullAncestor walk is like an ancestor walk, but triggers
82
+ // the callback on each node
83
+ function fullAncestor(node, callback, baseVisitor, state) {
84
+ if (!baseVisitor) { baseVisitor = base; }
85
+ var ancestors = [], last
86
+ ;(function c(node, st, override) {
87
+ var type = override || node.type;
88
+ var isNew = node !== ancestors[ancestors.length - 1];
89
+ if (isNew) { ancestors.push(node); }
90
+ baseVisitor[type](node, st, c);
91
+ if (last !== node) {
92
+ callback(node, st || ancestors, ancestors, type);
93
+ last = node;
94
+ }
95
+ if (isNew) { ancestors.pop(); }
96
+ })(node, state);
97
+ }
98
+
99
+ // Find a node with a given start, end, and type (all are optional,
100
+ // null can be used as wildcard). Returns a {node, state} object, or
101
+ // undefined when it doesn't find a matching node.
102
+ function findNodeAt(node, start, end, test, baseVisitor, state) {
103
+ if (!baseVisitor) { baseVisitor = base; }
104
+ test = makeTest(test);
105
+ try {
106
+ (function c(node, st, override) {
107
+ var type = override || node.type;
108
+ if ((start == null || node.start <= start) &&
109
+ (end == null || node.end >= end))
110
+ { baseVisitor[type](node, st, c); }
111
+ if ((start == null || node.start === start) &&
112
+ (end == null || node.end === end) &&
113
+ test(type, node))
114
+ { throw new Found(node, st) }
115
+ })(node, state);
116
+ } catch (e) {
117
+ if (e instanceof Found) { return e }
118
+ throw e
119
+ }
120
+ }
121
+
122
+ // Find the innermost node of a given type that contains the given
123
+ // position. Interface similar to findNodeAt.
124
+ function findNodeAround(node, pos, test, baseVisitor, state) {
125
+ test = makeTest(test);
126
+ if (!baseVisitor) { baseVisitor = base; }
127
+ try {
128
+ (function c(node, st, override) {
129
+ var type = override || node.type;
130
+ if (node.start > pos || node.end < pos) { return }
131
+ baseVisitor[type](node, st, c);
132
+ if (test(type, node)) { throw new Found(node, st) }
133
+ })(node, state);
134
+ } catch (e) {
135
+ if (e instanceof Found) { return e }
136
+ throw e
137
+ }
138
+ }
139
+
140
+ // Find the outermost matching node after a given position.
141
+ function findNodeAfter(node, pos, test, baseVisitor, state) {
142
+ test = makeTest(test);
143
+ if (!baseVisitor) { baseVisitor = base; }
144
+ try {
145
+ (function c(node, st, override) {
146
+ if (node.end < pos) { return }
147
+ var type = override || node.type;
148
+ if (node.start >= pos && test(type, node)) { throw new Found(node, st) }
149
+ baseVisitor[type](node, st, c);
150
+ })(node, state);
151
+ } catch (e) {
152
+ if (e instanceof Found) { return e }
153
+ throw e
154
+ }
155
+ }
156
+
157
+ // Find the outermost matching node before a given position.
158
+ function findNodeBefore(node, pos, test, baseVisitor, state) {
159
+ test = makeTest(test);
160
+ if (!baseVisitor) { baseVisitor = base; }
161
+ var max
162
+ ;(function c(node, st, override) {
163
+ if (node.start > pos) { return }
164
+ var type = override || node.type;
165
+ if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
166
+ { max = new Found(node, st); }
167
+ baseVisitor[type](node, st, c);
168
+ })(node, state);
169
+ return max
170
+ }
171
+
172
+ // Used to create a custom walker. Will fill in all missing node
173
+ // type properties with the defaults.
174
+ function make(funcs, baseVisitor) {
175
+ var visitor = Object.create(baseVisitor || base);
176
+ for (var type in funcs) { visitor[type] = funcs[type]; }
177
+ return visitor
178
+ }
179
+
180
+ function skipThrough(node, st, c) { c(node, st); }
181
+ function ignore(_node, _st, _c) {}
182
+
183
+ // Node walkers.
184
+
185
+ var base = {};
186
+
187
+ base.Program = base.BlockStatement = base.StaticBlock = function (node, st, c) {
188
+ for (var i = 0, list = node.body; i < list.length; i += 1)
189
+ {
190
+ var stmt = list[i];
191
+
192
+ c(stmt, st, "Statement");
193
+ }
194
+ };
195
+ base.Statement = skipThrough;
196
+ base.EmptyStatement = ignore;
197
+ base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression =
198
+ function (node, st, c) { return c(node.expression, st, "Expression"); };
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) { return c(node.body, st, "Statement"); };
205
+ base.BreakStatement = base.ContinueStatement = ignore;
206
+ base.WithStatement = function (node, st, c) {
207
+ c(node.object, st, "Expression");
208
+ c(node.body, st, "Statement");
209
+ };
210
+ base.SwitchStatement = function (node, st, c) {
211
+ c(node.discriminant, st, "Expression");
212
+ for (var i = 0, list = node.cases; i < list.length; i += 1) {
213
+ var cs = list[i];
214
+
215
+ c(cs, st);
216
+ }
217
+ };
218
+ base.SwitchCase = function (node, st, c) {
219
+ if (node.test) { c(node.test, st, "Expression"); }
220
+ for (var i = 0, list = node.consequent; i < list.length; i += 1)
221
+ {
222
+ var cons = list[i];
223
+
224
+ c(cons, st, "Statement");
225
+ }
226
+ };
227
+ base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
228
+ if (node.argument) { c(node.argument, st, "Expression"); }
229
+ };
230
+ base.ThrowStatement = base.SpreadElement =
231
+ function (node, st, c) { return c(node.argument, st, "Expression"); };
232
+ base.TryStatement = function (node, st, c) {
233
+ c(node.block, st, "Statement");
234
+ if (node.handler) { c(node.handler, st); }
235
+ if (node.finalizer) { c(node.finalizer, st, "Statement"); }
236
+ };
237
+ base.CatchClause = function (node, st, c) {
238
+ if (node.param) { c(node.param, st, "Pattern"); }
239
+ c(node.body, st, "Statement");
240
+ };
241
+ base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
242
+ c(node.test, st, "Expression");
243
+ c(node.body, st, "Statement");
244
+ };
245
+ base.ForStatement = function (node, st, c) {
246
+ if (node.init) { c(node.init, st, "ForInit"); }
247
+ if (node.test) { c(node.test, st, "Expression"); }
248
+ if (node.update) { c(node.update, st, "Expression"); }
249
+ c(node.body, st, "Statement");
250
+ };
251
+ base.ForInStatement = base.ForOfStatement = function (node, st, c) {
252
+ c(node.left, st, "ForInit");
253
+ c(node.right, st, "Expression");
254
+ c(node.body, st, "Statement");
255
+ };
256
+ base.ForInit = function (node, st, c) {
257
+ if (node.type === "VariableDeclaration") { c(node, st); }
258
+ else { c(node, st, "Expression"); }
259
+ };
260
+ base.DebuggerStatement = ignore;
261
+
262
+ base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
263
+ base.VariableDeclaration = function (node, st, c) {
264
+ for (var i = 0, list = node.declarations; i < list.length; i += 1)
265
+ {
266
+ var decl = list[i];
267
+
268
+ c(decl, st);
269
+ }
270
+ };
271
+ base.VariableDeclarator = function (node, st, c) {
272
+ c(node.id, st, "Pattern");
273
+ if (node.init) { c(node.init, st, "Expression"); }
274
+ };
275
+
276
+ base.Function = function (node, st, c) {
277
+ if (node.id) { c(node.id, st, "Pattern"); }
278
+ for (var i = 0, list = node.params; i < list.length; i += 1)
279
+ {
280
+ var param = list[i];
281
+
282
+ c(param, st, "Pattern");
283
+ }
284
+ c(node.body, st, node.expression ? "Expression" : "Statement");
285
+ };
286
+
287
+ base.Pattern = function (node, st, c) {
288
+ if (node.type === "Identifier")
289
+ { c(node, st, "VariablePattern"); }
290
+ else if (node.type === "MemberExpression")
291
+ { c(node, st, "MemberPattern"); }
292
+ else
293
+ { c(node, st); }
294
+ };
295
+ base.VariablePattern = ignore;
296
+ base.MemberPattern = skipThrough;
297
+ base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
298
+ base.ArrayPattern = function (node, st, c) {
299
+ for (var i = 0, list = node.elements; i < list.length; i += 1) {
300
+ var elt = list[i];
301
+
302
+ if (elt) { c(elt, st, "Pattern"); }
303
+ }
304
+ };
305
+ base.ObjectPattern = function (node, st, c) {
306
+ for (var i = 0, list = node.properties; i < list.length; i += 1) {
307
+ var prop = list[i];
308
+
309
+ if (prop.type === "Property") {
310
+ if (prop.computed) { c(prop.key, st, "Expression"); }
311
+ c(prop.value, st, "Pattern");
312
+ } else if (prop.type === "RestElement") {
313
+ c(prop.argument, st, "Pattern");
314
+ }
315
+ }
316
+ };
317
+
318
+ base.Expression = skipThrough;
319
+ base.ThisExpression = base.Super = base.MetaProperty = ignore;
320
+ base.ArrayExpression = function (node, st, c) {
321
+ for (var i = 0, list = node.elements; i < list.length; i += 1) {
322
+ var elt = list[i];
323
+
324
+ if (elt) { c(elt, st, "Expression"); }
325
+ }
326
+ };
327
+ base.ObjectExpression = function (node, st, c) {
328
+ for (var i = 0, list = node.properties; i < list.length; i += 1)
329
+ {
330
+ var prop = list[i];
331
+
332
+ c(prop, st);
333
+ }
334
+ };
335
+ base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
336
+ base.SequenceExpression = function (node, st, c) {
337
+ for (var i = 0, list = node.expressions; i < list.length; i += 1)
338
+ {
339
+ var expr = list[i];
340
+
341
+ c(expr, st, "Expression");
342
+ }
343
+ };
344
+ base.TemplateLiteral = function (node, st, c) {
345
+ for (var i = 0, list = node.quasis; i < list.length; i += 1)
346
+ {
347
+ var quasi = list[i];
348
+
349
+ c(quasi, st);
350
+ }
351
+
352
+ for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)
353
+ {
354
+ var expr = list$1[i$1];
355
+
356
+ c(expr, st, "Expression");
357
+ }
358
+ };
359
+ base.TemplateElement = ignore;
360
+ base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
361
+ c(node.argument, st, "Expression");
362
+ };
363
+ base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
364
+ c(node.left, st, "Expression");
365
+ c(node.right, st, "Expression");
366
+ };
367
+ base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
368
+ c(node.left, st, "Pattern");
369
+ c(node.right, st, "Expression");
370
+ };
371
+ base.ConditionalExpression = function (node, st, c) {
372
+ c(node.test, st, "Expression");
373
+ c(node.consequent, st, "Expression");
374
+ c(node.alternate, st, "Expression");
375
+ };
376
+ base.NewExpression = base.CallExpression = function (node, st, c) {
377
+ c(node.callee, st, "Expression");
378
+ if (node.arguments)
379
+ { for (var i = 0, list = node.arguments; i < list.length; i += 1)
380
+ {
381
+ var arg = list[i];
382
+
383
+ c(arg, st, "Expression");
384
+ } }
385
+ };
386
+ base.MemberExpression = function (node, st, c) {
387
+ c(node.object, st, "Expression");
388
+ if (node.computed) { c(node.property, st, "Expression"); }
389
+ };
390
+ base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
391
+ if (node.declaration)
392
+ { c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
393
+ if (node.source) { c(node.source, st, "Expression"); }
394
+ };
395
+ base.ExportAllDeclaration = function (node, st, c) {
396
+ if (node.exported)
397
+ { c(node.exported, st); }
398
+ c(node.source, st, "Expression");
399
+ };
400
+ base.ImportDeclaration = function (node, st, c) {
401
+ for (var i = 0, list = node.specifiers; i < list.length; i += 1)
402
+ {
403
+ var spec = list[i];
404
+
405
+ c(spec, st);
406
+ }
407
+ c(node.source, st, "Expression");
408
+ };
409
+ base.ImportExpression = function (node, st, c) {
410
+ c(node.source, st, "Expression");
411
+ };
412
+ base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.PrivateIdentifier = base.Literal = ignore;
413
+
414
+ base.TaggedTemplateExpression = function (node, st, c) {
415
+ c(node.tag, st, "Expression");
416
+ c(node.quasi, st, "Expression");
417
+ };
418
+ base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
419
+ base.Class = function (node, st, c) {
420
+ if (node.id) { c(node.id, st, "Pattern"); }
421
+ if (node.superClass) { c(node.superClass, st, "Expression"); }
422
+ c(node.body, st);
423
+ };
424
+ base.ClassBody = function (node, st, c) {
425
+ for (var i = 0, list = node.body; i < list.length; i += 1)
426
+ {
427
+ var elt = list[i];
428
+
429
+ c(elt, st);
430
+ }
431
+ };
432
+ base.MethodDefinition = base.PropertyDefinition = base.Property = function (node, st, c) {
433
+ if (node.computed) { c(node.key, st, "Expression"); }
434
+ if (node.value) { c(node.value, st, "Expression"); }
435
+ };
436
+
437
+ export { ancestor, base, findNodeAfter, findNodeAround, findNodeAt, findNodeBefore, full, fullAncestor, make, recursive, simple };