Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a37c01e

Browse filesBrowse files
RafaelGSSaduh95
authored andcommitted
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 9c99ab6 commit a37c01e
Copy full SHA for a37c01e

File tree

Expand file treeCollapse file tree

3 files changed

+43
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+43
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/util.md‎

Copy file name to clipboardExpand all lines: doc/api/util.md
+9-1Lines changed: 9 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+12-7Lines 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;
Collapse file

‎test/parallel/test-util-deprecate.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-deprecate.js
+22Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.