Skip to content

Commit 29cf02a

Browse files
committed
feat(models): auto-generate JSDoc annotations
1 parent 6c7ed9f commit 29cf02a

File tree

12 files changed

+308
-3
lines changed

12 files changed

+308
-3
lines changed

package-lock.json

Lines changed: 171 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"prettier": "^3.4.1",
109109
"react": "18.3.1",
110110
"react-dom": "18.3.1",
111+
"ts-patch": "^3.3.0",
111112
"tsconfig-paths": "^4.2.0",
112113
"tsx": "^4.19.0",
113114
"type-fest": "^4.26.1",

packages/models/eslint.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import baseConfig from '../../eslint.config.js';
44
export default tseslint.config(
55
...baseConfig,
66
{
7-
files: ['**/*.ts'],
7+
files: ['/**/*.ts'],
88
languageOptions: {
99
parserOptions: {
1010
projectService: true,
@@ -18,4 +18,7 @@ export default tseslint.config(
1818
'@nx/dependency-checks': 'error',
1919
},
2020
},
21+
{
22+
ignores: ['packages/models/transformers/**/*.ts'],
23+
},
2124
);

packages/models/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"sourceRoot": "packages/models/src",
55
"projectType": "library",
6+
"implicitDependencies": ["models-transformers"],
67
"targets": {
78
"build": {
89
"executor": "@nx/js:tsc",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const baseConfig = require('../../../eslint.config.js').default;
2+
3+
module.exports = [
4+
...baseConfig,
5+
{
6+
files: ['**/*.json'],
7+
rules: {
8+
'@nx/dependency-checks': 'error',
9+
},
10+
},
11+
];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@code-pushup/models-transformers",
3+
"version": "0.0.0",
4+
"description": "TypeScript transformers enhancing models with JSDoc and schema metadata",
5+
"type": "commonjs",
6+
"main": "./src/index.js",
7+
"dependencies": {
8+
"typescript": "^5.5.4",
9+
"ts-patch": "^3.3.0"
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "models-transformers",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "packages/models/transformers/src",
5+
"projectType": "library",
6+
"targets": {
7+
"build": {
8+
"executor": "@nx/js:tsc",
9+
"outputs": ["{options.outputPath}"],
10+
"dependsOn": [{ "target": "pre-build" }],
11+
"options": {
12+
"outputPath": "dist/packages/models-transformers",
13+
"main": "packages/models/transformers/src/index.ts",
14+
"tsConfig": "packages/models/transformers/tsconfig.lib.json"
15+
}
16+
},
17+
"pre-build": {
18+
"executor": "nx:run-commands",
19+
"options": {
20+
"command": "npx ts-patch install"
21+
}
22+
}
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const transformers = require('./lib/transformers.js');
2+
3+
module.exports = transformers;
4+
module.exports.default = transformers;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"verbatimModuleSyntax": false
6+
},
7+
"files": [],
8+
"include": [],
9+
"references": [
10+
{
11+
"path": "./tsconfig.lib.json"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)