| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -225,11 +225,15 @@ added: v14.9.0 | |||
| 225 | 225 | Alias for `util.debuglog`. Usage allows for readability of that doesn't imply | |
| 226 | 226 | logging when only using `util.debuglog().enabled`. | |
| 227 | 227 | ||
| 228 | - ## `util.deprecate(fn, msg[, code])` | ||
| 228 | + ## `util.deprecate(fn, msg[, code[, options]])` | ||
| 229 | 229 | ||
| 230 | 230 | <!-- YAML | |
| 231 | 231 | added: v0.8.0 | |
| 232 | 232 | 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. | ||
| 233 | 237 | - version: v10.0.0 | |
| 234 | 238 | pr-url: https://github.com/nodejs/node/pull/16393 | |
| 235 | 239 | description: Deprecation warnings are only emitted once for each code. | |
@@ -240,6 +244,10 @@ changes: | |||
| 240 | 244 | invoked. | |
| 241 | 245 | * `code` {string} A deprecation code. See the [list of deprecated APIs][] for a | |
| 242 | 246 | 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`. | ||
| 243 | 251 | * Returns: {Function} The deprecated function wrapped to emit a warning. | |
| 244 | 252 | ||
| 245 | 253 | The `util.deprecate()` method wraps `fn` (which may be a function or class) in | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,7 +84,7 @@ const { getOptionValue } = require('internal/options'); | |||
| 84 | 84 | const binding = internalBinding('util'); | |
| 85 | 85 | ||
| 86 | 86 | const { | |
| 87 | - deprecate, | ||
| 87 | + deprecate: internalDeprecate, | ||
| 88 | 88 | getLazy, | |
| 89 | 89 | getSystemErrorMap, | |
| 90 | 90 | getSystemErrorName: internalErrorName, | |
@@ -459,13 +459,18 @@ function getCallSites(frameCount = 10, options) { | |||
| 459 | 459 | return binding.getCallSites(frameCount); | |
| 460 | 460 | }; | |
| 461 | 461 | ||
| 462 | + // Public util.deprecate API | ||
| 463 | + function deprecate(fn, msg, code, { modifyPrototype } = {}) { | ||
| 464 | + return internalDeprecate(fn, msg, code, undefined, modifyPrototype); | ||
| 465 | + } | ||
| 466 | + | ||
| 462 | 467 | // Keep the `exports =` so that various functions can still be monkeypatched | |
| 463 | 468 | module.exports = { | |
| 464 | 469 | _errnoException, | |
| 465 | 470 | _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'), | ||
| 469 | 474 | callbackify, | |
| 470 | 475 | debug: debuglog, | |
| 471 | 476 | debuglog, | |
@@ -479,9 +484,9 @@ module.exports = { | |||
| 479 | 484 | getSystemErrorMessage, | |
| 480 | 485 | inherits, | |
| 481 | 486 | 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'), | ||
| 485 | 490 | isDeepStrictEqual(a, b, skipPrototype) { | |
| 486 | 491 | if (internalDeepEqual === undefined) { | |
| 487 | 492 | internalDeepEqual = require('internal/util/comparisons').isDeepStrictEqual; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,6 +61,28 @@ for (const fn of [ | |||
| 61 | 61 | fn2(); | |
| 62 | 62 | } | |
| 63 | 63 | ||
| 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 | + | ||
| 64 | 86 | process.on('warning', (warning) => { | |
| 65 | 87 | assert.strictEqual(warning.name, 'DeprecationWarning'); | |
| 66 | 88 | assert.ok(expectedWarnings.has(warning.message)); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments