forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticBlock.ts
49 lines (43 loc) · 1.59 KB
/
StaticBlock.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
import type MagicString from 'magic-string';
import {
findFirstOccurrenceOutsideComment,
type RenderOptions,
renderStatementList
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import type ChildScope from '../scopes/ChildScope';
import * as NodeType from './NodeType';
import { type IncludeChildren, StatementBase, type StatementNode } from './shared/Node';
export default class StaticBlock extends StatementBase {
declare body: readonly StatementNode[];
declare type: NodeType.tStaticBlock;
createScope(parentScope: ChildScope): void {
this.scope = new BlockScope(parentScope);
}
hasEffects(context: HasEffectsContext): boolean {
for (const node of this.body) {
if (node.hasEffects(context)) return true;
}
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
this.included = true;
for (const node of this.body) {
if (includeChildrenRecursively || node.shouldBeIncluded(context))
node.include(context, includeChildrenRecursively);
}
}
render(code: MagicString, options: RenderOptions): void {
if (this.body.length > 0) {
const bodyStartPos =
findFirstOccurrenceOutsideComment(code.original.slice(this.start, this.end), '{') + 1;
renderStatementList(this.body, code, this.start + bodyStartPos, this.end - 1, options);
} else {
super.render(code, options);
}
}
}
export function isStaticBlock(statement: StatementNode): statement is StaticBlock {
return statement.type === NodeType.StaticBlock;
}