Debian Codegraph @

types/docs-generator/src/analyzer.ts

raw symbols SCIP
1 import ts from "typescript";
2 import * as tsdoc from "@microsoft/tsdoc";
3 import { DocManager, tagDefinitions } from "./DocManager";
4
5 function getFirstParentPropertySignatureOrTypeAliasDeclaration(
6 node: ts.Node
7 ): ts.PropertySignature | ts.TypeAliasDeclaration | undefined {
8 let n = node;
9 do {
10 if (ts.isPropertySignature(n) || ts.isTypeAliasDeclaration(n)) {
11 return n;
12 }
13 n = n.parent;
14 } while (n);
15 return undefined;
16 }
17
18 export function analyze(path: string): string {
19 const program = ts.createProgram([path], { target: ts.ScriptTarget.ESNext });
20 const sourceFile = program.getSourceFile(path);
21 const checker = program.getTypeChecker();
22
23 if (!sourceFile) {
24 throw new Error("Error retrieving source file");
25 }
26
27 const typeArgumentsReplacer = (
28 rootTypeAlias: ts.TypeAliasDeclaration,
29 typeArguments: ts.NodeArray<ts.TypeNode>
30 ) => {
31 const typeParamsMap = new Map<ts.__String, ts.TypeNode>();
32 for (const [index, param] of Array.from((rootTypeAlias.typeParameters ?? []).entries())) {
33 typeParamsMap.set(
34 param.name.escapedText,
35 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
36 typeArguments[index] ? typeArguments[index] : param.default!
37 );
38 }
39 return (ctx: ts.TransformationContext) =>
40 (typeNode: ts.TypeNode): ts.TypeNode => {
41 const visit = (node: ts.Node): ts.Node => {
42 if (
43 ts.isTypeReferenceNode(node) &&
44 ts.isIdentifier(node.typeName) &&
45 typeParamsMap.get(node.typeName.escapedText)
46 ) {
47 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
48 return typeParamsMap.get(node.typeName.escapedText)!;
49 }
50 return ts.visitEachChild(node, visit, ctx);
51 };
52
53 return ts.visitNode(typeNode, visit) as ts.TypeNode;
54 };
55 };
56
57 const typeReferenceTransformer: ts.TransformerFactory<ts.SourceFile> =
58 (ctx: ts.TransformationContext) => (sourceNode: ts.SourceFile) => {
59 const sourceText = sourceNode.getFullText();
60 const visit = (node: ts.Node): ts.Node => {
61 if (ts.isTypeReferenceNode(node)) {
62 // early return if the current node parent has an irreplaced tag or if the current node is the same as on of the parents
63 const parent = getFirstParentPropertySignatureOrTypeAliasDeclaration(node);
64 let docComment: tsdoc.DocComment | undefined;
65 if (parent) {
66 docComment = DocManager.parseFirstDocComment(parent, sourceText);
67 if (docComment && docComment.modifierTagSet.hasTag(tagDefinitions.irreplacedTag)) {
68 return ts.visitEachChild(node, visit, ctx);
69 }
70 }
71
72 const type = checker.getTypeAtLocation(node.typeName);
73 const declaration = type.aliasSymbol?.declarations?.[0];
74 if (
75 declaration &&
76 ts.isTypeAliasDeclaration(declaration) &&
77 // @ts-expect-error: parent is not property of ts.Symbol
78 type.aliasSymbol.parent?.escapedName === "Fig"
79 ) {
80 const declarationDocComment = DocManager.parseFirstDocComment(declaration, sourceText);
81 if (
82 !declarationDocComment ||
83 !declarationDocComment.modifierTagSet.hasTag(tagDefinitions.irreplaceableTag)
84 ) {
85 let newType = declaration.type;
86 if (node.typeArguments && node.typeArguments.length > 0) {
87 const { transformed } = ts.transform(declaration.type, [
88 typeArgumentsReplacer(declaration, node.typeArguments),
89 ]);
90 [newType] = transformed;
91 }
92
93 // if the @replaceFirstLevel is specified we just return the first level type reference
94 if (
95 docComment &&
96 docComment.modifierTagSet.hasTag(tagDefinitions.replaceFirstLevelTag)
97 ) {
98 return newType;
99 }
100 // we revisit the node since some replaced TypeReferences may be TypeReferences
101 return ts.visitNode(newType, visit);
102 }
103 }
104 }
105 return ts.visitEachChild(node, visit, ctx);
106 };
107
108 return ts.visitNode(sourceNode, visit) as ts.SourceFile;
109 };
110
111 const { transformed } = ts.transform<ts.SourceFile>(sourceFile, [typeReferenceTransformer]);
112 const printer = ts.createPrinter();
113 return printer.printFile(transformed[0]);
114 }