|
1 | 1 | import type {Expression, Identifier, Node, Pattern, VariableDeclaration} from "acorn"; |
2 | 2 | import {defaultGlobals} from "./globals.js"; |
3 | 3 | import {syntaxError} from "./syntaxError.js"; |
4 | | -import {simple} from "./walk.js"; |
| 4 | +import {ancestor} from "./walk.js"; |
5 | 5 |
|
6 | 6 | type Assignable = Expression | Pattern | VariableDeclaration; |
7 | 7 |
|
8 | | -export function checkAssignments(node: Node, references: Identifier[], input: string): void { |
9 | | - function checkConst(node: Assignable) { |
| 8 | +export function checkAssignments( |
| 9 | + node: Node, |
| 10 | + { |
| 11 | + input, |
| 12 | + locals, |
| 13 | + references, |
| 14 | + globals = defaultGlobals |
| 15 | + }: { |
| 16 | + input: string; |
| 17 | + locals: Map<Node, Set<string>>; |
| 18 | + globals?: Set<string>; |
| 19 | + references: Identifier[]; |
| 20 | + } |
| 21 | +): void { |
| 22 | + function isLocal({name}: Identifier, parents: Node[]): boolean { |
| 23 | + for (const p of parents) if (locals.get(p)?.has(name)) return true; |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + function checkConst(node: Assignable, parents: Node[]) { |
10 | 28 | switch (node.type) { |
11 | 29 | case "Identifier": |
| 30 | + if (isLocal(node, parents)) break; |
12 | 31 | if (references.includes(node)) |
13 | 32 | throw syntaxError(`Assignment to external variable '${node.name}'`, node, input); |
14 | | - if (defaultGlobals.has(node.name)) |
| 33 | + if (globals.has(node.name)) |
15 | 34 | throw syntaxError(`Assignment to global '${node.name}'`, node, input); |
16 | 35 | break; |
17 | | - case "ObjectPattern": |
18 | | - node.properties.forEach((node) => checkConst(node.type === "Property" ? node.value : node)); |
19 | | - break; |
20 | 36 | case "ArrayPattern": |
21 | | - node.elements.forEach((node) => node && checkConst(node)); |
| 37 | + for (const e of node.elements) if (e) checkConst(e, parents); |
| 38 | + break; |
| 39 | + case "ObjectPattern": |
| 40 | + for (const p of node.properties) checkConst(p.type === "Property" ? p.value : p, parents); |
22 | 41 | break; |
23 | 42 | case "RestElement": |
24 | | - checkConst(node.argument); |
| 43 | + checkConst(node.argument, parents); |
25 | 44 | break; |
26 | 45 | } |
27 | 46 | } |
28 | | - function checkConstLeft({left}: {left: Assignable}) { |
29 | | - checkConst(left); |
| 47 | + |
| 48 | + function checkConstArgument({argument}: {argument: Assignable}, parents: Node[]) { |
| 49 | + checkConst(argument, parents); |
30 | 50 | } |
31 | | - function checkConstArgument({argument}: {argument: Assignable}) { |
32 | | - checkConst(argument); |
| 51 | + |
| 52 | + function checkConstLeft({left}: {left: Assignable}, parents: Node[]) { |
| 53 | + checkConst(left, parents); |
33 | 54 | } |
34 | | - simple(node, { |
| 55 | + |
| 56 | + ancestor(node, { |
35 | 57 | AssignmentExpression: checkConstLeft, |
36 | 58 | AssignmentPattern: checkConstLeft, |
37 | 59 | UpdateExpression: checkConstArgument, |
|
0 commit comments