@@ -41,6 +41,7 @@ const {
41
41
StringPrototypeToUpperCase,
42
42
Symbol,
43
43
SymbolFor,
44
+ SymbolPrototypeGetDescription,
44
45
SymbolReplace,
45
46
SymbolSplit,
46
47
} = primordials ;
@@ -67,6 +68,7 @@ const {
67
68
} = internalBinding ( 'util' ) ;
68
69
const { isNativeError, isPromise } = internalBinding ( 'types' ) ;
69
70
const { getOptionValue } = require ( 'internal/options' ) ;
71
+ const assert = require ( 'internal/assert' ) ;
70
72
const { encodings } = internalBinding ( 'string_decoder' ) ;
71
73
72
74
const noCrypto = ! process . versions . openssl ;
@@ -897,10 +899,37 @@ const encodingsMap = { __proto__: null };
897
899
for ( let i = 0 ; i < encodings . length ; ++ i )
898
900
encodingsMap [ encodings [ i ] ] = i ;
899
901
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
+
900
928
module . exports = {
901
929
getLazy,
902
930
assertCrypto,
903
931
assertTypeScript,
932
+ assignFunctionName,
904
933
cachedResult,
905
934
convertToValidSignal,
906
935
createClassWrapper,
0 commit comments