MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / collectDeclaredIdentifiers

Function collectDeclaredIdentifiers

packages/js-runtime/src/ast-walker.ts:45–78  ·  view source on GitHub ↗
(ast: unknown)

Source from the content-addressed store, hash-verified

43 * Track declared variables/parameters to know what identifiers are "local"
44 */
45export function collectDeclaredIdentifiers(ast: unknown): Set<string> {
46 const declared = new Set<string>();
47
48 walkNode(ast, (node) => {
49 // Variable declarations: const x = ..., let y = ..., var z = ...
50 if (node.type === 'VariableDeclarator') {
51 const id = node.id as Record<string, unknown>;
52 if (id.type === 'Identifier') {
53 declared.add(id.name as string);
54 }
55 // Handle destructuring patterns
56 if (id.type === 'ObjectPattern') {
57 collectPatternIdentifiers(id, declared);
58 }
59 if (id.type === 'ArrayPattern') {
60 collectPatternIdentifiers(id, declared);
61 }
62 }
63
64 // Function parameters
65 if (
66 node.type === 'ArrowFunctionExpression' ||
67 node.type === 'FunctionExpression' ||
68 node.type === 'FunctionDeclaration'
69 ) {
70 const params = node.params as Array<Record<string, unknown>>;
71 for (const param of params) {
72 collectPatternIdentifiers(param, declared);
73 }
74 }
75 });
76
77 return declared;
78}
79
80/**
81 * Collect identifiers from destructuring patterns

Callers 1

validateFunction · 0.90

Calls 3

walkNodeFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected