Skip to content

Commit f76893d

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 8eb9930 commit f76893d

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
@@ -141,6 +141,12 @@ function pendingDeprecate(fn, msg, code) {
141141
emitDeprecationWarning();
142142
return ReflectApply(fn, this, args);
143143
}
144+
145+
ObjectDefineProperty(deprecated, 'length', {
146+
__proto__: null,
147+
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
148+
});
149+
144150
return deprecated;
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
return deprecated;
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
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)