-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: emit deprecation code only once
If another function has already emitted the deprecation warning with the same code as the warning that is about to be emitted, do not emit the warning. This is a breaking change. Previously, different functions could emit the same deprecation warning multiple times. This was a known bug rather than a feature, but this change is being treated as a breaking change out of caution. Identical deprecation warnings should not be emitted. PR-URL: #16393 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
Showing
3 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
// Tests basic functionality of util.deprecate(). | ||
|
||
const assert = require('assert'); | ||
const util = require('util'); | ||
|
||
const expectedWarnings = new Map(); | ||
|
||
// Emits deprecation only once if same function is called. | ||
{ | ||
const msg = 'fhqwhgads'; | ||
const fn = util.deprecate(() => {}, msg); | ||
expectedWarnings.set(msg, { code: undefined, count: 1 }); | ||
fn(); | ||
fn(); | ||
} | ||
|
||
// Emits deprecation twice for different functions. | ||
{ | ||
const msg = 'sterrance'; | ||
const fn1 = util.deprecate(() => {}, msg); | ||
const fn2 = util.deprecate(() => {}, msg); | ||
expectedWarnings.set(msg, { code: undefined, count: 2 }); | ||
fn1(); | ||
fn2(); | ||
} | ||
|
||
// Emits deprecation only once if optional code is the same, even for different | ||
// functions. | ||
{ | ||
const msg = 'cannonmouth'; | ||
const code = 'deprecatesque'; | ||
const fn1 = util.deprecate(() => {}, msg, code); | ||
const fn2 = util.deprecate(() => {}, msg, code); | ||
expectedWarnings.set(msg, { code, count: 1 }); | ||
fn1(); | ||
fn2(); | ||
fn1(); | ||
fn2(); | ||
} | ||
|
||
process.on('warning', (warning) => { | ||
assert.strictEqual(warning.name, 'DeprecationWarning'); | ||
assert.ok(expectedWarnings.has(warning.message)); | ||
const expected = expectedWarnings.get(warning.message); | ||
assert.strictEqual(warning.code, expected.code); | ||
expected.count = expected.count - 1; | ||
if (expected.count === 0) | ||
expectedWarnings.delete(warning.message); | ||
}); | ||
|
||
process.on('exit', () => { | ||
assert.deepStrictEqual(expectedWarnings, new Map()); | ||
}); |