/
IfStatement.ts
131 lines (121 loc) · 4.42 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
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { removeAnnotations } from '../../utils/treeshakeNode';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { BROKEN_FLOW_NONE, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_IMMUTABLE_TRACKER, EMPTY_PATH } from '../utils/PathTracker';
import { LiteralValueOrUnknown, UnknownValue } from '../values';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, StatementBase, StatementNode } from './shared/Node';
export default class IfStatement extends StatementBase implements DeoptimizableEntity {
alternate!: StatementNode | null;
consequent!: StatementNode;
test!: ExpressionNode;
type!: NodeType.tIfStatement;
private testValue: LiteralValueOrUnknown;
bind() {
super.bind();
this.testValue = this.test.getLiteralValueAtPath(EMPTY_PATH, EMPTY_IMMUTABLE_TRACKER, this);
}
deoptimizeCache() {
this.testValue = UnknownValue;
}
hasEffects(context: HasEffectsContext): boolean {
if (this.test.hasEffects(context)) return true;
if (this.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 this.testValue
? this.consequent.hasEffects(context)
: this.alternate !== null && this.alternate.hasEffects(context);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
if (includeChildrenRecursively) {
this.includeRecursively(includeChildrenRecursively, context);
} else if (this.testValue === UnknownValue) {
this.includeUnknownTest(context);
} else {
this.includeKnownTest(context);
}
}
render(code: MagicString, options: RenderOptions) {
// Note that unknown test values are always included
if (
!this.test.included &&
(this.testValue
? this.alternate === null || !this.alternate.included
: !this.consequent.included)
) {
const singleRetainedBranch = (this.testValue
? this.consequent
: this.alternate) as StatementNode;
code.remove(this.start, singleRetainedBranch.start);
code.remove(singleRetainedBranch.end, this.end);
removeAnnotations(this, code);
singleRetainedBranch.render(code, options);
} else {
if (this.test.included) {
this.test.render(code, options);
} else {
code.overwrite(this.test.start, this.test.end, this.testValue ? 'true' : 'false');
}
if (this.consequent.included) {
this.consequent.render(code, options);
} else {
code.overwrite(this.consequent.start, this.consequent.end, ';');
}
if (this.alternate !== null) {
if (this.alternate.included) {
this.alternate.render(code, options);
} else {
code.remove(this.consequent.end, this.alternate.end);
}
}
}
}
private includeKnownTest(context: InclusionContext) {
if (this.test.shouldBeIncluded(context)) {
this.test.include(context, false);
}
if (this.testValue && this.consequent.shouldBeIncluded(context)) {
this.consequent.include(context, false);
}
if (this.alternate !== null && !this.testValue && this.alternate.shouldBeIncluded(context)) {
this.alternate.include(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.include(context, false);
consequentBrokenFlow = context.brokenFlow;
context.brokenFlow = brokenFlow;
}
if (this.alternate !== null && this.alternate.shouldBeIncluded(context)) {
this.alternate.include(context, false);
context.brokenFlow =
context.brokenFlow < consequentBrokenFlow ? context.brokenFlow : consequentBrokenFlow;
}
}
}