-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathObjectPattern.ts
142 lines (129 loc) · 4.5 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type MagicString from 'magic-string';
import type { RenderOptions } from '../../utils/renderHelpers';
import { getCommaSeparatedNodesWithBoundaries } from '../../utils/renderHelpers';
import { treeshakeNode } from '../../utils/treeshakeNode';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteractionAssigned } from '../NodeInteractions';
import { EMPTY_PATH, type ObjectPath } from '../utils/PathTracker';
import type LocalVariable from '../variables/LocalVariable';
import type Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import type Property from './Property';
import type RestElement from './RestElement';
import type { ExpressionEntity } from './shared/Expression';
import { doNotDeoptimize, NodeBase, onlyIncludeSelfNoDeoptimize } from './shared/Node';
import type { DeclarationPatternNode } from './shared/Pattern';
import type { VariableKind } from './shared/VariableKinds';
export default class ObjectPattern extends NodeBase implements DeclarationPatternNode {
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.addExportedVariables(variables, exportNamesByVariable);
} else {
property.argument.addExportedVariables(variables, exportNamesByVariable);
}
}
}
declare(
kind: VariableKind,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): LocalVariable[] {
const variables: LocalVariable[] = [];
for (const property of this.properties) {
variables.push(...property.declare(kind, destructuredInitPath, init));
}
return variables;
}
deoptimizeAssignment(destructuredInitPath: ObjectPath, init: ExpressionEntity): void {
for (const property of this.properties) {
property.deoptimizeAssignment(destructuredInitPath, init);
}
}
deoptimizePath(path: ObjectPath): void {
if (path.length === 0) {
for (const property of this.properties) {
property.deoptimizePath(path);
}
}
}
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;
}
hasEffectsWhenDestructuring(
context: HasEffectsContext,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): boolean {
for (const property of this.properties) {
if (property.hasEffectsWhenDestructuring(context, destructuredInitPath, init)) return true;
}
return false;
}
includeDestructuredIfNecessary(
context: InclusionContext,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): boolean {
if (!this.properties.length) return false;
const lastProperty = this.properties.at(-1)!;
const lastPropertyIncluded = lastProperty.includeDestructuredIfNecessary(
context,
destructuredInitPath,
init
);
const lastPropertyIsRestElement = lastProperty.type === NodeType.RestElement;
let included = lastPropertyIsRestElement ? lastPropertyIncluded : false;
for (const property of this.properties.slice(0, -1)) {
if (lastPropertyIsRestElement && lastPropertyIncluded) {
property.includeNode(context);
}
included =
property.includeDestructuredIfNecessary(context, destructuredInitPath, init) || included;
}
return (this.included ||= included);
}
markDeclarationReached(): void {
for (const property of this.properties) {
property.markDeclarationReached();
}
}
render(code: MagicString, options: RenderOptions): void {
if (this.properties.length > 0) {
const separatedNodes = getCommaSeparatedNodesWithBoundaries(
this.properties,
code,
this.start + 1,
this.end - 1
);
let lastSeparatorPos: number | null = null;
for (const { node, separator, start, end } of separatedNodes) {
if (!node.included) {
treeshakeNode(node, code, start, end);
continue;
}
lastSeparatorPos = separator;
node.render(code, options);
}
if (lastSeparatorPos) {
code.remove(lastSeparatorPos, this.end - 1);
}
}
}
}
ObjectPattern.prototype.includeNode = onlyIncludeSelfNoDeoptimize;
ObjectPattern.prototype.applyDeoptimizations = doNotDeoptimize;