-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codemod to propagate JSDoc comments to dual signatures (#3697)
- Loading branch information
Showing
6 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -16,8 +16,10 @@ jobs: | |
- uses: actions/checkout@v4 | ||
- name: Install dependencies | ||
uses: ./.github/actions/setup | ||
- name: Run codemods | ||
run: pnpm codemod | ||
- name: Build package | ||
run: pnpm build | ||
- name: Create snapshot | ||
id: snapshot | ||
run: pnpx [email protected].17 publish --pnpm --comment=off ./packages/* | ||
run: pnpx [email protected].28 publish --pnpm --comment=off ./packages/* |
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
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
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" | ||
}) |
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,72 @@ | ||
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) => { | ||
// Don't override comments if they already exist | ||
if (!Array.isArray(path.node.comments)) { | ||
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) => { | ||
// Don't override comments if they already exist | ||
if (!Array.isArray(param.comments)) { | ||
param.comments = comments | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
return root.toSource() | ||
} |