Skip to content

util: preserve length of deprecated functions #57806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

ObjectDefineProperty(deprecated, 'length', {
__proto__: null,
...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
Loading