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

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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
return ReflectApply(fn, this, args);
144144
}
145+
146+
ObjectDefineProperty(deprecated, 'length', {
147+
__proto__: null,
148+
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
149+
});
150+
145151
return deprecated;
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
return deprecated;
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
const assert = require('assert');
89
const util = require('util');
10+
const internalUtil = require('internal/util');
911

1012
const expectedWarnings = new Map();
1113

14+
// Deprecated function length is preserved
15+
for (const fn of [
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
const msg = 'fhqwhgads';

0 commit comments

Comments
 (0)