Skip to content

Commit cfa11ba

Browse files
authored
lib: add options to util.deprecate
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #59982 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f4145f0 commit cfa11ba

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

doc/api/util.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,15 @@ added: v14.9.0
225225
Alias for `util.debuglog`. Usage allows for readability of that doesn't imply
226226
logging when only using `util.debuglog().enabled`.
227227

228-
## `util.deprecate(fn, msg[, code])`
228+
## `util.deprecate(fn, msg[, code[, options]])`
229229

230230
<!-- YAML
231231
added: v0.8.0
232232
changes:
233+
- version: REPLACEME
234+
pr-url: https://github.com/nodejs/node/pull/59982
235+
description: Add options object with modifyPrototype to conditionally
236+
modify the prototype of the deprecated object.
233237
- version: v10.0.0
234238
pr-url: https://github.com/nodejs/node/pull/16393
235239
description: Deprecation warnings are only emitted once for each code.
@@ -240,6 +244,10 @@ changes:
240244
invoked.
241245
* `code` {string} A deprecation code. See the [list of deprecated APIs][] for a
242246
list of codes.
247+
* `options` {Object}
248+
* `modifyPrototype` {boolean} When false do not change the prototype of object
249+
while emitting the deprecation warning.
250+
**Default:** `true`.
243251
* Returns: {Function} The deprecated function wrapped to emit a warning.
244252

245253
The `util.deprecate()` method wraps `fn` (which may be a function or class) in

lib/util.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const { getOptionValue } = require('internal/options');
8484
const binding = internalBinding('util');
8585

8686
const {
87-
deprecate,
87+
deprecate: internalDeprecate,
8888
getLazy,
8989
getSystemErrorMap,
9090
getSystemErrorName: internalErrorName,
@@ -459,13 +459,18 @@ function getCallSites(frameCount = 10, options) {
459459
return binding.getCallSites(frameCount);
460460
};
461461

462+
// Public util.deprecate API
463+
function deprecate(fn, msg, code, { modifyPrototype } = {}) {
464+
return internalDeprecate(fn, msg, code, undefined, modifyPrototype);
465+
}
466+
462467
// Keep the `exports =` so that various functions can still be monkeypatched
463468
module.exports = {
464469
_errnoException,
465470
_exceptionWithHostPort,
466-
_extend: deprecate(_extend,
467-
'The `util._extend` API is deprecated. Please use Object.assign() instead.',
468-
'DEP0060'),
471+
_extend: internalDeprecate(_extend,
472+
'The `util._extend` API is deprecated. Please use Object.assign() instead.',
473+
'DEP0060'),
469474
callbackify,
470475
debug: debuglog,
471476
debuglog,
@@ -479,9 +484,9 @@ module.exports = {
479484
getSystemErrorMessage,
480485
inherits,
481486
inspect,
482-
isArray: deprecate(ArrayIsArray,
483-
'The `util.isArray` API is deprecated. Please use `Array.isArray()` instead.',
484-
'DEP0044'),
487+
isArray: internalDeprecate(ArrayIsArray,
488+
'The `util.isArray` API is deprecated. Please use `Array.isArray()` instead.',
489+
'DEP0044'),
485490
isDeepStrictEqual(a, b, skipPrototype) {
486491
if (internalDeepEqual === undefined) {
487492
internalDeepEqual = require('internal/util/comparisons').isDeepStrictEqual;

test/parallel/test-util-deprecate.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ for (const fn of [
6161
fn2();
6262
}
6363

64+
65+
// Test modifyPrototype option
66+
{
67+
const msg = 'prototype-test';
68+
const code = 'proto-code';
69+
70+
function OriginalFn() {}
71+
OriginalFn.prototype.testMethod = function() { return 'test'; };
72+
73+
const deprecatedWithoutProto = util.deprecate(OriginalFn, msg, code, { modifyPrototype: false });
74+
75+
assert.notStrictEqual(deprecatedWithoutProto.prototype, OriginalFn.prototype);
76+
assert.notStrictEqual(Object.getPrototypeOf(deprecatedWithoutProto), OriginalFn);
77+
assert.strictEqual(deprecatedWithoutProto.prototype.testMethod, undefined);
78+
79+
const deprecatedWithProto = util.deprecate(OriginalFn, msg, code);
80+
81+
assert.strictEqual(deprecatedWithProto.prototype, OriginalFn.prototype);
82+
assert.strictEqual(Object.getPrototypeOf(deprecatedWithProto), OriginalFn);
83+
assert.strictEqual(typeof deprecatedWithProto.prototype.testMethod, 'function');
84+
}
85+
6486
process.on('warning', (warning) => {
6587
assert.strictEqual(warning.name, 'DeprecationWarning');
6688
assert.ok(expectedWarnings.has(warning.message));

0 commit comments

Comments
 (0)