/
SequenceExpression.ts
114 lines (105 loc) · 3.38 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
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 { ExecutionPathOptions } from '../ExecutionPathOptions';
import { ImmutableEntityPathTracker } from '../utils/ImmutableEntityPathTracker';
import { LiteralValueOrUnknown, ObjectPath } from '../values';
import CallExpression from './CallExpression';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
export default class SequenceExpression extends NodeBase {
expressions!: ExpressionNode[];
type!: NodeType.tSequenceExpression;
deoptimizePath(path: ObjectPath) {
if (path.length > 0) this.expressions[this.expressions.length - 1].deoptimizePath(path);
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: ImmutableEntityPathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
return this.expressions[this.expressions.length - 1].getLiteralValueAtPath(
path,
recursionTracker,
origin
);
}
hasEffects(options: ExecutionPathOptions): boolean {
for (const expression of this.expressions) {
if (expression.hasEffects(options)) return true;
}
return false;
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
return (
path.length > 0 &&
this.expressions[this.expressions.length - 1].hasEffectsWhenAccessedAtPath(path, options)
);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
return (
path.length === 0 ||
this.expressions[this.expressions.length - 1].hasEffectsWhenAssignedAtPath(path, options)
);
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
options: ExecutionPathOptions
): boolean {
return this.expressions[this.expressions.length - 1].hasEffectsWhenCalledAtPath(
path,
callOptions,
options
);
}
include(includeChildrenRecursively: IncludeChildren) {
this.included = true;
for (let i = 0; i < this.expressions.length - 1; i++) {
const node = this.expressions[i];
if (includeChildrenRecursively || node.shouldBeIncluded())
node.include(includeChildrenRecursively);
}
this.expressions[this.expressions.length - 1].include(includeChildrenRecursively);
}
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, preventASI }: NodeRenderOptions = BLANK
) {
let includedNodes = 0;
for (const { node, start, end } of getCommaSeparatedNodesWithBoundaries(
this.expressions,
code,
this.start,
this.end
)) {
if (!node.included) {
treeshakeNode(node, code, start, end);
continue;
}
includedNodes++;
if (includedNodes === 1 && preventASI) {
removeLineBreaks(code, start, node.start);
}
if (node === this.expressions[this.expressions.length - 1] && includedNodes === 1) {
node.render(code, options, {
isCalleeOfRenderedParent: renderedParentType
? isCalleeOfRenderedParent
: (this.parent as CallExpression).callee === this,
renderedParentType: renderedParentType || this.parent.type
});
} else {
node.render(code, options);
}
}
}
}