Skip to content

Commit 909c45a

Browse files
authored
Add JSDoc comments for typing info (#13)
Helps to check contributions follow the babel API
1 parent 0ab45f9 commit 909c45a

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
/** Check if node is a template literal with no expressions (e.g., `foo`) */
1+
/** @import { PluginObj, NodePath } from '@babel/core' */
2+
/** @import { Node, Property, Method, MemberExpression, TemplateLiteral } from '@babel/types' */
3+
/** @import { Visitor } from '@babel/traverse' */
4+
/** @typedef {typeof import('@babel/types')} BabelTypes */
5+
6+
/**
7+
* @typedef {Object} PluginOptions
8+
* @property {Record<string, string>} [rename] - A map of property names to rename
9+
*/
10+
11+
/**
12+
* Check if node is a template literal with no expressions (e.g., `foo`)
13+
* @param {BabelTypes} t - The Babel types object
14+
* @param {Node} node - The AST node to check
15+
* @returns {boolean} True if the node is a constant template literal
16+
*/
217
function isConstantTemplateLiteral(t, node) {
318
return (
419
t.isTemplateLiteral(node) &&
@@ -7,17 +22,29 @@ function isConstantTemplateLiteral(t, node) {
722
);
823
}
924

10-
/** Create a template literal node with no expressions (e.g., `foo`) */
25+
/**
26+
* Create a template literal node with no expressions (e.g., `foo`)
27+
* @param {BabelTypes} t - The Babel types object
28+
* @param {string} value - The string value for the template literal
29+
* @returns {TemplateLiteral} The template literal node
30+
*/
1131
function constantTemplateLiteral(t, value) {
1232
return t.templateLiteral(
1333
[t.templateElement({ raw: value, cooked: value }, true)],
1434
[]
1535
);
1636
}
1737

38+
/**
39+
* Babel plugin that renames properties, methods, and member expressions.
40+
* @param {{ types: BabelTypes }} babel - The Babel object
41+
* @param {PluginOptions} [options] - Plugin options
42+
* @returns {PluginObj} The Babel plugin object
43+
*/
1844
module.exports = function ({ types: t }, options = {}) {
1945
const rename = options.rename || {};
2046

47+
/** @type {Map<string, string>} */
2148
const nameMap = new Map();
2249
Object.keys(rename).forEach((key) => {
2350
const value = rename[key];
@@ -29,10 +56,16 @@ module.exports = function ({ types: t }, options = {}) {
2956
nameMap.set(key, value);
3057
});
3158

59+
/**
60+
* Visitor for Property and Method nodes that renames keys.
61+
* @type {Visitor}
62+
*/
3263
const replacePropertyOrMethod = {
64+
/** @param {NodePath<Property | Method>} path */
3365
exit(path) {
3466
const node = path.node;
3567

68+
/** @type {string | undefined} */
3669
let name;
3770
if (t.isIdentifier(node.key) && !node.computed) {
3871
name = node.key.name;
@@ -64,9 +97,11 @@ module.exports = function ({ types: t }, options = {}) {
6497
Property: replacePropertyOrMethod,
6598
Method: replacePropertyOrMethod,
6699
MemberExpression: {
100+
/** @param {NodePath<MemberExpression>} path */
67101
exit(path) {
68102
const node = path.node;
69103

104+
/** @type {string | undefined} */
70105
let name;
71106
if (t.isIdentifier(node.property) && !node.computed) {
72107
name = node.property.name;
@@ -81,6 +116,7 @@ module.exports = function ({ types: t }, options = {}) {
81116
return;
82117
}
83118

119+
/** @type {MemberExpression} */
84120
let newNode;
85121
if (t.isValidIdentifier(newName)) {
86122
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)