-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfix-types-for-back-compat.js
49 lines (45 loc) · 1.71 KB
/
fix-types-for-back-compat.js
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
"use strict";
const typescript = require("typescript");
const fs = require("fs");
const program = typescript.createProgram(["index.d.ts"], {});
const source = program.getSourceFile("index.d.ts");
const printer = typescript.createPrinter();
const fix = context => node => {
const visit = node => {
if (
typescript.isIdentifier(node) &&
node.escapedText === "ResolveTypescriptPluginOptions"
) {
return typescript.factory.createQualifiedName(
typescript.factory.createIdentifier("ResolveTypescriptPlugin"),
typescript.factory.createIdentifier("ResolveTypescriptPluginOptions")
);
} else {
return typescript.visitEachChild(node, visit, context);
}
};
const visitTopLevel = node => {
if (
typescript.isInterfaceDeclaration(node) &&
node.name.escapedText === "ResolveTypescriptPluginOptions"
) {
return typescript.factory.createModuleDeclaration(
[],
[typescript.factory.createModifier(typescript.SyntaxKind.DeclareKeyword)],
typescript.factory.createIdentifier("ResolveTypescriptPlugin"),
typescript.factory.createModuleBlock([node]),
typescript.NodeFlags.Namespace
);
} else {
return typescript.visitEachChild(node, visit, context);
}
};
return typescript.visitEachChild(node, visitTopLevel, context);
};
if (source != null) {
typescript.transform(source, [fix]).transformed.forEach(node => {
if (typescript.isSourceFile(node)) {
fs.writeFileSync("index.d.ts", printer.printFile(node));
}
});
}