Skip to content

Commit d2fd736

Browse files
author
Michael Hladky
committed
refactor: add baseUrl options
1 parent 9b12e0f commit d2fd736

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

packages/models/tsconfig.lib.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"plugins": [
88
{
99
"transform": "./tools/jsdoc-annotation-transformer/dist",
10-
"afterDeclarations": true
10+
"afterDeclarations": true,
11+
"baseUrl": "https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md"
1112
}
1213
]
1314
},

tools/jsdoc-annotation-transformer/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export type Report = {
5252
"plugins": [
5353
{
5454
"transform": "./path/to/transformer/dist",
55-
"afterDeclarations": true
55+
"afterDeclarations": true,
56+
"baseUrl": "https://example.com/docs/api-reference.md"
5657
}
5758
]
5859
}
@@ -67,3 +68,4 @@ export type Report = {
6768
| ------------------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
6869
| `transform` | `string` | Yes | Path to the transformer module |
6970
| `afterDeclarations` | `boolean` | No | Set to `true` to run the transformer after TypeScript generates declaration files (`.d.ts`). This ensures JSDoc comments are added to the emitted type definitions. |
71+
| `baseUrl` | `string` | Yes | Base URL for documentation links (e.g., `https://example.com/docs/api-reference.md`) |

tools/jsdoc-annotation-transformer/src/lib/transformers.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import type * as ts from 'typescript';
33

44
const tsInstance: typeof ts = require('typescript');
55

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()}`;
6+
function generateJSDocComment(typeName: string, baseUrl: string): string {
7+
const markdownLink = `${baseUrl}#${typeName.toLowerCase()}`;
118
return `*
129
* Type Definition: \`${typeName}\`
1310
*
@@ -20,17 +17,25 @@ function generateJSDocComment(typeName: string): string {
2017

2118
function annotateTypeDefinitions(
2219
_program: ts.Program,
23-
_pluginConfig: PluginConfig,
20+
pluginConfig: PluginConfig,
2421
extras?: TransformerExtras,
2522
): ts.TransformerFactory<ts.SourceFile> {
23+
const baseUrl = pluginConfig.baseUrl as string | undefined;
24+
25+
if (!baseUrl) {
26+
throw new Error(
27+
'jsdoc-annotation-transformer: "baseUrl" option is required. ' +
28+
'Please configure it in your tsconfig.json plugins section.',
29+
);
30+
}
2631
const tsLib = extras?.ts ?? tsInstance;
2732
return (context: ts.TransformationContext) => {
2833
const visitor = (node: ts.Node): ts.Node => {
2934
if (
3035
tsLib.isTypeAliasDeclaration(node) ||
3136
tsLib.isInterfaceDeclaration(node)
3237
) {
33-
const jsDocComment = generateJSDocComment(node.name.text);
38+
const jsDocComment = generateJSDocComment(node.name.text, baseUrl);
3439
tsLib.addSyntheticLeadingComment(
3540
node,
3641
tsLib.SyntaxKind.MultiLineCommentTrivia,

0 commit comments

Comments
 (0)