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+ */
116module . 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 ) ) ;
0 commit comments