Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
util: preserve length of deprecated functions
  • Loading branch information
LiviaMedeiros committed Apr 9, 2025
commit 53cdf0e23346fa9beabac794f43a727f8422d8af
3 changes: 3 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function pendingDeprecate(fn, msg, code) {
emitDeprecationWarning();
return ReflectApply(fn, this, args);
}
ObjectDefineProperty(deprecated, 'length', ObjectGetOwnPropertyDescriptor(fn, 'length'));
return deprecated;
}

Expand Down Expand Up @@ -180,6 +181,8 @@ function deprecate(fn, msg, code, useEmitSync) {
deprecated.prototype = fn.prototype;
}

ObjectDefineProperty(deprecated, 'length', ObjectGetOwnPropertyDescriptor(fn, 'length'));

return deprecated;
}

Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-util-deprecate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --expose-internals
'use strict';

require('../common');
Expand All @@ -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';
Expand Down