|
| 1 | +import type { PluginConfig, TransformerExtras } from 'ts-patch'; |
| 2 | +import type * as ts from 'typescript'; |
| 3 | + |
| 4 | +const tsInstance: typeof ts = require('typescript'); |
| 5 | + |
| 6 | +const BASE_URL = |
| 7 | + 'https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md'; |
| 8 | + |
| 9 | +function generateJSDocComment(typeName: string): string { |
| 10 | + const markdownLink = `${BASE_URL}#${typeName.toLowerCase()}`; |
| 11 | + return `* |
| 12 | + * Type Definition: \`${typeName}\` |
| 13 | + * |
| 14 | + * This type is derived from a Zod schema and represents |
| 15 | + * the validated structure of \`${typeName}\` used within the application. |
| 16 | + * |
| 17 | + * @see {@link ${markdownLink}} |
| 18 | + `; |
| 19 | +} |
| 20 | + |
| 21 | +function annotateTypeDefinitions( |
| 22 | + _program: ts.Program, |
| 23 | + _pluginConfig: PluginConfig, |
| 24 | + extras?: TransformerExtras, |
| 25 | +): ts.TransformerFactory<ts.SourceFile> { |
| 26 | + const tsLib = extras?.ts ?? tsInstance; |
| 27 | + return (context: ts.TransformationContext) => { |
| 28 | + const visitor = (node: ts.Node): ts.Node => { |
| 29 | + if ( |
| 30 | + tsLib.isTypeAliasDeclaration(node) || |
| 31 | + tsLib.isInterfaceDeclaration(node) |
| 32 | + ) { |
| 33 | + const jsDocComment = generateJSDocComment(node.name.text); |
| 34 | + tsLib.addSyntheticLeadingComment( |
| 35 | + node, |
| 36 | + tsLib.SyntaxKind.MultiLineCommentTrivia, |
| 37 | + jsDocComment, |
| 38 | + true, |
| 39 | + ); |
| 40 | + return node; |
| 41 | + } |
| 42 | + return tsLib.visitEachChild(node, visitor, context); |
| 43 | + }; |
| 44 | + return (sourceFile: ts.SourceFile) => { |
| 45 | + return tsLib.visitNode(sourceFile, visitor, tsLib.isSourceFile); |
| 46 | + }; |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +module.exports = annotateTypeDefinitions; |
0 commit comments