forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchCase.ts
63 lines (58 loc) · 2 KB
/
SwitchCase.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
import type MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
type NodeRenderOptions,
type RenderOptions,
renderStatementList
} from '../../utils/renderHelpers';
import {
BROKEN_FLOW_BREAK_CONTINUE,
BROKEN_FLOW_ERROR_RETURN_LABEL,
type HasEffectsContext,
type InclusionContext
} from '../ExecutionContext';
import type * as NodeType from './NodeType';
import {
type ExpressionNode,
type IncludeChildren,
NodeBase,
type StatementNode
} from './shared/Node';
export default class SwitchCase extends NodeBase {
declare consequent: readonly StatementNode[];
declare needsBoundaries: true;
declare test: ExpressionNode | null;
declare type: NodeType.tSwitchCase;
hasEffects(context: HasEffectsContext): boolean {
if (this.test?.hasEffects(context)) return true;
for (const node of this.consequent) {
if (context.brokenFlow) break;
if (node.hasEffects(context)) return true;
}
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
this.test?.include(context, includeChildrenRecursively);
for (const node of this.consequent) {
if (includeChildrenRecursively || node.shouldBeIncluded(context))
node.include(context, includeChildrenRecursively);
}
if (context.existedBroken && context.brokenFlow === BROKEN_FLOW_ERROR_RETURN_LABEL) {
context.brokenFlow = BROKEN_FLOW_BREAK_CONTINUE;
}
}
render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void {
if (this.consequent.length > 0) {
this.test && this.test.render(code, options);
const testEnd = this.test
? this.test.end
: findFirstOccurrenceOutsideComment(code.original, 'default', this.start) + 7;
const consequentStart = findFirstOccurrenceOutsideComment(code.original, ':', testEnd) + 1;
renderStatementList(this.consequent, code, consequentStart, nodeRenderOptions!.end!, options);
} else {
super.render(code, options);
}
}
}
SwitchCase.prototype.needsBoundaries = true;