Skip to content

Commit 6e2504f

Browse files
committed
Add JSDoc comments for typing info
Helps to check contributions follow the babel API
1 parent 810ef21 commit 6e2504f

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
/** @import { PluginObj, NodePath } from '@babel/core' */
2+
/** @import { Property, Method, MemberExpression } from '@babel/types' */
3+
/** @import { Visitor } from '@babel/traverse' */
4+
5+
/**
6+
* @typedef {Object} PluginOptions
7+
* @property {Record<string, string>} [rename] - A map of property names to rename
8+
*/
9+
10+
/**
11+
* Babel plugin that renames properties, methods, and member expressions.
12+
* @param {{ types: typeof import('@babel/types') }} babel - The Babel object
13+
* @param {PluginOptions} [options] - Plugin options
14+
* @returns {PluginObj} The Babel plugin object
15+
*/
116
module.exports = function ({ types: t }, options = {}) {
217
const rename = options.rename || {};
318

19+
/** @type {Map<string, string>} */
420
const nameMap = new Map();
521
Object.keys(rename).forEach((key) => {
622
const value = rename[key];
@@ -12,10 +28,16 @@ module.exports = function ({ types: t }, options = {}) {
1228
nameMap.set(key, value);
1329
});
1430

31+
/**
32+
* Visitor for Property and Method nodes that renames keys.
33+
* @type {Visitor}
34+
*/
1535
const replacePropertyOrMethod = {
36+
/** @param {NodePath<Property | Method>} path */
1637
exit(path) {
1738
const node = path.node;
1839

40+
/** @type {string | undefined} */
1941
let name;
2042
if (t.isIdentifier(node.key) && !node.computed) {
2143
name = node.key.name;
@@ -47,9 +69,11 @@ module.exports = function ({ types: t }, options = {}) {
4769
Property: replacePropertyOrMethod,
4870
Method: replacePropertyOrMethod,
4971
MemberExpression: {
72+
/** @param {NodePath<MemberExpression>} path */
5073
exit(path) {
5174
const node = path.node;
5275

76+
/** @type {string | undefined} */
5377
let name;
5478
if (t.isIdentifier(node.property) && !node.computed) {
5579
name = node.property.name;
@@ -64,6 +88,7 @@ module.exports = function ({ types: t }, options = {}) {
6488
return;
6589
}
6690

91+
/** @type {MemberExpression} */
6792
let newNode;
6893
if (t.isValidIdentifier(newName)) {
6994
newNode = t.memberExpression(node.object, t.identifier(newName));

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"scripts": {
1313
"test": "vitest --run",
1414
"test:watch": "vitest",
15-
"lint": "eslint --max-warnings 0 --ignore-path .gitignore ."
15+
"lint": "yarn run tsc && yarn run eslint",
16+
"tsc": "tsc --noEmit --allowJs --checkJs --esModuleInterop --skipLibCheck index.js test/index.test.js",
17+
"eslint": "eslint --max-warnings 0 --ignore-path .gitignore ."
1618
},
1719
"peerDependencies": {
1820
"@babel/core": "^7.0.0-0"
@@ -23,6 +25,7 @@
2325
"eslint-config-prettier": "^8.5.0",
2426
"eslint-plugin-prettier": "^4.2.1",
2527
"prettier": "^2.7.1",
28+
"typescript": "^5.9.3",
2629
"vitest": "^0.24.3"
2730
}
2831
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,11 @@ type-fest@^0.20.2:
13921392
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4"
13931393
integrity sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==
13941394

1395+
typescript@^5.9.3:
1396+
version "5.9.3"
1397+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.9.3.tgz#5b4f59e15310ab17a216f5d6cf53ee476ede670f"
1398+
integrity sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==
1399+
13951400
update-browserslist-db@^1.0.9:
13961401
version "1.0.10"
13971402
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.10.tgz#0f54b876545726f17d00cd9a2561e6dade943ff3"

0 commit comments

Comments
 (0)