-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathProgram.ts
111 lines (103 loc) · 3.3 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { locate } from 'locate-character';
import type MagicString from 'magic-string';
import type { RollupAnnotation } from '../../utils/astConverterHelpers';
import { LOGLEVEL_INFO, LOGLEVEL_WARN } from '../../utils/logging';
import { logFirstSideEffect, logInvalidAnnotation } from '../../utils/logs';
import {
findFirstLineBreakOutsideComment,
type RenderOptions,
renderStatementList
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { createHasEffectsContext } from '../ExecutionContext';
import type * as NodeType from './NodeType';
import {
doNotDeoptimize,
type IncludeChildren,
NodeBase,
onlyIncludeSelfNoDeoptimize,
type StatementNode
} from './shared/Node';
export default class Program extends NodeBase {
declare body: readonly StatementNode[];
declare sourceType: 'module';
declare type: NodeType.tProgram;
declare invalidAnnotations?: readonly RollupAnnotation[];
private hasCachedEffect: boolean | null = null;
private hasLoggedEffect = false;
hasCachedEffects(): boolean {
if (!this.included) {
return false;
}
return this.hasCachedEffect === null
? (this.hasCachedEffect = this.hasEffects(createHasEffectsContext()))
: this.hasCachedEffect;
}
hasEffects(context: HasEffectsContext): boolean {
for (const node of this.body) {
if (node.hasEffects(context)) {
if (this.scope.context.options.experimentalLogSideEffects && !this.hasLoggedEffect) {
this.hasLoggedEffect = true;
const { code, log, module } = this.scope.context;
log(
LOGLEVEL_INFO,
logFirstSideEffect(code, module.id, locate(code, node.start, { offsetLine: 1 })!),
node.start
);
}
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);
}
}
}
initialise() {
super.initialise();
if (this.invalidAnnotations)
for (const { start, end, type } of this.invalidAnnotations) {
this.scope.context.magicString.remove(start, end);
if (type === 'pure' || type === 'noSideEffects') {
this.scope.context.log(
LOGLEVEL_WARN,
logInvalidAnnotation(
this.scope.context.code.slice(start, end),
this.scope.context.module.id,
type
),
start
);
}
}
}
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) {
// Keep all consecutive lines that start with a comment
while (code.original[start] === '/' && /[*/]/.test(code.original[start + 1])) {
const firstLineBreak = findFirstLineBreakOutsideComment(
code.original.slice(start, this.body[0].start)
);
if (firstLineBreak[0] === -1) {
break;
}
start += firstLineBreak[1];
}
renderStatementList(this.body, code, start, this.end, options);
} else {
super.render(code, options);
}
}
}
Program.prototype.includeNode = onlyIncludeSelfNoDeoptimize;
Program.prototype.applyDeoptimizations = doNotDeoptimize;