MCPcopy Create free account
hub / github.com/observablehq/framework / findDeclarations

Function findDeclarations

src/javascript/declarations.ts:5–51  ·  view source on GitHub ↗
(node: Program, input: string)

Source from the content-addressed store, hash-verified

3import {syntaxError} from "./syntaxError.js";
4
5export function findDeclarations(node: Program, input: string): Identifier[] {
6 const declarations: Identifier[] = [];
7
8 function declareLocal(node: Identifier) {
9 if (defaultGlobals.has(node.name) || node.name === "arguments") {
10 throw syntaxError(`Global '${node.name}' cannot be redefined`, node, input);
11 }
12 declarations.push(node);
13 }
14
15 function declarePattern(node: Pattern) {
16 switch (node.type) {
17 case "Identifier":
18 declareLocal(node);
19 break;
20 case "ObjectPattern":
21 node.properties.forEach((node) => declarePattern(node.type === "Property" ? node.value : node));
22 break;
23 case "ArrayPattern":
24 node.elements.forEach((node) => node && declarePattern(node));
25 break;
26 case "RestElement":
27 declarePattern(node.argument);
28 break;
29 case "AssignmentPattern":
30 declarePattern(node.left);
31 break;
32 }
33 }
34
35 for (const child of node.body) {
36 switch (child.type) {
37 case "VariableDeclaration":
38 child.declarations.forEach((node) => declarePattern(node.id));
39 break;
40 case "ClassDeclaration":
41 case "FunctionDeclaration":
42 declareLocal(child.id);
43 break;
44 case "ImportDeclaration":
45 child.specifiers.forEach((node) => declareLocal(node.local));
46 break;
47 }
48 }
49
50 return declarations;
51}

Callers 1

parseJavaScriptFunction · 0.85

Calls 2

declarePatternFunction · 0.70
declareLocalFunction · 0.70

Tested by

no test coverage detected