-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathIfStatement.ts
212 lines (197 loc) · 7.18 KB
/
IfStatement.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
import type MagicString from 'magic-string';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import { type HasEffectsContext, type InclusionContext } from '../ExecutionContext';
import TrackingScope from '../scopes/TrackingScope';
import { EMPTY_PATH, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import { tryCastLiteralValueToBoolean } from '../utils/tryCastLiteralValueToBoolean';
import BlockStatement from './BlockStatement';
import type Identifier from './Identifier';
import * as NodeType from './NodeType';
import { type LiteralValueOrUnknown, UnknownValue } from './shared/Expression';
import {
doNotDeoptimize,
type ExpressionNode,
type GenericEsTreeNode,
type IncludeChildren,
onlyIncludeSelfNoDeoptimize,
StatementBase,
type StatementNode
} from './shared/Node';
const unset = Symbol('unset');
export default class IfStatement extends StatementBase implements DeoptimizableEntity {
declare alternate: StatementNode | null;
declare consequent: StatementNode;
declare test: ExpressionNode;
declare type: NodeType.tIfStatement;
declare alternateScope?: TrackingScope;
declare consequentScope: TrackingScope;
private testValue: LiteralValueOrUnknown | typeof unset = unset;
deoptimizeCache(): void {
this.testValue = UnknownValue;
}
hasEffects(context: HasEffectsContext): boolean {
if (this.test.hasEffects(context)) {
return true;
}
const testValue = this.getTestValue();
if (typeof testValue === 'symbol') {
const { brokenFlow } = context;
if (this.consequent.hasEffects(context)) return true;
const consequentBrokenFlow = context.brokenFlow;
context.brokenFlow = brokenFlow;
if (this.alternate === null) return false;
if (this.alternate.hasEffects(context)) return true;
context.brokenFlow = context.brokenFlow && consequentBrokenFlow;
return false;
}
return testValue ? this.consequent.hasEffects(context) : !!this.alternate?.hasEffects(context);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
if (includeChildrenRecursively) {
this.includeRecursively(includeChildrenRecursively, context);
} else {
const testValue = this.getTestValue();
if (typeof testValue === 'symbol') {
this.includeUnknownTest(context);
} else {
this.includeKnownTest(context, testValue);
}
}
}
parseNode(esTreeNode: GenericEsTreeNode): this {
this.consequent = new (this.scope.context.getNodeConstructor(esTreeNode.consequent.type))(
this,
(this.consequentScope = new TrackingScope(this.scope))
).parseNode(esTreeNode.consequent);
if (esTreeNode.alternate) {
this.alternate = new (this.scope.context.getNodeConstructor(esTreeNode.alternate.type))(
this,
(this.alternateScope = new TrackingScope(this.scope))
).parseNode(esTreeNode.alternate);
}
return super.parseNode(esTreeNode);
}
render(code: MagicString, options: RenderOptions): void {
const {
snippets: { getPropertyAccess }
} = options;
// Note that unknown test values are always included
const testValue = this.getTestValue();
const hoistedDeclarations: Identifier[] = [];
const includesIfElse = this.test.included;
const noTreeshake = !this.scope.context.options.treeshake;
if (includesIfElse) {
this.test.render(code, options);
} else {
code.remove(this.start, this.consequent.start);
}
if (this.consequent.included && (noTreeshake || typeof testValue === 'symbol' || testValue)) {
this.consequent.render(code, options);
} else {
code.overwrite(this.consequent.start, this.consequent.end, includesIfElse ? ';' : '');
hoistedDeclarations.push(...this.consequentScope.hoistedDeclarations);
}
if (this.alternate) {
if (this.alternate.included && (noTreeshake || typeof testValue === 'symbol' || !testValue)) {
if (includesIfElse) {
if (code.original.charCodeAt(this.alternate.start - 1) === 101) {
code.prependLeft(this.alternate.start, ' ');
}
} else {
code.remove(this.consequent.end, this.alternate.start);
}
this.alternate.render(code, options);
} else {
if (includesIfElse && this.shouldKeepAlternateBranch()) {
code.overwrite(this.alternate.start, this.end, ';');
} else {
code.remove(this.consequent.end, this.end);
}
hoistedDeclarations.push(...this.alternateScope!.hoistedDeclarations);
}
}
this.renderHoistedDeclarations(hoistedDeclarations, code, getPropertyAccess);
}
private getTestValue(): LiteralValueOrUnknown {
if (this.testValue === unset) {
return (this.testValue = tryCastLiteralValueToBoolean(
this.test.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this)
));
}
return this.testValue;
}
private includeKnownTest(context: InclusionContext, testValue: LiteralValueOrUnknown) {
if (this.test.shouldBeIncluded(context)) {
this.test.include(context, false);
}
if (testValue && this.consequent.shouldBeIncluded(context)) {
this.consequent.include(context, false, { asSingleStatement: true });
}
if (!testValue && this.alternate?.shouldBeIncluded(context)) {
this.alternate.include(context, false, { asSingleStatement: true });
}
}
private includeRecursively(
includeChildrenRecursively: true | 'variables',
context: InclusionContext
) {
this.test.include(context, includeChildrenRecursively);
this.consequent.include(context, includeChildrenRecursively);
this.alternate?.include(context, includeChildrenRecursively);
}
private includeUnknownTest(context: InclusionContext) {
this.test.include(context, false);
const { brokenFlow } = context;
let consequentBrokenFlow = false;
if (this.consequent.shouldBeIncluded(context)) {
this.consequent.include(context, false, { asSingleStatement: true });
consequentBrokenFlow = context.brokenFlow;
context.brokenFlow = brokenFlow;
}
if (this.alternate?.shouldBeIncluded(context)) {
this.alternate.include(context, false, { asSingleStatement: true });
context.brokenFlow = context.brokenFlow && consequentBrokenFlow;
}
}
private renderHoistedDeclarations(
hoistedDeclarations: readonly Identifier[],
code: MagicString,
getPropertyAccess: (name: string) => string
) {
const hoistedVariables = [
...new Set(
hoistedDeclarations.map(identifier => {
const variable = identifier.variable!;
return variable.included ? variable.getName(getPropertyAccess) : '';
})
)
]
.filter(Boolean)
.join(', ');
if (hoistedVariables) {
const parentType = this.parent.type;
const needsBraces = parentType !== NodeType.Program && parentType !== NodeType.BlockStatement;
code.prependRight(this.start, `${needsBraces ? '{ ' : ''}var ${hoistedVariables}; `);
if (needsBraces) {
code.appendLeft(this.end, ` }`);
}
}
}
private shouldKeepAlternateBranch() {
let currentParent = this.parent;
do {
if (currentParent instanceof IfStatement && currentParent.alternate) {
return true;
}
if (currentParent instanceof BlockStatement) {
return false;
}
currentParent = (currentParent as any).parent;
} while (currentParent);
return false;
}
}
IfStatement.prototype.includeNode = onlyIncludeSelfNoDeoptimize;
IfStatement.prototype.applyDeoptimizations = doNotDeoptimize;