forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.ts
More file actions
97 lines (82 loc) · 3.44 KB
/
node.ts
File metadata and controls
97 lines (82 loc) · 3.44 KB
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
import {
Bundle,
Debug,
EmitHint,
isSourceFile,
map,
ModuleKind,
Node,
SourceFile,
SyntaxKind,
TransformationContext,
transformECMAScriptModule,
transformModule,
} from "../../_namespaces/ts";
/** @internal */
export function transformNodeModule(context: TransformationContext) {
const previousOnSubstituteNode = context.onSubstituteNode;
const previousOnEmitNode = context.onEmitNode;
const esmTransform = transformECMAScriptModule(context);
const esmOnSubstituteNode = context.onSubstituteNode;
const esmOnEmitNode = context.onEmitNode;
context.onSubstituteNode = previousOnSubstituteNode;
context.onEmitNode = previousOnEmitNode;
const cjsTransform = transformModule(context);
const cjsOnSubstituteNode = context.onSubstituteNode;
const cjsOnEmitNode = context.onEmitNode;
context.onSubstituteNode = onSubstituteNode;
context.onEmitNode = onEmitNode;
context.enableSubstitution(SyntaxKind.SourceFile);
context.enableEmitNotification(SyntaxKind.SourceFile);
let currentSourceFile: SourceFile | undefined;
return transformSourceFileOrBundle;
function onSubstituteNode(hint: EmitHint, node: Node) {
if (isSourceFile(node)) {
currentSourceFile = node;
// Neither component transform wants substitution notifications for `SourceFile`s, and, in fact, relies on
// the source file emit notification to setup scope variables for substitutions (so we _cannot_ call their substitute
// functions on source files safely, as that context only gets setup in a later pipeline phase!)
return previousOnSubstituteNode(hint, node);
}
else {
if (!currentSourceFile) {
return previousOnSubstituteNode(hint, node);
}
if (currentSourceFile.impliedNodeFormat === ModuleKind.ESNext) {
return esmOnSubstituteNode(hint, node);
}
return cjsOnSubstituteNode(hint, node);
}
}
function onEmitNode(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void {
if (isSourceFile(node)) {
currentSourceFile = node;
}
if (!currentSourceFile) {
return previousOnEmitNode(hint, node, emitCallback);
}
if (currentSourceFile.impliedNodeFormat === ModuleKind.ESNext) {
return esmOnEmitNode(hint, node, emitCallback);
}
return cjsOnEmitNode(hint, node, emitCallback);
}
function getModuleTransformForFile(file: SourceFile): (typeof esmTransform) {
return file.impliedNodeFormat === ModuleKind.ESNext ? esmTransform : cjsTransform;
}
function transformSourceFile(node: SourceFile) {
if (node.isDeclarationFile) {
return node;
}
currentSourceFile = node;
const result = getModuleTransformForFile(node)(node);
currentSourceFile = undefined;
Debug.assert(isSourceFile(result));
return result;
}
function transformSourceFileOrBundle(node: SourceFile | Bundle) {
return node.kind === SyntaxKind.SourceFile ? transformSourceFile(node) : transformBundle(node);
}
function transformBundle(node: Bundle) {
return context.factory.createBundle(map(node.sourceFiles, transformSourceFile), node.prepends);
}
}