/
AssignmentExpression.ts
135 lines (129 loc) · 3.96 KB
/
AssignmentExpression.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
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
NodeRenderOptions,
removeLineBreaks,
RenderOptions
} from '../../utils/renderHelpers';
import {
renderSystemExportExpression,
renderSystemExportFunction,
renderSystemExportSequenceAfterExpression
} from '../../utils/systemJsRendering';
import { createHasEffectsContext, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
import Variable from '../variables/Variable';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
export default class AssignmentExpression extends NodeBase {
left!: ExpressionNode | PatternNode;
operator!:
| '='
| '+='
| '-='
| '*='
| '/='
| '%='
| '<<='
| '>>='
| '>>>='
| '|='
| '^='
| '&='
| '**=';
right!: ExpressionNode;
type!: NodeType.tAssignmentExpression;
protected deoptimized = false;
hasEffects(context: HasEffectsContext): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
return (
this.right.hasEffects(context) ||
this.left.hasEffects(context) ||
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context)
);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return path.length > 0 && this.right.hasEffectsWhenAccessedAtPath(path, context);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.deoptimized) this.applyDeoptimizations();
this.included = true;
let hasEffectsContext;
if (
includeChildrenRecursively ||
this.operator !== '=' ||
this.left.included ||
((hasEffectsContext = createHasEffectsContext()),
this.left.hasEffects(hasEffectsContext) ||
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, hasEffectsContext))
) {
this.left.include(context, includeChildrenRecursively);
}
this.right.include(context, includeChildrenRecursively);
}
render(
code: MagicString,
options: RenderOptions,
{ preventASI, renderedParentType, renderedSurroundingElement }: NodeRenderOptions = BLANK
): void {
if (this.left.included) {
this.left.render(code, options);
this.right.render(code, options);
} else {
const inclusionStart = findNonWhiteSpace(
code.original,
findFirstOccurrenceOutsideComment(code.original, '=', this.left.end) + 1
);
code.remove(this.start, inclusionStart);
if (preventASI) {
removeLineBreaks(code, inclusionStart, this.right.start);
}
this.right.render(code, options, {
renderedParentType: renderedParentType || renderedSurroundingElement || this.parent.type
});
}
if (options.format === 'system') {
if (this.left instanceof Identifier) {
const variable = this.left.variable!;
const exportNames = options.exportNamesByVariable.get(variable);
if (exportNames) {
if (exportNames.length === 1) {
renderSystemExportExpression(variable, this.start, this.end, code, options);
} else {
renderSystemExportSequenceAfterExpression(
variable,
this.start,
this.end,
this.parent.type !== NodeType.ExpressionStatement,
code,
options
);
}
}
} else {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports, options.exportNamesByVariable);
if (systemPatternExports.length > 0) {
renderSystemExportFunction(
systemPatternExports,
this.start,
this.end,
(renderedParentType || renderedSurroundingElement) === NodeType.ExpressionStatement,
code,
options
);
}
}
}
}
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.right.deoptimizePath(UNKNOWN_PATH);
this.context.requestTreeshakingPass();
}
}