-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
inline-namespace-module-block-transformer.ts
52 lines (43 loc) · 1.83 KB
/
inline-namespace-module-block-transformer.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
import {TS} from "../../../../../type/ts";
import {visitNode} from "./visitor/visit-node";
import {shouldDebugMetrics, shouldDebugSourceFile} from "../../../../../util/is-debug/should-debug";
import {logMetrics} from "../../../../../util/logging/log-metrics";
import {logTransformer} from "../../../../../util/logging/log-transformer";
import {preserveMeta} from "../../util/clone-node-with-meta";
import {DeclarationTransformer} from "../../declaration-bundler-options";
import {InlineNamespaceModuleBlockOptions} from "./inline-namespace-module-block-options";
export function inlineNamespaceModuleBlockTransformer({intentToAddImportDeclaration}: InlineNamespaceModuleBlockOptions): DeclarationTransformer {
return options => {
const {typescript, context, sourceFile, pluginOptions, printer} = options;
const fullBenchmark = shouldDebugMetrics(pluginOptions.debug, sourceFile) ? logMetrics(`Inlining ModuleBlock to be wrapped in a Namespace`, sourceFile.fileName) : undefined;
const transformationLog = shouldDebugSourceFile(pluginOptions.debug, sourceFile) ? logTransformer("Inlining ModuleBlock to be wrapped in a Namespace", sourceFile, printer) : undefined;
// Prepare some VisitorOptions
const visitorOptions = {
...options,
intentToAddImportDeclaration,
childContinuation: <U extends TS.Node>(node: U): U =>
typescript.visitEachChild(
node,
nextNode =>
visitNode({
...visitorOptions,
node: nextNode
}),
context
),
continuation: <U extends TS.Node>(node: U): U =>
visitNode({
...visitorOptions,
node
}) as U
};
const result = preserveMeta(
typescript.visitEachChild(sourceFile, nextNode => visitorOptions.continuation(nextNode), context),
sourceFile,
options
);
transformationLog?.finish(result);
fullBenchmark?.finish();
return result;
};
}