/
SequenceExpression.ts
138 lines (128 loc) · 3.95 KB
/
SequenceExpression.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
136
137
138
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
getCommaSeparatedNodesWithBoundaries,
NodeRenderOptions,
removeLineBreaks,
RenderOptions
} from '../../utils/renderHelpers';
import { treeshakeNode } from '../../utils/treeshakeNode';
import { CallOptions } from '../CallOptions';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { NodeEvent } from '../NodeEvents';
import { ObjectPath, PathTracker } from '../utils/PathTracker';
import ExpressionStatement from './ExpressionStatement';
import * as NodeType from './NodeType';
import { ExpressionEntity, LiteralValueOrUnknown } from './shared/Expression';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
export default class SequenceExpression extends NodeBase {
expressions!: ExpressionNode[];
type!: NodeType.tSequenceExpression;
deoptimizePath(path: ObjectPath): void {
this.expressions[this.expressions.length - 1].deoptimizePath(path);
}
deoptimizeThisOnEventAtPath(
event: NodeEvent,
path: ObjectPath,
thisParameter: ExpressionEntity,
recursionTracker: PathTracker
): void {
this.expressions[this.expressions.length - 1].deoptimizeThisOnEventAtPath(
event,
path,
thisParameter,
recursionTracker
);
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
return this.expressions[this.expressions.length - 1].getLiteralValueAtPath(
path,
recursionTracker,
origin
);
}
hasEffects(context: HasEffectsContext): boolean {
for (const expression of this.expressions) {
if (expression.hasEffects(context)) return true;
}
return false;
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return (
path.length > 0 &&
this.expressions[this.expressions.length - 1].hasEffectsWhenAccessedAtPath(path, context)
);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
return this.expressions[this.expressions.length - 1].hasEffectsWhenAssignedAtPath(
path,
context
);
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
context: HasEffectsContext
): boolean {
return this.expressions[this.expressions.length - 1].hasEffectsWhenCalledAtPath(
path,
callOptions,
context
);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
const lastExpression = this.expressions[this.expressions.length - 1];
for (const expression of this.expressions) {
if (
includeChildrenRecursively ||
(expression === lastExpression && !(this.parent instanceof ExpressionStatement)) ||
expression.shouldBeIncluded(context)
)
expression.include(context, includeChildrenRecursively);
}
}
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, preventASI }: NodeRenderOptions = BLANK
): void {
let includedNodes = 0;
let lastSeparatorPos: number | null = null;
const lastNode = this.expressions[this.expressions.length - 1];
for (const { node, separator, start, end } of getCommaSeparatedNodesWithBoundaries(
this.expressions,
code,
this.start,
this.end
)) {
if (!node.included) {
treeshakeNode(node, code, start, end);
continue;
}
includedNodes++;
lastSeparatorPos = separator;
if (includedNodes === 1 && preventASI) {
removeLineBreaks(code, start, node.start);
}
if (includedNodes === 1) {
const parentType = renderedParentType || this.parent.type;
node.render(code, options, {
isCalleeOfRenderedParent: isCalleeOfRenderedParent && node === lastNode,
renderedParentType: parentType,
renderedSurroundingElement: parentType
});
} else {
node.render(code, options);
}
}
if (lastSeparatorPos) {
code.remove(lastSeparatorPos, this.end);
}
}
}