Skip to content

Commit 1d6c17e

Browse files
addaleaxevanlucas
authored andcommitted
util: adhere to noDeprecation set at runtime
Until now, the docs stated that `process.noDeprecation` could be set at runtime, but before any modules were loaded. That was not true, because `lib/internal/util.js` was loaded during the process startup process, so setting the flag at runtime was pointless. Minimal test case: process.noDeprecation = true; process.EventEmitter; This patch moves checking `process.noDeprecation` to the place where it was actually used. PR-URL: #6683 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 3af9382 commit 1d6c17e

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

doc/api/util.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ exports.puts = util.deprecate(() => {
6767

6868
It returns a modified function which warns once by default.
6969

70-
If `--no-deprecation` is set then this function is a NO-OP. Configurable
71-
at run-time through the `process.noDeprecation` boolean (only effective
72-
when set before a module is loaded.)
70+
This function does nothing if either the `--no-deprecation` command
71+
line flag is used, or the `process.noDeprecation` property is set to
72+
`true` *prior* to the first deprecation warning.
7373

7474
If `--trace-deprecation` is set, a warning and a stack trace are logged
7575
to the console the first time the deprecated API is used. Configurable

lib/internal/util.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const binding = process.binding('util');
44
const prefix = `(${process.release.name}:${process.pid}) `;
5-
const noDeprecation = process.noDeprecation;
65

76
exports.getHiddenValue = binding.getHiddenValue;
87
exports.setHiddenValue = binding.setHiddenValue;
@@ -16,7 +15,7 @@ exports.deprecate = function(fn, msg) {
1615
// All the internal deprecations have to use this function only, as this will
1716
// prepend the prefix to the actual message.
1817
exports.printDeprecationMessage = function(msg, warned, ctor) {
19-
if (warned || noDeprecation)
18+
if (warned || process.noDeprecation)
2019
return true;
2120
process.emitWarning(msg, 'DeprecationWarning',
2221
ctor || exports.printDeprecationMessage);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
// Flags: --expose_internals --no-warnings
3+
4+
// The --no-warnings flag only supresses writing the warning to stderr, not the
5+
// emission of the corresponding event. This test file can be run without it.
6+
7+
process.noDeprecation = true;
8+
9+
const common = require('../common');
10+
const assert = require('assert');
11+
12+
function listener() {
13+
common.fail('received unexpected warning');
14+
}
15+
16+
process.addListener('warning', listener);
17+
18+
const internalUtil = require('internal/util');
19+
internalUtil.printDeprecationMessage('Something is deprecated.');
20+
21+
// The warning would be emitted in the next tick, so continue after that.
22+
process.nextTick(common.mustCall(() => {
23+
// Check that deprecations can be re-enabled.
24+
process.noDeprecation = false;
25+
process.removeListener('warning', listener);
26+
27+
process.addListener('warning', common.mustCall((warning) => {
28+
assert.strictEqual(warning.name, 'DeprecationWarning');
29+
assert.strictEqual(warning.message, 'Something else is deprecated.');
30+
}));
31+
32+
internalUtil.printDeprecationMessage('Something else is deprecated.');
33+
}));

0 commit comments

Comments
 (0)