-
Notifications
You must be signed in to change notification settings - Fork 189
/
createStructurePrinterFactory.ts
107 lines (96 loc) · 4.46 KB
/
createStructurePrinterFactory.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
/**
* Code Manipulation - Creates the StructurePrinterFactory
* --------------------------------------------------------
* Automatically maintains this class based on changes in the application.
* --------------------------------------------------------
*/
import { tsMorph } from "@ts-morph/scripts";
import { ArrayUtils } from "@ts-morph/common";
import { TsMorphInspector } from "../inspectors";
export function createStructurePrinterFactory(inspector: TsMorphInspector) {
const project = inspector.getProject();
const sourceFile = project.getSourceFileOrThrow("StructurePrinterFactory.ts");
sourceFile.removeText();
// add imports
sourceFile.addImportDeclarations([{
namedImports: ["Memoize"],
moduleSpecifier: "@ts-morph/common"
}, {
namespaceImport: "structurePrinters",
moduleSpecifier: sourceFile.getRelativePathAsModuleSpecifierTo(project.getSourceFileOrThrow("src/structurePrinters/index.ts"))
}, {
namedImports: ["SupportedFormatCodeSettings"],
moduleSpecifier: sourceFile.getRelativePathAsModuleSpecifierTo(project.getSourceFileOrThrow("src/options/index.ts"))
}]);
sourceFile.addClass({
docs: [{ description: "Cached lazy factory for StructurePrinters." }],
isExported: true,
name: "StructurePrinterFactory",
ctors: [{
parameters: [{ isReadonly: true, scope: tsMorph.Scope.Private, name: "_getFormatCodeSettings", type: "() => SupportedFormatCodeSettings" }]
}],
methods: [{
name: "getFormatCodeSettings",
returnType: "SupportedFormatCodeSettings",
statements: ["return this._getFormatCodeSettings();"]
}, ...getMethods()]
});
sourceFile.insertText(0, writer => writer.writeLine("// DO NOT EDIT - Automatically maintained by createStructurePrinterFactory.ts"));
function getMethods() {
const structurePrinters = ArrayUtils.flatten(Array.from(project.getSourceFileOrThrow("./src/structurePrinters/index.ts")
.getExportedDeclarations()
.values()))
.filter(tsMorph.Node.isClassDeclaration)
.filter(c => isAllowedStructurePrinter(c.getNameOrThrow()));
const methods: tsMorph.MethodDeclarationStructure[] = [];
for (const structurePrinter of structurePrinters) {
const ctor = structurePrinter.getConstructors()[0] ?? structurePrinter.getBaseClassOrThrow().getConstructors()[0];
const ctorParams = ctor?.getParameters() ?? [];
const exposedCtorParams = ctorParams.filter(exposeCtorParam);
const name = structurePrinter.getNameOrThrow();
methods.push({
kind: tsMorph.StructureKind.Method,
decorators: [{ name: "Memoize" }],
name: `for${name.replace(/StructurePrinter$/, "")}`,
returnType: `structurePrinters.${name}`,
statements: [`return new structurePrinters.${name}(${ctorParams.map(ctorParamToArgument).join(", ")});`],
parameters: exposedCtorParams.map(p => ({
name: p.getName(),
type: getTypeText(p),
hasQuestionToken: p.isOptional()
}))
});
}
return methods;
function exposeCtorParam(ctorParam: tsMorph.ParameterDeclaration) {
const typeName = getTypeText(ctorParam);
if (typeName === "StructurePrinterFactory")
return false;
return true;
}
function ctorParamToArgument(ctorParam: tsMorph.ParameterDeclaration) {
const typeName = getTypeText(ctorParam);
if (typeName === "StructurePrinterFactory")
return "this";
return ctorParam.getName();
}
function getTypeText(param: tsMorph.ParameterDeclaration) {
const typeNode = param.getTypeNode();
return typeNode?.getText() ?? param.getType().getText();
}
}
function isAllowedStructurePrinter(name: string) {
switch (name) {
case "Printer":
case "NodePrinter":
case "BlankLineFormattingStructuresPrinter":
case "NewLineFormattingStructuresPrinter":
case "SpaceFormattingStructuresPrinter":
case "CommaSeparatedStructuresPrinter":
case "CommaNewLineSeparatedStructuresPrinter":
case "StringStructurePrinter":
return false;
}
return true;
}
}