/
AssignmentExpression.ts
122 lines (116 loc) · 3.67 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
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
NodeRenderOptions,
RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportFunctionLeft } from '../../utils/systemJsRendering';
import { createHasEffectsContext, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
export default class AssignmentExpression extends NodeBase {
left!: PatternNode;
operator!:
| '='
| '+='
| '-='
| '*='
| '/='
| '%='
| '<<='
| '>>='
| '>>>='
| '|='
| '^='
| '&='
| '**=';
right!: ExpressionNode;
type!: NodeType.tAssignmentExpression;
private 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,
{ renderedParentType }: NodeRenderOptions = BLANK
) {
if (this.left.included) {
this.left.render(code, options);
this.right.render(code, options);
} else {
this.right.render(code, options, {
renderedParentType: renderedParentType || this.parent.type
});
code.remove(this.start, this.right.start);
}
if (options.format === 'system') {
const exportNames =
this.left.variable && options.exportNamesByVariable.get(this.left.variable);
if (this.left.type === 'Identifier' && exportNames) {
const _ = options.compact ? '' : ' ';
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
const operation =
this.operator.length > 1 ? `${exportNames[0]}${_}${this.operator.slice(0, -1)}${_}` : '';
code.overwrite(
operatorPos,
findNonWhiteSpace(code.original, operatorPos + this.operator.length),
`=${_}${
exportNames.length === 1
? `exports('${exportNames[0]}',${_}`
: getSystemExportFunctionLeft([this.left.variable!], false, options)
}${operation}`
);
code.appendLeft(this.right.end, ')');
} else {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports, options.exportNamesByVariable);
if (systemPatternExports.length > 0) {
code.prependRight(
this.start,
getSystemExportFunctionLeft(systemPatternExports, true, options)
);
code.appendLeft(this.end, ')');
}
}
}
}
private applyDeoptimizations() {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.right.deoptimizePath(UNKNOWN_PATH);
}
}