forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabeledStatement.ts
62 lines (58 loc) · 2.05 KB
/
LabeledStatement.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
import type MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
type RenderOptions
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type Identifier from './Identifier';
import type * as NodeType from './NodeType';
import { type IncludeChildren, StatementBase, type StatementNode } from './shared/Node';
export default class LabeledStatement extends StatementBase {
declare body: StatementNode;
declare label: Identifier;
declare type: NodeType.tLabeledStatement;
hasEffects(context: HasEffectsContext): boolean {
const { brokenFlow, includedLabels } = context;
context.ignore.labels.add(this.label.name);
context.includedLabels = new Set<string>();
let bodyHasEffects = false;
if (this.body.hasEffects(context)) {
bodyHasEffects = true;
} else {
context.ignore.labels.delete(this.label.name);
if (context.includedLabels.has(this.label.name)) {
context.includedLabels.delete(this.label.name);
context.brokenFlow = brokenFlow;
}
}
context.includedLabels = new Set([...includedLabels, ...context.includedLabels]);
return bodyHasEffects;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
const { brokenFlow, includedLabels } = context;
context.includedLabels = new Set<string>();
this.body.include(context, includeChildrenRecursively);
if (includeChildrenRecursively || context.includedLabels.has(this.label.name)) {
this.label.include();
context.includedLabels.delete(this.label.name);
context.brokenFlow = brokenFlow;
}
context.includedLabels = new Set([...includedLabels, ...context.includedLabels]);
}
render(code: MagicString, options: RenderOptions): void {
if (this.label.included) {
this.label.render(code, options);
} else {
code.remove(
this.start,
findNonWhiteSpace(
code.original,
findFirstOccurrenceOutsideComment(code.original, ':', this.label.end) + 1
)
);
}
this.body.render(code, options);
}
}