forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRestElement.ts
More file actions
109 lines (96 loc) · 3.34 KB
/
RestElement.ts
File metadata and controls
109 lines (96 loc) · 3.34 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
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteractionAssigned } from '../NodeInteractions';
import { EMPTY_PATH, type ObjectPath, 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 type { IncludeChildren } from './shared/Node';
import { NodeBase, onlyIncludeSelf } from './shared/Node';
import type { DeclarationPatternNode, PatternNode } from './shared/Pattern';
import type { VariableKind } from './shared/VariableKinds';
export default class RestElement extends NodeBase implements DeclarationPatternNode {
declare argument: PatternNode;
declare type: NodeType.tRestElement;
private declarationInit: ExpressionEntity | null = null;
addExportedVariables(
variables: readonly Variable[],
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void {
this.argument.addExportedVariables(variables, exportNamesByVariable);
}
declare(
kind: VariableKind,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): LocalVariable[] {
this.declarationInit = init;
return (this.argument as DeclarationPatternNode).declare(
kind,
getIncludedPatternPath(destructuredInitPath),
init
);
}
deoptimizeAssignment(destructuredInitPath: ObjectPath, init: ExpressionEntity): void {
this.argument.deoptimizeAssignment(getIncludedPatternPath(destructuredInitPath), init);
}
deoptimizePath(path: ObjectPath): void {
if (path.length === 0) {
this.argument.deoptimizePath(EMPTY_PATH);
}
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteractionAssigned,
context: HasEffectsContext
): boolean {
return (
path.length > 0 ||
this.argument.hasEffectsOnInteractionAtPath(EMPTY_PATH, interaction, context)
);
}
hasEffectsWhenDestructuring(
context: HasEffectsContext,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): boolean {
return this.argument.hasEffectsWhenDestructuring(
context,
getIncludedPatternPath(destructuredInitPath),
init
);
}
includeDestructuredIfNecessary(
context: InclusionContext,
destructuredInitPath: ObjectPath,
init: ExpressionEntity
): boolean {
return (this.included =
this.argument.includeDestructuredIfNecessary(
context,
getIncludedPatternPath(destructuredInitPath),
init
) || this.included);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
if (!this.included) this.includeNode(context);
// This should just include the identifier, its properties should be
// included where the variable is used.
this.argument.include(context, includeChildrenRecursively);
}
markDeclarationReached(): void {
(this.argument as DeclarationPatternNode).markDeclarationReached();
}
applyDeoptimizations() {
this.deoptimized = true;
if (this.declarationInit !== null) {
this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
this.scope.context.requestTreeshakingPass();
}
}
}
RestElement.prototype.includeNode = onlyIncludeSelf;
const getIncludedPatternPath = (destructuredInitPath: ObjectPath): ObjectPath =>
destructuredInitPath.at(-1) === UnknownKey
? destructuredInitPath
: [...destructuredInitPath, UnknownKey];