forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayPattern.ts
More file actions
121 lines (110 loc) · 3.86 KB
/
ArrayPattern.ts
File metadata and controls
121 lines (110 loc) · 3.86 KB
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
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteractionAssigned } from '../NodeInteractions';
import { EMPTY_PATH, type ObjectPath, UnknownInteger, UnknownKey } from '../utils/PathTracker';
import type LocalVariable from '../variables/LocalVariable';
import type Variable from '../variables/Variable';
import type * as NodeType from './NodeType';
import type { ExpressionEntity } from './shared/Expression';
import { NodeBase, onlyIncludeSelf } from './shared/Node';
import type { DeclarationPatternNode, PatternNode } from './shared/Pattern';
import type { VariableKind } from './shared/VariableKinds';
export default class ArrayPattern extends NodeBase implements DeclarationPatternNode {
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: VariableKind,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): LocalVariable[] {
const variables: LocalVariable[] = [];
const includedPatternPath = getIncludedPatternPath(destructuredInitPath);
for (const element of this.elements) {
if (element !== null) {
variables.push(
...(element as DeclarationPatternNode).declare(kind, includedPatternPath, init)
);
}
}
return variables;
}
deoptimizeAssignment(destructuredInitPath: ObjectPath, init: ExpressionEntity): void {
const includedPatternPath = getIncludedPatternPath(destructuredInitPath);
for (const element of this.elements) {
element?.deoptimizeAssignment(includedPatternPath, init);
}
}
// Patterns can only be deoptimized at the empty path at the moment
deoptimizePath(): void {
for (const element of this.elements) {
element?.deoptimizePath(EMPTY_PATH);
}
}
hasEffectsWhenDestructuring(
context: HasEffectsContext,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): boolean {
const includedPatternPath = getIncludedPatternPath(destructuredInitPath);
for (const element of this.elements) {
if (element?.hasEffectsWhenDestructuring(context, includedPatternPath, init)) {
return true;
}
}
return false;
}
// Patterns are only checked at the empty path at the moment
hasEffectsOnInteractionAtPath(
_path: ObjectPath,
interaction: NodeInteractionAssigned,
context: HasEffectsContext
): boolean {
for (const element of this.elements) {
if (element?.hasEffectsOnInteractionAtPath(EMPTY_PATH, interaction, context)) return true;
}
return false;
}
includeDestructuredIfNecessary(
context: InclusionContext,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): boolean {
let included = false;
const includedPatternPath = getIncludedPatternPath(destructuredInitPath);
for (const element of this.elements) {
if (element) {
element.included ||= included;
included =
element.includeDestructuredIfNecessary(context, includedPatternPath, init) || included;
}
}
if (included) {
// This is necessary so that if any pattern element is included, all are
// included for proper deconflicting
for (const element of this.elements) {
if (element && !element.included) {
element.included = true;
element.includeDestructuredIfNecessary(context, includedPatternPath, init);
}
}
}
return (this.included ||= included);
}
markDeclarationReached(): void {
for (const element of this.elements) {
(element as DeclarationPatternNode)?.markDeclarationReached();
}
}
}
ArrayPattern.prototype.includeNode = onlyIncludeSelf;
const getIncludedPatternPath = (destructuredInitPath: ObjectPath): ObjectPath =>
destructuredInitPath.at(-1) === UnknownKey
? destructuredInitPath
: [...destructuredInitPath, UnknownInteger];