Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(models): auto-generate JSDoc annotations #928

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 171 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"prettier": "^3.4.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"ts-patch": "^3.3.0",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.19.0",
"type-fest": "^4.26.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/models/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import baseConfig from '../../eslint.config.js';
export default tseslint.config(
...baseConfig,
{
files: ['**/*.ts'],
files: ['/**/*.ts'],
languageOptions: {
parserOptions: {
projectService: true,
Expand All @@ -18,4 +18,7 @@ export default tseslint.config(
'@nx/dependency-checks': 'error',
},
},
{
ignores: ['packages/models/transformers/**/*.ts'],
},
);
1 change: 1 addition & 0 deletions packages/models/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/models/src",
"projectType": "library",
"implicitDependencies": ["models-transformers"],
"targets": {
"build": {
"executor": "@nx/js:tsc",
Expand Down
11 changes: 11 additions & 0 deletions packages/models/transformers/eslint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const baseConfig = require('../../../eslint.config.js').default;

module.exports = [
...baseConfig,
{
files: ['**/*.json'],
rules: {
'@nx/dependency-checks': 'error',
},
},
];
11 changes: 11 additions & 0 deletions packages/models/transformers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@code-pushup/models-transformers",
"version": "0.0.0",
"description": "TypeScript transformers enhancing models with JSDoc and schema metadata",
"type": "commonjs",
"main": "./src/index.js",
"dependencies": {
"typescript": "^5.5.4",
"ts-patch": "^3.3.0"
}
}
24 changes: 24 additions & 0 deletions packages/models/transformers/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "models-transformers",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/models/transformers/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"dependsOn": [{ "target": "pre-build" }],
"options": {
"outputPath": "dist/packages/models-transformers",
"main": "packages/models/transformers/src/index.ts",
"tsConfig": "packages/models/transformers/tsconfig.lib.json"
}
},
"pre-build": {
"executor": "nx:run-commands",
"options": {
"command": "npx ts-patch install"
}
}
}
}
4 changes: 4 additions & 0 deletions packages/models/transformers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const transformers = require('./lib/transformers.js');

Check failure on line 1 in packages/models/transformers/src/index.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.

Check warning on line 1 in packages/models/transformers/src/index.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for transformers

module.exports = transformers;
module.exports.default = transformers;

Check warning on line 4 in packages/models/transformers/src/index.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 1-4 are not covered in any test case.
50 changes: 50 additions & 0 deletions packages/models/transformers/src/lib/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { PluginConfig, TransformerExtras } from 'ts-patch';

Check failure on line 1 in packages/models/transformers/src/lib/transformers.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
import type * as ts from 'typescript';

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

Check warning on line 4 in packages/models/transformers/src/lib/transformers.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for tsInstance

const BASE_URL =

Check warning on line 6 in packages/models/transformers/src/lib/transformers.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for BASE_URL
'https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md';

function generateJSDocComment(typeName: string): string {

Check warning on line 9 in packages/models/transformers/src/lib/transformers.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Functions coverage

Missing functions documentation for generateJSDocComment
const markdownLink = `${BASE_URL}#${typeName.toLowerCase()}`;
return `*
* Type Definition: \`${typeName}\`
*
* This type is derived from a Zod schema and represents
* the validated structure of \`${typeName}\` used within the application.
*
* @see {@link ${markdownLink}}
`;
}

function annotateTypeDefinitions(

Check warning on line 21 in packages/models/transformers/src/lib/transformers.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Functions coverage

Missing functions documentation for annotateTypeDefinitions
_program: ts.Program,
_pluginConfig: PluginConfig,
extras?: TransformerExtras,
): ts.TransformerFactory<ts.SourceFile> {
const tsLib = extras?.ts ?? tsInstance;
return (context: ts.TransformationContext) => {
const visitor = (node: ts.Node): ts.Node => {
if (
tsLib.isTypeAliasDeclaration(node) ||
tsLib.isInterfaceDeclaration(node)
) {
const jsDocComment = generateJSDocComment(node.name.text);
tsLib.addSyntheticLeadingComment(
node,
tsLib.SyntaxKind.MultiLineCommentTrivia,
jsDocComment,
true,
);
return node;
}
return tsLib.visitEachChild(node, visitor, context);
};
return (sourceFile: ts.SourceFile) => {
return tsLib.visitNode(sourceFile, visitor, tsLib.isSourceFile);
};
};
}

module.exports = annotateTypeDefinitions;

Check warning on line 50 in packages/models/transformers/src/lib/transformers.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 1-50 are not covered in any test case.
14 changes: 14 additions & 0 deletions packages/models/transformers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"verbatimModuleSyntax": false
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
}
]
}
10 changes: 10 additions & 0 deletions packages/models/transformers/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/packages/models-transformers",
"rootDir": "./",
"module": "commonjs",
"types": ["node"]
},
"include": ["src/**/*.ts"]
}
8 changes: 7 additions & 1 deletion packages/models/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
"types": ["node"],
"plugins": [
{
"transform": "./dist/packages/models-transformers",
"afterDeclarations": true
}
]
},
"include": ["src/**/*.ts"],
"exclude": [
Expand Down
Loading