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+ */
217function 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+ */
1131function 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+ */
1844module . 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 ) ) ;
0 commit comments