/
AssignmentPattern.ts
71 lines (62 loc) · 2.47 KB
/
AssignmentPattern.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
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import type { HasEffectsContext } from '../ExecutionContext';
import { InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, type ObjectPath, UNKNOWN_PATH } 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 ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import type { PatternNode } from './shared/Pattern';
export default class AssignmentPattern extends NodeBase implements PatternNode {
declare left: PatternNode;
declare right: ExpressionNode;
declare type: NodeType.tAssignmentPattern;
protected deoptimized = false;
addExportedVariables(
variables: readonly Variable[],
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void {
this.left.addExportedVariables(variables, exportNamesByVariable);
}
declare(kind: string, init: ExpressionEntity): LocalVariable[] {
return this.left.declare(kind, init);
}
deoptimizePath(path: ObjectPath): void {
path.length === 0 && this.left.deoptimizePath(path);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return path.length > 0 || this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
}
// Note that FunctionBase may directly include .left and .right without
// including the pattern itself. This is how default parameter tree-shaking
// works at the moment.
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
this.left.include(context, includeChildrenRecursively);
this.right.include(context, includeChildrenRecursively);
}
markDeclarationReached(): void {
this.left.markDeclarationReached();
}
render(
code: MagicString,
options: RenderOptions,
{ isShorthandProperty }: NodeRenderOptions = BLANK
): void {
this.left.render(code, options, { isShorthandProperty });
if (this.right.included) {
this.right.render(code, options);
} else {
code.remove(this.left.end, this.end);
}
}
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.right.deoptimizePath(UNKNOWN_PATH);
this.context.requestTreeshakingPass();
}
}