/
Program.ts
48 lines (42 loc) · 1.5 KB
/
Program.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
import type MagicString from 'magic-string';
import { type RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type * as NodeType from './NodeType';
import { type IncludeChildren, NodeBase, type StatementNode } from './shared/Node';
export default class Program extends NodeBase {
declare body: readonly StatementNode[];
declare sourceType: 'module';
declare type: NodeType.tProgram;
private hasCachedEffect = false;
hasEffects(context: HasEffectsContext): boolean {
// We are caching here to later more efficiently identify side-effect-free modules
if (this.hasCachedEffect) return true;
for (const node of this.body) {
if (node.hasEffects(context)) {
return (this.hasCachedEffect = 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 {
let start = this.start;
if (code.original.startsWith('#!')) {
start = Math.min(code.original.indexOf('\n') + 1, this.end);
code.remove(0, start);
}
if (this.body.length > 0) {
renderStatementList(this.body, code, start, this.end, options);
} else {
super.render(code, options);
}
}
protected applyDeoptimizations() {}
}