forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicalExpression.ts
235 lines (220 loc) · 6.72 KB
/
LogicalExpression.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import type MagicString from 'magic-string';
import { BLANK, EMPTY_ARRAY } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
type NodeRenderOptions,
removeLineBreaks,
type RenderOptions
} from '../../utils/renderHelpers';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteraction, NodeInteractionCalled } from '../NodeInteractions';
import {
EMPTY_PATH,
type ObjectPath,
type PathTracker,
SHARED_RECURSION_TRACKER,
UNKNOWN_PATH
} from '../utils/PathTracker';
import type * as NodeType from './NodeType';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UnknownValue
} from './shared/Expression';
import { MultiExpression } from './shared/MultiExpression';
import { type ExpressionNode, type IncludeChildren, NodeBase } from './shared/Node';
export type LogicalOperator = '||' | '&&' | '??';
export default class LogicalExpression extends NodeBase implements DeoptimizableEntity {
declare left: ExpressionNode;
declare operator: LogicalOperator;
declare right: ExpressionNode;
declare type: NodeType.tLogicalExpression;
//private isBranchResolutionAnalysed = false;
private get isBranchResolutionAnalysed(): boolean {
return isFlagSet(this.flags, Flag.isBranchResolutionAnalysed);
}
private set isBranchResolutionAnalysed(value: boolean) {
this.flags = setFlag(this.flags, Flag.isBranchResolutionAnalysed, value);
}
// We collect deoptimization information if usedBranch !== null
private expressionsToBeDeoptimized: DeoptimizableEntity[] = [];
private usedBranch: ExpressionNode | null = null;
deoptimizeArgumentsOnInteractionAtPath(
interaction: NodeInteraction,
path: ObjectPath,
recursionTracker: PathTracker
): void {
this.left.deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker);
this.right.deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker);
}
deoptimizeCache(): void {
if (this.usedBranch) {
const unusedBranch = this.usedBranch === this.left ? this.right : this.left;
this.usedBranch = null;
unusedBranch.deoptimizePath(UNKNOWN_PATH);
const {
scope: { context },
expressionsToBeDeoptimized
} = this;
this.expressionsToBeDeoptimized = EMPTY_ARRAY as unknown as DeoptimizableEntity[];
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
// Request another pass because we need to ensure "include" runs again if
// it is rendered
context.requestTreeshakingPass();
}
}
deoptimizePath(path: ObjectPath): void {
const usedBranch = this.getUsedBranch();
if (usedBranch) {
usedBranch.deoptimizePath(path);
} else {
this.left.deoptimizePath(path);
this.right.deoptimizePath(path);
}
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
const usedBranch = this.getUsedBranch();
if (!usedBranch) return UnknownValue;
this.expressionsToBeDeoptimized.push(origin);
return usedBranch.getLiteralValueAtPath(path, recursionTracker, origin);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
interaction: NodeInteractionCalled,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): [expression: ExpressionEntity, isPure: boolean] {
const usedBranch = this.getUsedBranch();
if (!usedBranch)
return [
new MultiExpression([
this.left.getReturnExpressionWhenCalledAtPath(
path,
interaction,
recursionTracker,
origin
)[0],
this.right.getReturnExpressionWhenCalledAtPath(
path,
interaction,
recursionTracker,
origin
)[0]
]),
false
];
this.expressionsToBeDeoptimized.push(origin);
return usedBranch.getReturnExpressionWhenCalledAtPath(
path,
interaction,
recursionTracker,
origin
);
}
hasEffects(context: HasEffectsContext): boolean {
if (this.left.hasEffects(context)) {
return true;
}
if (this.getUsedBranch() !== this.left) {
return this.right.hasEffects(context);
}
return false;
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
const usedBranch = this.getUsedBranch();
if (!usedBranch) {
return (
this.left.hasEffectsOnInteractionAtPath(path, interaction, context) ||
this.right.hasEffectsOnInteractionAtPath(path, interaction, context)
);
}
return usedBranch.hasEffectsOnInteractionAtPath(path, interaction, context);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
const usedBranch = this.getUsedBranch();
if (
includeChildrenRecursively ||
(usedBranch === this.right && this.left.shouldBeIncluded(context)) ||
!usedBranch
) {
this.left.include(context, includeChildrenRecursively);
this.right.include(context, includeChildrenRecursively);
} else {
usedBranch.include(context, includeChildrenRecursively);
}
}
removeAnnotations(code: MagicString) {
this.left.removeAnnotations(code);
}
render(
code: MagicString,
options: RenderOptions,
{
isCalleeOfRenderedParent,
preventASI,
renderedParentType,
renderedSurroundingElement
}: NodeRenderOptions = BLANK
): void {
if (!this.left.included || !this.right.included) {
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
if (this.right.included) {
const removePos = findNonWhiteSpace(code.original, operatorPos + 2);
code.remove(this.start, removePos);
if (preventASI) {
removeLineBreaks(code, removePos, this.right.start);
}
this.left.removeAnnotations(code);
} else {
code.remove(operatorPos, this.end);
}
this.getUsedBranch()!.render(code, options, {
isCalleeOfRenderedParent,
preventASI,
renderedParentType: renderedParentType || this.parent.type,
renderedSurroundingElement: renderedSurroundingElement || this.parent.type
});
} else {
this.left.render(code, options, {
preventASI,
renderedSurroundingElement
});
this.right.render(code, options);
}
}
private getUsedBranch() {
if (!this.isBranchResolutionAnalysed) {
this.isBranchResolutionAnalysed = true;
const leftValue = this.left.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this);
if (typeof leftValue === 'symbol') {
return null;
} else {
this.usedBranch =
(this.operator === '||' && leftValue) ||
(this.operator === '&&' && !leftValue) ||
(this.operator === '??' && leftValue != null)
? this.left
: this.right;
}
}
return this.usedBranch;
}
}