-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ObjectPattern.ts
89 lines (82 loc) · 2.89 KB
/
ObjectPattern.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteractionAssigned } from '../NodeInteractions';
import { EMPTY_PATH, type ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
import type LocalVariable from '../variables/LocalVariable';
import type Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import Property from './Property';
import RestElement from './RestElement';
import VariableDeclarator from './VariableDeclarator';
import type { ExpressionEntity, InclusionOptions } from './shared/Expression';
import type { IncludeChildren } from './shared/Node';
import { NodeBase } from './shared/Node';
import type { PatternNode } from './shared/Pattern';
import type { VariableKind } from './shared/VariableKinds';
export default class ObjectPattern extends NodeBase implements PatternNode {
declare properties: readonly (Property | RestElement)[];
declare type: NodeType.tObjectPattern;
addExportedVariables(
variables: readonly Variable[],
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void {
for (const property of this.properties) {
if (property.type === NodeType.Property) {
(property.value as unknown as PatternNode).addExportedVariables(
variables,
exportNamesByVariable
);
} else {
property.argument.addExportedVariables(variables, exportNamesByVariable);
}
}
}
declare(kind: VariableKind, init: ExpressionEntity): LocalVariable[] {
const variables: LocalVariable[] = [];
for (const property of this.properties) {
variables.push(...property.declare(kind, init));
}
return variables;
}
deoptimizePath(path: ObjectPath): void {
if (path.length === 0) {
for (const property of this.properties) {
property.deoptimizePath(path);
}
}
}
includePath(
path: ObjectPath,
context: InclusionContext,
includeChildrenRecursively: IncludeChildren,
options?: InclusionOptions | undefined
): void {
super.includePath(path, context, includeChildrenRecursively, options);
if (this.parent instanceof VariableDeclarator) {
for (const p of this.properties) {
if (p instanceof Property) {
this.parent.init?.includePath([p.key.name], context, includeChildrenRecursively, options);
}
if (p instanceof RestElement) {
this.parent.init?.includePath(UNKNOWN_PATH, context, includeChildrenRecursively, options);
}
}
}
}
hasEffectsOnInteractionAtPath(
// At the moment, this is only triggered for assignment left-hand sides,
// where the path is empty
_path: ObjectPath,
interaction: NodeInteractionAssigned,
context: HasEffectsContext
): boolean {
for (const property of this.properties) {
if (property.hasEffectsOnInteractionAtPath(EMPTY_PATH, interaction, context)) return true;
}
return false;
}
markDeclarationReached(): void {
for (const property of this.properties) {
property.markDeclarationReached();
}
}
}