/
AssignmentExpression.ts
82 lines (77 loc) · 2.43 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
import MagicString from 'magic-string';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { HasEffectsContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
export default class AssignmentExpression extends NodeBase {
left!: PatternNode | ExpressionNode;
operator!:
| '='
| '+='
| '-='
| '*='
| '/='
| '%='
| '<<='
| '>>='
| '>>>='
| '|='
| '^='
| '&='
| '**=';
right!: ExpressionNode;
type!: NodeType.tAssignmentExpression;
bind() {
super.bind();
this.left.deoptimizePath(EMPTY_PATH);
// We cannot propagate mutations of the new binding to the old binding with certainty
this.right.deoptimizePath(UNKNOWN_PATH);
}
hasEffects(context: HasEffectsContext): boolean {
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);
}
render(code: MagicString, options: RenderOptions) {
this.left.render(code, options);
this.right.render(code, options);
if (options.format === 'system') {
if (this.left.variable && this.left.variable.exportName) {
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
const operation =
this.operator.length > 1
? ` ${this.left.variable.exportName} ${this.operator.slice(0, -1)}`
: '';
code.overwrite(
operatorPos,
operatorPos + this.operator.length,
`= exports('${this.left.variable.exportName}',${operation}`
);
code.appendLeft(this.right.end, `)`);
} else if ('addExportedVariables' in this.left) {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports);
if (systemPatternExports.length > 0) {
code.prependRight(
this.start,
`function (v) {${getSystemExportStatement(systemPatternExports)} return v;} (`
);
code.appendLeft(this.end, ')');
}
}
}
}
}