-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(models): auto-generate JSDoc annotations
- Loading branch information
1 parent
6c7ed9f
commit 29cf02a
Showing
12 changed files
with
308 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const transformers = require('./lib/transformers.js'); | ||
|
||
module.exports = transformers; | ||
module.exports.default = transformers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { PluginConfig, TransformerExtras } from 'ts-patch'; | ||
import type * as ts from 'typescript'; | ||
|
||
const tsInstance: typeof ts = require('typescript'); | ||
|
||
const BASE_URL = | ||
'https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md'; | ||
|
||
function generateJSDocComment(typeName: string): string { | ||
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( | ||
_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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters