Skip to content

Commit 30060e1

Browse files
LiviaMedeirosaduh95
authored andcommitted
util: preserve length of deprecated functions
PR-URL: #57806 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 3ae04c9 commit 30060e1

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎lib/internal/util.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ function pendingDeprecate(fn, msg, code) {
141141
emitDeprecationWarning();
142142
returnReflectApply(fn,this,args);
143143
}
144+
145+
ObjectDefineProperty(deprecated,'length',{
146+
__proto__: null,
147+
...ObjectGetOwnPropertyDescriptor(fn,'length'),
148+
});
149+
144150
returndeprecated;
145151
}
146152

@@ -179,6 +185,11 @@ function deprecate(fn, msg, code, useEmitSync) {
179185
deprecated.prototype=fn.prototype;
180186
}
181187

188+
ObjectDefineProperty(deprecated,'length',{
189+
__proto__: null,
190+
...ObjectGetOwnPropertyDescriptor(fn,'length'),
191+
});
192+
182193
returndeprecated;
183194
}
184195

‎test/parallel/test-util-deprecate.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --expose-internals
12
'use strict';
23

34
require('../common');
@@ -6,9 +7,27 @@ require('../common');
67

78
constassert=require('assert');
89
constutil=require('util');
10+
constinternalUtil=require('internal/util');
911

1012
constexpectedWarnings=newMap();
1113

14+
// Deprecated function length is preserved
15+
for(constfnof[
16+
function(){},
17+
function(a){},
18+
function(a,b,c){},
19+
function(...args){},
20+
function(a,b,c, ...args){},
21+
()=>{},
22+
(a)=>{},
23+
(a,b,c)=>{},
24+
(...args)=>{},
25+
(a,b,c, ...args)=>{},
26+
]){
27+
assert.strictEqual(util.deprecate(fn).length,fn.length);
28+
assert.strictEqual(internalUtil.pendingDeprecate(fn).length,fn.length);
29+
}
30+
1231
// Emits deprecation only once if same function is called.
1332
{
1433
constmsg='fhqwhgads';

0 commit comments

Comments
 (0)