(node: Program, input: string)
| 3 | import {syntaxError} from "./syntaxError.js"; |
| 4 | |
| 5 | export 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 | } |
no test coverage detected