Skip to content

Commit

Permalink
add jsdoc codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 committed Sep 27, 2024
1 parent ce8b810 commit 99b72e3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
- uses: actions/checkout@v4
- name: Install dependencies
uses: ./.github/actions/setup
- run: pnpm codemod
- run: pnpm check
- run: pnpm dtslint

Expand All @@ -44,6 +45,7 @@ jobs:
- uses: actions/checkout@v4
- name: Install dependencies
uses: ./.github/actions/setup
- run: pnpm codemod
- run: pnpm circular
- run: pnpm lint

Expand All @@ -61,6 +63,7 @@ jobs:
- uses: actions/checkout@v4
- name: Install dependencies
uses: ./.github/actions/setup
- run: pnpm codemod
- uses: oven-sh/setup-bun@v1
if: matrix.runtime == 'Bun'
with:
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@
"@eslint/compat": "1.1.1",
"@eslint/eslintrc": "3.1.0",
"@eslint/js": "9.9.1",
"@types/jscodeshift": "^0.11.11",
"@types/node": "^22.5.4",
"@typescript-eslint/eslint-plugin": "^7.16.0",
"@typescript-eslint/parser": "^7.16.0",
"@vitest/browser": "^2.0.5",
"@vitest/coverage-v8": "^2.0.5",
"@vitest/expect": "^2.0.5",
"@vitest/web-worker": "^2.0.5",
"ast-types": "^0.14.2",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"eslint": "^9.9.1",
"eslint-import-resolver-typescript": "^3.6.3",
Expand All @@ -68,6 +70,7 @@
"eslint-plugin-sort-destructure-keys": "^2.0.0",
"fast-check": "^3.21.0",
"glob": "^11.0.0",
"jscodeshift": "^0.16.1",
"madge": "^8.0.0",
"playwright": "^1.46.0",
"prettier": "^3.3.3",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

66 changes: 66 additions & 0 deletions scripts/codemods/jsdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type cs from "jscodeshift"

//
// this is needed to resolve a bug in jscodeshift that
// forgets to traverse type parameters in call expressions
//
declare module "ast-types/gen/namedTypes.js" {
namespace namedTypes {
interface CallExpression extends TSHasOptionalTypeParameterInstantiation { }
}
}

export default function transformer(file: cs.FileInfo, api: cs.API) {
const j = api.jscodeshift

const root = j(file.source)

root.find(j.ExportNamedDeclaration, {
declaration: {
type: "VariableDeclaration",
declarations: [{
type: "VariableDeclarator",
id: {
type: "Identifier",
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: {
type: "TSTypeLiteral",
members: [{ type: "TSCallSignatureDeclaration" }]
}
}
}
}]
}
}).forEach((path) => {
const comments = path.node.comments ?? []
j(path).find(j.TSCallSignatureDeclaration).forEach((path) => {
path.node.comments = comments
})
})

root.find(j.ExportNamedDeclaration, {
declaration: {
type: "VariableDeclaration",
declarations: [{
type: "VariableDeclarator",
init: {
type: "CallExpression",
callee: {
type: "Identifier",
name: "dual"
}
}
}]
}
}).forEach((path) => {
const comments = path.node.comments ?? []
j(path).find(j.CallExpression).forEach((path) => {
path.node.typeParameters?.params.forEach((param) => {
param.comments = comments
})
})
})

return root.toSource()
}
22 changes: 22 additions & 0 deletions scripts/codeshift.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-check
import * as Glob from "glob"
import jscodeshift from "jscodeshift/src/Runner.js"
import * as Fs from "node:fs"
import * as Path from "node:path"

const packageJsonPath = Path.resolve("package.json")
const packageJson = JSON.parse(Fs.readFileSync(packageJsonPath, "utf-8"))
const workspaces = Glob.globSync(packageJson["workspaces"])
const packages = workspaces.map((workspace) => workspace.replace("packages/", ""))
const pattern = `packages/{${packages.join(",")}}/src/**/*.ts`

const paths = Glob.globSync(pattern, {
ignore: ["**/internal/**"]
}).map((path) => Path.resolve(path))

const transformer = Path.resolve("scripts/codemods/jsdoc.ts")

jscodeshift.run(Path.resolve(transformer), paths, {
babel: true,
parser: "ts"
}).then((result) => console.log(result))

0 comments on commit 99b72e3

Please sign in to comment.