Skip to content

Commit 8ca56a8

Browse files
LiviaMedeirosRafaelGSS
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 660d238 commit 8ca56a8

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
@@ -142,6 +142,12 @@ function pendingDeprecate(fn, msg, code) {
142142
emitDeprecationWarning();
143143
returnReflectApply(fn,this,args);
144144
}
145+
146+
ObjectDefineProperty(deprecated,'length',{
147+
__proto__: null,
148+
...ObjectGetOwnPropertyDescriptor(fn,'length'),
149+
});
150+
145151
returndeprecated;
146152
}
147153

@@ -180,6 +186,11 @@ function deprecate(fn, msg, code, useEmitSync) {
180186
deprecated.prototype=fn.prototype;
181187
}
182188

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

‎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)