Skip to content

Commit 16fe968

Browse files
authored
Add codemod to propagate JSDoc comments to dual signatures (Effect-TS#3697)
1 parent 3b5b332 commit 16fe968

6 files changed

Lines changed: 114 additions & 2 deletions

File tree

.github/workflows/check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
- uses: actions/checkout@v4
3434
- name: Install dependencies
3535
uses: ./.github/actions/setup
36+
- run: pnpm codemod
3637
- run: pnpm check
3738
- run: pnpm dtslint
3839

@@ -61,6 +62,7 @@ jobs:
6162
- uses: actions/checkout@v4
6263
- name: Install dependencies
6364
uses: ./.github/actions/setup
65+
- run: pnpm codemod
6466
- uses: oven-sh/setup-bun@v1
6567
if: matrix.runtime == 'Bun'
6668
with:

.github/workflows/snapshot.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ jobs:
1616
- uses: actions/checkout@v4
1717
- name: Install dependencies
1818
uses: ./.github/actions/setup
19+
- name: Run codemods
20+
run: pnpm codemod
1921
- name: Build package
2022
run: pnpm build
2123
- name: Create snapshot
2224
id: snapshot
23-
run: pnpx pkg-pr-new@0.0.17 publish --pnpm --comment=off ./packages/*
25+
run: pnpx pkg-pr-new@0.0.28 publish --pnpm --comment=off ./packages/*

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"clean": "node scripts/clean.mjs",
1010
"codegen": "pnpm --recursive --parallel run codegen",
11+
"codemod": "node scripts/codemod.mjs",
1112
"build": "tsc -b tsconfig.build.json && pnpm --recursive --parallel run build",
1213
"circular": "node scripts/circular.mjs",
1314
"test": "vitest",
@@ -20,7 +21,7 @@
2021
"dtslint": "pnpm --recursive --parallel run dtslint",
2122
"dtslint-clean": "dtslint --installAll",
2223
"changeset-version": "changeset version && node scripts/version.mjs",
23-
"changeset-publish": "pnpm build && TEST_DIST= pnpm vitest && changeset publish"
24+
"changeset-publish": "pnpm codemod && pnpm lint-fix && pnpm build && TEST_DIST= pnpm vitest && changeset publish"
2425
},
2526
"resolutions": {
2627
"dependency-tree": "^10.0.9",
@@ -51,13 +52,15 @@
5152
"@eslint/compat": "1.1.1",
5253
"@eslint/eslintrc": "3.1.0",
5354
"@eslint/js": "9.9.1",
55+
"@types/jscodeshift": "^0.11.11",
5456
"@types/node": "^22.5.4",
5557
"@typescript-eslint/eslint-plugin": "^7.16.0",
5658
"@typescript-eslint/parser": "^7.16.0",
5759
"@vitest/browser": "^2.0.5",
5860
"@vitest/coverage-v8": "^2.0.5",
5961
"@vitest/expect": "^2.0.5",
6062
"@vitest/web-worker": "^2.0.5",
63+
"ast-types": "^0.14.2",
6164
"babel-plugin-annotate-pure-calls": "^0.4.0",
6265
"eslint": "^9.9.1",
6366
"eslint-import-resolver-typescript": "^3.6.3",
@@ -68,6 +71,7 @@
6871
"eslint-plugin-sort-destructure-keys": "^2.0.0",
6972
"fast-check": "^3.21.0",
7073
"glob": "^11.0.0",
74+
"jscodeshift": "^0.16.1",
7175
"madge": "^8.0.0",
7276
"playwright": "^1.46.0",
7377
"prettier": "^3.3.3",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/codemod.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @ts-check
2+
import * as Glob from "glob"
3+
import jscodeshift from "jscodeshift/src/Runner.js"
4+
import * as Fs from "node:fs"
5+
import * as Path from "node:path"
6+
7+
const packageJsonPath = Path.resolve("package.json")
8+
const packageJson = JSON.parse(Fs.readFileSync(packageJsonPath, "utf-8"))
9+
const workspaces = Glob.globSync(packageJson["workspaces"])
10+
const packages = workspaces.map((workspace) => workspace.replace("packages/", ""))
11+
const pattern = `packages/{${packages.join(",")}}/src/**/*.ts`
12+
13+
const paths = Glob.globSync(pattern, {
14+
ignore: ["**/internal/**"]
15+
}).map((path) => Path.resolve(path))
16+
17+
const transformer = Path.resolve("scripts/codemods/jsdoc.ts")
18+
19+
jscodeshift.run(Path.resolve(transformer), paths, {
20+
babel: true,
21+
parser: "ts"
22+
})

scripts/codemods/jsdoc.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type cs from "jscodeshift"
2+
3+
//
4+
// this is needed to resolve a bug in jscodeshift that
5+
// forgets to traverse type parameters in call expressions
6+
//
7+
declare module "ast-types/gen/namedTypes.js" {
8+
namespace namedTypes {
9+
interface CallExpression extends TSHasOptionalTypeParameterInstantiation {}
10+
}
11+
}
12+
13+
export default function transformer(file: cs.FileInfo, api: cs.API) {
14+
const j = api.jscodeshift
15+
16+
const root = j(file.source)
17+
18+
root.find(j.ExportNamedDeclaration, {
19+
declaration: {
20+
type: "VariableDeclaration",
21+
declarations: [{
22+
type: "VariableDeclarator",
23+
id: {
24+
type: "Identifier",
25+
typeAnnotation: {
26+
type: "TSTypeAnnotation",
27+
typeAnnotation: {
28+
type: "TSTypeLiteral",
29+
members: [{ type: "TSCallSignatureDeclaration" }]
30+
}
31+
}
32+
}
33+
}]
34+
}
35+
}).forEach((path) => {
36+
const comments = path.node.comments ?? []
37+
j(path).find(j.TSCallSignatureDeclaration).forEach((path) => {
38+
// Don't override comments if they already exist
39+
if (!Array.isArray(path.node.comments)) {
40+
path.node.comments = comments
41+
}
42+
})
43+
})
44+
45+
root.find(j.ExportNamedDeclaration, {
46+
declaration: {
47+
type: "VariableDeclaration",
48+
declarations: [{
49+
type: "VariableDeclarator",
50+
init: {
51+
type: "CallExpression",
52+
callee: {
53+
type: "Identifier",
54+
name: "dual"
55+
}
56+
}
57+
}]
58+
}
59+
}).forEach((path) => {
60+
const comments = path.node.comments ?? []
61+
j(path).find(j.CallExpression).forEach((path) => {
62+
path.node.typeParameters?.params.forEach((param) => {
63+
// Don't override comments if they already exist
64+
if (!Array.isArray(param.comments)) {
65+
param.comments = comments
66+
}
67+
})
68+
})
69+
})
70+
71+
return root.toSource()
72+
}

0 commit comments

Comments
 (0)