@@ -41,6 +41,7 @@ const {
4141 StringPrototypeToUpperCase,
4242 Symbol,
4343 SymbolFor,
44+ SymbolPrototypeGetDescription,
4445 SymbolReplace,
4546 SymbolSplit,
4647} = primordials ;
@@ -67,6 +68,7 @@ const {
6768} = internalBinding ( 'util' ) ;
6869const { isNativeError, isPromise } = internalBinding ( 'types' ) ;
6970const { getOptionValue } = require ( 'internal/options' ) ;
71+ const assert = require ( 'internal/assert' ) ;
7072const { encodings } = internalBinding ( 'string_decoder' ) ;
7173
7274const noCrypto = ! process . versions . openssl ;
@@ -897,10 +899,37 @@ const encodingsMap = { __proto__: null };
897899for ( let i = 0 ; i < encodings . length ; ++ i )
898900 encodingsMap [ encodings [ i ] ] = i ;
899901
902+ /**
903+ * Reassigns the .name property of a function.
904+ * Should be used when function can't be initially defined with desired name
905+ * or when desired name should include `#`, `[`, `]`, etc.
906+ * @param {string | symbol } name
907+ * @param {Function } fn
908+ * @param {object } [descriptor]
909+ * @returns {Function } the same function, renamed
910+ */
911+ function assignFunctionName ( name , fn , descriptor = kEmptyObject ) {
912+ if ( typeof name !== 'string' ) {
913+ const symbolDescription = SymbolPrototypeGetDescription ( name ) ;
914+ assert ( symbolDescription !== undefined , 'Attempted to name function after descriptionless Symbol' ) ;
915+ name = `[${ symbolDescription } ]` ;
916+ }
917+ return ObjectDefineProperty ( fn , 'name' , {
918+ __proto__ : null ,
919+ writable : false ,
920+ enumerable : false ,
921+ configurable : true ,
922+ ...ObjectGetOwnPropertyDescriptor ( fn , 'name' ) ,
923+ ...descriptor ,
924+ value : name ,
925+ } ) ;
926+ }
927+
900928module . exports = {
901929 getLazy,
902930 assertCrypto,
903931 assertTypeScript,
932+ assignFunctionName,
904933 cachedResult,
905934 convertToValidSignal,
906935 createClassWrapper,
0 commit comments