util: preserve length of deprecated functions

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Livia Medeiros 2025-04-12 01:26:25 +09:00 committed by GitHub
parent 795dd8eb79
commit 86f86a25e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -142,6 +142,12 @@ function pendingDeprecate(fn, msg, code) {
emitDeprecationWarning();
return ReflectApply(fn, this, args);
}
ObjectDefineProperty(deprecated, 'length', {
__proto__: null,
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
});
return deprecated;
}
@ -180,6 +186,11 @@ function deprecate(fn, msg, code, useEmitSync) {
deprecated.prototype = fn.prototype;
}
ObjectDefineProperty(deprecated, 'length', {
__proto__: null,
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
});
return deprecated;
}

View File

@ -1,3 +1,4 @@
// Flags: --expose-internals
'use strict';
require('../common');
@ -6,9 +7,27 @@ require('../common');
const assert = require('assert');
const util = require('util');
const internalUtil = require('internal/util');
const expectedWarnings = new Map();
// Deprecated function length is preserved
for (const fn of [
function() {},
function(a) {},
function(a, b, c) {},
function(...args) {},
function(a, b, c, ...args) {},
() => {},
(a) => {},
(a, b, c) => {},
(...args) => {},
(a, b, c, ...args) => {},
]) {
assert.strictEqual(util.deprecate(fn).length, fn.length);
assert.strictEqual(internalUtil.pendingDeprecate(fn).length, fn.length);
}
// Emits deprecation only once if same function is called.
{
const msg = 'fhqwhgads';