Skip to content

Commit f74d2dd

Browse files
BioPhotonJohn Doematejchalk
authored
chore: move transformers to new project (#1163)
This PR moves the transformer logic into tools and makes it configurable Followup PR: #1162 --------- Co-authored-by: John Doe <john.doe@example.com> Co-authored-by: Matěj Chalk <34691111+matejchalk@users.noreply.github.com>
1 parent a863abb commit f74d2dd

File tree

12 files changed

+133
-23
lines changed

12 files changed

+133
-23
lines changed

packages/models/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependsOn": [
2222
"^build",
2323
"generate-docs",
24-
{ "projects": "models-transformers", "target": "build" }
24+
{ "projects": "zod2md-jsdocs", "target": "build" }
2525
]
2626
},
2727
"lint": {},

packages/models/tsconfig.lib.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"types": ["node"],
77
"plugins": [
88
{
9-
"transform": "./packages/models/transformers/dist",
10-
"afterDeclarations": true
9+
"transform": "./tools/zod2md-jsdocs/dist",
10+
"afterDeclarations": true,
11+
"baseUrl": "https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md"
1112
}
1213
]
1314
},

tools/zod2md-jsdocs/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# @code-pushup/zod2md-jsdocs
2+
3+
TypeScript transformer plugin that automatically enhances type definitions with JSDoc comments and schema metadata.
4+
5+
## Purpose
6+
7+
This package provides a TypeScript compiler transformer that automatically adds JSDoc documentation to type aliases and interfaces during compilation. It's designed to improve developer experience by injecting helpful metadata and documentation links directly into generated type definitions.
8+
9+
## How It Works
10+
11+
The [TS transformer](https://github.com/itsdouges/typescript-transformer-handbook) hooks into the TypeScript compilation process using `ts-patch` and automatically adds JSDoc comments above type definitions. Each comment includes:
12+
13+
- The type name
14+
- A description explaining the type is derived from a Zod schema
15+
- A link to the type reference documentation
16+
17+
## Example
18+
19+
Given a type definition like:
20+
21+
```typescript
22+
export type Report = {
23+
// ... type properties
24+
};
25+
```
26+
27+
The transformer automatically generates:
28+
29+
```typescript
30+
/**
31+
* Type Definition: `Report`
32+
*
33+
* This type is derived from a Zod schema and represents
34+
* the validated structure of `Report` used within the application.
35+
*
36+
* @see {@link https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md#report}
37+
*/
38+
export type Report = {
39+
// ... type properties
40+
};
41+
```
42+
43+
## Usage
44+
45+
1. `ts-patch install`
46+
47+
2. Add the transformer to your `tsconfig.json`:
48+
49+
```json
50+
{
51+
"compilerOptions": {
52+
"plugins": [
53+
{
54+
"transform": "./path/to/transformer/dist",
55+
"afterDeclarations": true,
56+
"baseUrl": "https://example.com/docs/api-reference.md"
57+
}
58+
]
59+
}
60+
}
61+
```
62+
63+
3. Build your TypeScript project. The transformer will run automatically and add JSDoc comments to your type definitions.
64+
65+
### Options
66+
67+
| Option | Type | Required | Description |
68+
| ------------------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
69+
| `transform` | `string` | Yes | Path to the transformer module |
70+
| `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`) |

packages/models/transformers/eslint.config.cjs renamed to tools/zod2md-jsdocs/eslint.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const baseConfig = require('../../../eslint.config.js').default;
1+
const baseConfig = require('../../eslint.config.js').default;
22

33
module.exports = [
44
...baseConfig,

packages/models/transformers/package.json renamed to tools/zod2md-jsdocs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@code-pushup/models-transformers",
2+
"name": "@code-pushup/zod2md-jsdocs",
33
"version": "0.0.0",
44
"description": "TypeScript transformers enhancing models with JSDoc and schema metadata",
55
"type": "commonjs",

packages/models/transformers/project.json renamed to tools/zod2md-jsdocs/project.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "models-transformers",
3-
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4-
"sourceRoot": "packages/models/transformers/src",
2+
"name": "zod2md-jsdocs",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "tools/zod2md-jsdocs/src",
55
"projectType": "library",
66
"targets": {
77
"build": {
88
"executor": "@nx/js:tsc",
99
"outputs": ["{options.outputPath}"],
1010
"dependsOn": ["pre-build"],
1111
"options": {
12-
"outputPath": "packages/models/transformers/dist",
13-
"main": "packages/models/transformers/src/index.ts",
14-
"tsConfig": "packages/models/transformers/tsconfig.lib.json"
12+
"outputPath": "tools/zod2md-jsdocs/dist",
13+
"main": "tools/zod2md-jsdocs/src/index.ts",
14+
"tsConfig": "tools/zod2md-jsdocs/tsconfig.lib.json"
1515
}
1616
},
1717
"pre-build": {
File renamed without changes.

packages/models/transformers/src/lib/transformers.ts renamed to tools/zod2md-jsdocs/src/lib/transformers.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,39 @@ 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}\`
13-
*
14-
* This type is derived from a Zod schema and represents
10+
*
11+
* This type is derived from a Zod schema and represents
1512
* the validated structure of \`${typeName}\` used within the application.
16-
*
13+
*
1714
* @see {@link ${markdownLink}}
1815
`;
1916
}
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+
'zod2md-jsdocs: "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,

packages/models/transformers/tsconfig.json renamed to tools/zod2md-jsdocs/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "../../../tsconfig.base.json",
2+
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
44
"module": "commonjs",
55
"verbatimModuleSyntax": false

packages/models/transformers/tsconfig.lib.json renamed to tools/zod2md-jsdocs/tsconfig.lib.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"outDir": "./dist",
55
"rootDir": "./",
66
"module": "commonjs",
7-
"types": ["node"]
7+
"types": ["node"],
8+
"esModuleInterop": true
89
},
910
"include": ["src/**/*.ts"]
1011
}

0 commit comments

Comments
 (0)