Skip to content

[v24.x] lib: modify the prototype based on cond on deprecate #58907

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

Draft
wants to merge 1 commit into
base: v24.x
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ class AsyncResource {
enumerable: true,
get: deprecate(function() {
return self;
}, 'The asyncResource property on bound functions is deprecated', 'DEP0172'),
}, 'The asyncResource property on bound functions is deprecated', 'DEP0172', false),
set: deprecate(function(val) {
self = val;
}, 'The asyncResource property on bound functions is deprecated', 'DEP0172'),
}, 'The asyncResource property on bound functions is deprecated', 'DEP0172', false),
},
});
return bound;
Expand Down
27 changes: 15 additions & 12 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function pendingDeprecate(fn, msg, code) {
// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
function deprecate(fn, msg, code, useEmitSync) {
function deprecate(fn, msg, code, useEmitSync, modifyPrototype = true) {
// Lazy-load to avoid a circular dependency.
if (validateString === undefined)
({ validateString } = require('internal/validators'));
Expand All @@ -192,19 +192,22 @@ function deprecate(fn, msg, code, useEmitSync) {
return ReflectApply(fn, this, args);
}

// The wrapper will keep the same prototype as fn to maintain prototype chain
ObjectSetPrototypeOf(deprecated, fn);
if (fn.prototype) {
// Setting this (rather than using Object.setPrototype, as above) ensures
// that calling the unwrapped constructor gives an instanceof the wrapped
// constructor.
deprecated.prototype = fn.prototype;
if (modifyPrototype) {
// The wrapper will keep the same prototype as fn to maintain prototype chain
ObjectSetPrototypeOf(deprecated, fn);
if (fn.prototype) {
// Setting this (rather than using Object.setPrototype, as above) ensures
// that calling the unwrapped constructor gives an instanceof the wrapped
// constructor.
deprecated.prototype = fn.prototype;
}

ObjectDefineProperty(deprecated, 'length', {
__proto__: null,
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
});
}

ObjectDefineProperty(deprecated, 'length', {
__proto__: null,
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
});

return deprecated;
}
Expand Down