/
Program.ts
89 lines (82 loc) · 2.76 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
import { locate } from 'locate-character';
import type MagicString from 'magic-string';
import getCodeFrame from '../../utils/getCodeFrame';
import { getOriginalLocation } from '../../utils/getOriginalLocation';
import relativeId from '../../utils/relativeId';
import { type RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { createHasEffectsContext } 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: 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.context.options.experimentalLogSideEffects && !this.hasLoggedEffect) {
this.hasLoggedEffect = true;
const { code, module } = this.context;
const { line, column } = locate(code, node.start, { offsetLine: 1 });
console.log(
`First side effect in ${relativeId(
module.id
)} is at (${line}:${column})\n${getCodeFrame(code, line, column)}`
);
try {
const { column: originalColumn, line: originalLine } = getOriginalLocation(
module.sourcemapChain,
{ column, line }
);
if (originalLine !== line) {
console.log(
`Original location is at (${originalLine}:${originalColumn})\n${getCodeFrame(
module.originalCode,
originalLine,
originalColumn
)}\n`
);
}
} catch {
/* ignored */
}
console.log();
}
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() {}
}