/
SwitchStatement.ts
95 lines (88 loc) · 2.92 KB
/
SwitchStatement.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
import MagicString from 'magic-string';
import { RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import {
BROKEN_FLOW_BREAK_CONTINUE,
createHasEffectsContext,
HasEffectsContext,
InclusionContext
} from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import Scope from '../scopes/Scope';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, StatementBase } from './shared/Node';
import SwitchCase from './SwitchCase';
export default class SwitchStatement extends StatementBase {
cases!: SwitchCase[];
discriminant!: ExpressionNode;
type!: NodeType.tSwitchStatement;
private defaultCase!: number | null;
createScope(parentScope: Scope) {
this.scope = new BlockScope(parentScope);
}
hasEffects(context: HasEffectsContext) {
if (this.discriminant.hasEffects(context)) return true;
const {
brokenFlow,
ignore: { breaks }
} = context;
let minBrokenFlow = Infinity;
context.ignore.breaks = true;
for (const switchCase of this.cases) {
if (switchCase.hasEffects(context)) return true;
minBrokenFlow = context.brokenFlow < minBrokenFlow ? context.brokenFlow : minBrokenFlow;
context.brokenFlow = brokenFlow;
}
if (this.defaultCase !== null && !(minBrokenFlow === BROKEN_FLOW_BREAK_CONTINUE)) {
context.brokenFlow = minBrokenFlow;
}
context.ignore.breaks = breaks;
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
this.discriminant.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
let minBrokenFlow = Infinity;
let isCaseIncluded =
includeChildrenRecursively ||
(this.defaultCase !== null && this.defaultCase < this.cases.length - 1);
for (let caseIndex = this.cases.length - 1; caseIndex >= 0; caseIndex--) {
const switchCase = this.cases[caseIndex];
if (switchCase.included) {
isCaseIncluded = true;
}
if (!isCaseIncluded) {
const hasEffectsContext = createHasEffectsContext();
hasEffectsContext.ignore.breaks = true;
isCaseIncluded = switchCase.hasEffects(hasEffectsContext);
}
if (isCaseIncluded) {
switchCase.include(context, includeChildrenRecursively);
minBrokenFlow = minBrokenFlow < context.brokenFlow ? minBrokenFlow : context.brokenFlow;
context.brokenFlow = brokenFlow;
}
}
if (
isCaseIncluded &&
this.defaultCase !== null &&
!(minBrokenFlow === BROKEN_FLOW_BREAK_CONTINUE)
) {
context.brokenFlow = minBrokenFlow;
}
}
initialise() {
for (let caseIndex = 0; caseIndex < this.cases.length; caseIndex++) {
if (this.cases[caseIndex].test === null) {
this.defaultCase = caseIndex;
return;
}
}
this.defaultCase = null;
}
render(code: MagicString, options: RenderOptions) {
this.discriminant.render(code, options);
if (this.cases.length > 0) {
renderStatementList(this.cases, code, this.cases[0].start, this.end - 1, options);
}
}
}