/
ArrayPattern.ts
53 lines (47 loc) · 1.66 KB
/
ArrayPattern.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { HasEffectsContext } from '../ExecutionContext';
import { EMPTY_PATH, type ObjectPath } from '../utils/PathTracker';
import type LocalVariable from '../variables/LocalVariable';
import type Variable from '../variables/Variable';
import type * as NodeType from './NodeType';
import { UNKNOWN_EXPRESSION } from './shared/Expression';
import { NodeBase } from './shared/Node';
import type { PatternNode } from './shared/Pattern';
export default class ArrayPattern extends NodeBase implements PatternNode {
declare elements: (PatternNode | null)[];
declare type: NodeType.tArrayPattern;
addExportedVariables(
variables: readonly Variable[],
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void {
for (const element of this.elements) {
element?.addExportedVariables(variables, exportNamesByVariable);
}
}
declare(kind: string): LocalVariable[] {
const variables: LocalVariable[] = [];
for (const element of this.elements) {
if (element !== null) {
variables.push(...element.declare(kind, UNKNOWN_EXPRESSION));
}
}
return variables;
}
// Patterns can only be deoptimized at the empty path at the moment
deoptimizePath(): void {
for (const element of this.elements) {
element?.deoptimizePath(EMPTY_PATH);
}
}
// Patterns are only checked at the emtpy path at the moment
hasEffectsWhenAssignedAtPath(_path: ObjectPath, context: HasEffectsContext): boolean {
for (const element of this.elements) {
if (element?.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context)) return true;
}
return false;
}
markDeclarationReached(): void {
for (const element of this.elements) {
element?.markDeclarationReached();
}
}
}