diff options
author | Florian Dold <florian@dold.me> | 2022-10-04 19:07:36 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-10-04 19:07:36 +0200 |
commit | f4564b57acd04dd34751ced8ecb2bd10974452a6 (patch) | |
tree | 5e5befb01e5959aada4face4d162786bbb306653 /extract-tsdefs | |
parent | effcc17d92268b057ec1f643b4328ab0b2f3fedb (diff) | |
download | docs-f4564b57acd04dd34751ced8ecb2bd10974452a6.tar.gz docs-f4564b57acd04dd34751ced8ecb2bd10974452a6.tar.bz2 docs-f4564b57acd04dd34751ced8ecb2bd10974452a6.zip |
tweak wallet-core doc generation
Diffstat (limited to 'extract-tsdefs')
-rw-r--r-- | extract-tsdefs/extract.ts | 84 |
1 files changed, 59 insertions, 25 deletions
diff --git a/extract-tsdefs/extract.ts b/extract-tsdefs/extract.ts index ea133f30..5be82479 100644 --- a/extract-tsdefs/extract.ts +++ b/extract-tsdefs/extract.ts @@ -30,7 +30,11 @@ const walletRootDir = process.argv[2]; const outfile = process.argv[3]; const walletCoreDir = path.join(walletRootDir, "packages/taler-wallet-core"); -const excludedNames = new Set(["TalerErrorCode", "WalletBackupContentV1"]); +const excludedNames = new Set([ + "TalerErrorCode", + "WalletBackupContentV1", + "Array", +]); const configFile = ts.findConfigFile( walletCoreDir, @@ -76,6 +80,10 @@ interface PerOpGatherState { opName: string; nameSet: Set<string>; group: string; + /** + * Enum member declaration in the form 'Foo = "bar"'. + */ + enumMemberDecl: string | undefined; } interface GatherState { @@ -88,8 +96,12 @@ function gatherDecls( perOpState: PerOpGatherState ): void { switch (node.kind) { + case ts.SyntaxKind.EnumDeclaration: + // Always handled via parent + return; + case ts.SyntaxKind.Identifier: case ts.SyntaxKind.TypeReference: { - console.log(`typeref ${node.getText()}`); + console.log(`start typeref-or-id ${node.getText()}`); const type = checker.getTypeAtLocation(node); if (type.flags === ts.TypeFlags.String) { console.log("string!"); @@ -97,41 +109,31 @@ function gatherDecls( } const symbol = type.symbol || type.aliasSymbol; if (!symbol) { - console.log(`no symbol for ${node.getText()}`); + console.log(`no type symbol for ${node.getText()}`); break; } const name = symbol.name; + console.log(`symbol name: ${type.symbol?.name}`); + console.log(`alias symbol name: ${type.aliasSymbol?.name}`); if (perOpState.nameSet.has(name)) { + console.log("already found!"); break; } perOpState.nameSet.add(name); if (excludedNames.has(name)) { + console.log("excluded!"); break; } const decls = symbol.getDeclarations(); decls?.forEach((decl) => { - console.log(`decl kind ${ts.SyntaxKind[node.kind]}`); - console.log(`decl for ${node.getText()}`); const sourceFilename = decl.getSourceFile().fileName; if (path.basename(sourceFilename).startsWith("lib.")) { return; } - console.log(`decl source ${decl.getSourceFile().fileName}`); - console.log(`mod name ${decl.getSourceFile().moduleName}`); switch (decl.kind) { case ts.SyntaxKind.EnumMember: { - const parentType = checker.getTypeAtLocation(decl.parent); - const declText = printer.printNode( - ts.EmitHint.Unspecified, - decl, - decl.getSourceFile()! - ); - gatherState.declTexts.set( - name, - `// Enum value:\n// ${ - parentType.symbol.name || parentType.aliasSymbol?.name - }.${declText}` - ); + gatherDecls(decl.parent, gatherState, perOpState); + console.log("enum member", decl.getText()); break; } case ts.SyntaxKind.InterfaceDeclaration: @@ -151,16 +153,43 @@ function gatherDecls( break; } gatherDecls(decl, gatherState, perOpState); + console.log(`end typeref-or-id ${node.getText()}`); }); break; } default: - node.forEachChild((child) => { - gatherDecls(child, gatherState, perOpState); - }); - //console.log(`// unknown node kind ${ts.SyntaxKind[node.kind]}`); - return; + break; } + console.log(`syntax children for ${node.getText()}`); + node.forEachChild((child) => { + console.log(`syntax child: ${ts.SyntaxKind[child.kind]}`); + gatherDecls(child, gatherState, perOpState); + }); + //console.log(`// unknown node kind ${ts.SyntaxKind[node.kind]}`); + return; +} + +function getOpEnumDecl(decl: ts.Declaration): string | undefined { + let enumMemberDecl: undefined | string = undefined; + function walk(node: ts.Node) { + node.forEachChild((x) => { + console.log(`child kind: ${ts.SyntaxKind[x.kind]}`); + console.log(x.getText()); + switch (x.kind) { + case ts.SyntaxKind.PropertySignature: { + const sig = x as ts.PropertySignature; + if (sig.name.getText() == "op") { + const type = checker.getTypeFromTypeNode(sig.type!); + enumMemberDecl = type.symbol.declarations![0]!.getText(); + } + break; + } + } + walk(x); + }); + } + walk(decl); + return enumMemberDecl; } const main = async () => { @@ -178,11 +207,12 @@ const main = async () => { } const decls = v.getDeclarations(); decls?.forEach((decl) => { + console.log(`export decl, kind ${ts.SyntaxKind[decl.kind]}`); + const commentRanges = ts.getLeadingCommentRanges( sourceFile.getFullText(), decl.getFullStart() ); - console.log(commentRanges); commentRanges?.forEach((r) => { const text = sourceFile.getFullText().slice(r.pos, r.end); console.log("comment text:", text); @@ -199,12 +229,16 @@ const main = async () => { opName: v.name, nameSet: new Set<string>(), group: currentGroup, + enumMemberDecl: getOpEnumDecl(decl), }; let declText = printer.printNode( ts.EmitHint.Unspecified, decl, decl.getSourceFile()! ); + if (perOpState.enumMemberDecl) { + declText = declText + `\n// ${perOpState.enumMemberDecl}\n`; + } console.log("replacing group in", declText); // Remove group comments declText = declText.replace(/\/\/ group: [^\n]*[\n]/m, ""); |