/
SequenceExpression.ts
122 lines (114 loc) · 3.62 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
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
getCommaSeparatedNodesWithBoundaries,
type NodeRenderOptions,
removeLineBreaks,
type RenderOptions
} from '../../utils/renderHelpers';
import { treeshakeNode } from '../../utils/treeshakeNode';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteractionWithThisArg } from '../NodeInteractions';
import { NodeInteraction } from '../NodeInteractions';
import type { ObjectPath, PathTracker } from '../utils/PathTracker';
import ExpressionStatement from './ExpressionStatement';
import type * as NodeType from './NodeType';
import type { LiteralValueOrUnknown } from './shared/Expression';
import { type ExpressionNode, type IncludeChildren, NodeBase } from './shared/Node';
export default class SequenceExpression extends NodeBase {
declare expressions: ExpressionNode[];
declare type: NodeType.tSequenceExpression;
deoptimizePath(path: ObjectPath): void {
this.expressions[this.expressions.length - 1].deoptimizePath(path);
}
deoptimizeThisOnInteractionAtPath(
interaction: NodeInteractionWithThisArg,
path: ObjectPath,
recursionTracker: PathTracker
): void {
this.expressions[this.expressions.length - 1].deoptimizeThisOnInteractionAtPath(
interaction,
path,
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;
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
return this.expressions[this.expressions.length - 1].hasEffectsOnInteractionAtPath(
path,
interaction,
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);
}
}
}