-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
IfStatement.ts
205 lines (191 loc) · 6.9 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
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { BROKEN_FLOW_NONE, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import TrackingScope from '../scopes/TrackingScope';
import { EMPTY_PATH, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import BlockStatement from './BlockStatement';
import Identifier from './Identifier';
import * as NodeType from './NodeType';
import { LiteralValueOrUnknown, UnknownValue } from './shared/Expression';
import {
ExpressionNode,
GenericEsTreeNode,
IncludeChildren,
StatementBase,
StatementNode
} from './shared/Node';
const unset = Symbol('unset');
export default class IfStatement extends StatementBase implements DeoptimizableEntity {
alternate!: StatementNode | null;
consequent!: StatementNode;
test!: ExpressionNode;
type!: NodeType.tIfStatement;
private alternateScope?: TrackingScope;
private 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 (testValue === UnknownValue) {
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 ? context.brokenFlow : consequentBrokenFlow;
return false;
}
return testValue
? this.consequent.hasEffects(context)
: this.alternate !== null && 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 (testValue === UnknownValue) {
this.includeUnknownTest(context);
} else {
this.includeKnownTest(context, testValue);
}
}
}
parseNode(esTreeNode: GenericEsTreeNode): void {
this.consequentScope = new TrackingScope(this.scope);
this.consequent = new (this.context.nodeConstructors[esTreeNode.consequent.type] ||
this.context.nodeConstructors.UnknownNode)(esTreeNode.consequent, this, this.consequentScope);
if (esTreeNode.alternate) {
this.alternateScope = new TrackingScope(this.scope);
this.alternate = new (this.context.nodeConstructors[esTreeNode.alternate.type] ||
this.context.nodeConstructors.UnknownNode)(esTreeNode.alternate, this, this.alternateScope);
}
super.parseNode(esTreeNode);
}
render(code: MagicString, options: RenderOptions): void {
// Note that unknown test values are always included
const testValue = this.getTestValue();
const hoistedDeclarations: Identifier[] = [];
const includesIfElse = this.test.included;
const noTreeshake = !this.context.options.treeshake;
if (includesIfElse) {
this.test.render(code, options);
} else {
code.remove(this.start, this.consequent.start);
}
if (this.consequent.included && (noTreeshake || testValue === UnknownValue || 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 || testValue === UnknownValue || !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);
}
private getTestValue(): LiteralValueOrUnknown {
if (this.testValue === unset) {
return (this.testValue = 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.includeAsSingleStatement(context, false);
}
if (this.alternate !== null && !testValue && this.alternate.shouldBeIncluded(context)) {
this.alternate.includeAsSingleStatement(context, false);
}
}
private includeRecursively(
includeChildrenRecursively: true | 'variables',
context: InclusionContext
) {
this.test.include(context, includeChildrenRecursively);
this.consequent.include(context, includeChildrenRecursively);
if (this.alternate !== null) {
this.alternate.include(context, includeChildrenRecursively);
}
}
private includeUnknownTest(context: InclusionContext) {
this.test.include(context, false);
const { brokenFlow } = context;
let consequentBrokenFlow = BROKEN_FLOW_NONE;
if (this.consequent.shouldBeIncluded(context)) {
this.consequent.includeAsSingleStatement(context, false);
consequentBrokenFlow = context.brokenFlow;
context.brokenFlow = brokenFlow;
}
if (this.alternate !== null && this.alternate.shouldBeIncluded(context)) {
this.alternate.includeAsSingleStatement(context, false);
context.brokenFlow =
context.brokenFlow < consequentBrokenFlow ? context.brokenFlow : consequentBrokenFlow;
}
}
private renderHoistedDeclarations(hoistedDeclarations: Identifier[], code: MagicString) {
const hoistedVars = [
...new Set(
hoistedDeclarations.map(identifier => {
const variable = identifier.variable!;
return variable.included ? variable.getName() : '';
})
)
]
.filter(Boolean)
.join(', ');
if (hoistedVars) {
const parentType = this.parent.type;
const needsBraces = parentType !== NodeType.Program && parentType !== NodeType.BlockStatement;
code.prependRight(this.start, `${needsBraces ? '{ ' : ''}var ${hoistedVars}; `);
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;
}
}