Skip to content

Commit d9a04a9

Browse files
lib: add option to silence warnings
Introduce an option `--suppress-warnings` to silence experimental and deprecation warnings for specified features Fixes: #30810 Co-Authored-By: Cody Deckard <cjdeckard@gmail.com>
1 parent e22b751 commit d9a04a9

20 files changed

+103
-22
lines changed

doc/api/cli.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,13 @@ Enables report to be generated on uncaught exceptions. Useful when inspecting
848848
the JavaScript stack in conjunction with native stack and other runtime
849849
environment data.
850850

851+
### `--suppress-warnings`
852+
<!-- YAML
853+
added: REPLACEME
854+
-->
855+
856+
Silence deprecation and experimental warnings for specified codes.
857+
851858
### `--throw-deprecation`
852859
<!-- YAML
853860
added: v0.11.14
@@ -1358,6 +1365,7 @@ Node.js options that are allowed are:
13581365
* `--report-signal`
13591366
* `--report-uncaught-exception`
13601367
* `--require`, `-r`
1368+
* `--suppress-warnings`
13611369
* `--throw-deprecation`
13621370
* `--title`
13631371
* `--tls-cipher-list`

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ Enables
359359
to be generated on un-caught exceptions. Useful when inspecting JavaScript
360360
stack in conjunction with native stack and other runtime environment data.
361361
.
362+
.It Fl -suppress-warnings
363+
Silence warnings for specified codes.
364+
.
362365
.It Fl -throw-deprecation
363366
Throw errors for deprecations.
364367
.

lib/internal/modules/esm/translators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ translators.set('builtin', async function builtinStrategy(url) {
290290

291291
// Strategy for loading a JSON file
292292
translators.set('json', async function jsonStrategy(url) {
293-
emitExperimentalWarning('Importing JSON modules');
293+
emitExperimentalWarning('Importing JSON modules', 'EXP0003');
294294
debug(`Translating JSONModule ${url}`);
295295
debug(`Loading JSONModule ${url}`);
296296
const pathname = StringPrototypeStartsWith(url, 'file:') ?
@@ -351,7 +351,7 @@ translators.set('json', async function jsonStrategy(url) {
351351

352352
// Strategy for loading a wasm module
353353
translators.set('wasm', async function(url) {
354-
emitExperimentalWarning('Importing Web Assembly modules');
354+
emitExperimentalWarning('Importing Web Assembly modules', 'EXP0007');
355355
let { source } = await this._getSource(
356356
url, { format: 'wasm' }, defaultGetSource);
357357
assertBufferSource(source, false, 'getSource');

lib/internal/process/esm_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function initializeLoader() {
5151
// If --experimental-loader is specified, create a loader with user hooks.
5252
// Otherwise create the default loader.
5353
const { emitExperimentalWarning } = require('internal/util');
54-
emitExperimentalWarning('--experimental-loader');
54+
emitExperimentalWarning('--experimental-loader', 'EXP0002');
5555
return (async () => {
5656
const hooks =
5757
await ESMLoader.import(userLoader, pathToFileURL(cwd).href);

lib/internal/process/warning.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ function emitWarning(warning, type, code, ctor) {
137137
if (warning.name === 'DeprecationWarning') {
138138
if (process.noDeprecation)
139139
return;
140+
const options = require('internal/options');
141+
const opts = options.getOptionValue('--suppress-warnings');
142+
var list = opts.split(',');
143+
let found = false;
144+
list.forEach((e) => {
145+
if (e === code) found = true;
146+
});
147+
if (found) return;
140148
if (process.throwDeprecation) {
141149
// Delay throwing the error to guarantee that all former warnings were
142150
// properly logged.

lib/internal/util.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,20 @@ function slowCases(enc) {
175175
}
176176
}
177177

178-
function emitExperimentalWarning(feature) {
178+
function emitExperimentalWarning(feature, code) {
179179
if (experimentalWarnings.has(feature)) return;
180-
const msg = `${feature} is an experimental feature. This feature could ` +
181-
'change at any time';
182180
experimentalWarnings.add(feature);
181+
const { getOptionValue } = require('internal/options');
182+
const opts = getOptionValue('--suppress-warnings');
183+
const list = opts.split(',');
184+
let found = false;
185+
list.forEach((e) => {
186+
if (e === code) found = true;
187+
});
188+
if (found) return;
189+
190+
const msg = `[${code}] ${feature} is an experimental feature. ` +
191+
'This feature could change at any time';
183192
process.emitWarning(msg, 'ExperimentalWarning');
184193
}
185194

lib/internal/vm/module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const kLink = Symbol('kLink');
7474

7575
class Module {
7676
constructor(options) {
77-
emitExperimentalWarning('VM Modules');
77+
emitExperimentalWarning('VM Modules', 'EXP0005');
7878

7979
if (new.target === Module) {
8080
// eslint-disable-next-line no-restricted-syntax

lib/vm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ const measureMemoryExecutions = {
387387
};
388388

389389
function measureMemory(options = {}) {
390-
emitExperimentalWarning('vm.measureMemory');
390+
emitExperimentalWarning('vm.measureMemory', 'EXP0004');
391391
validateObject(options, 'options');
392392
const { mode = 'summary', execution = 'default' } = options;
393393
validateOneOf(mode, 'options.mode', ['summary', 'detailed']);

lib/wasi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const kSetMemory = Symbol('kSetMemory');
2626
const kStarted = Symbol('kStarted');
2727
const kInstance = Symbol('kInstance');
2828

29-
emitExperimentalWarning('WASI');
29+
emitExperimentalWarning('WASI', 'EXP0006');
3030

3131

3232
function setupInstance(self, instance) {

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
401401
AddOption("--prof-process",
402402
"process V8 profiler output generated using --prof",
403403
&EnvironmentOptions::prof_process);
404+
AddOption("--suppress-warnings",
405+
"silence warnings for specified codes",
406+
&EnvironmentOptions::suppress_warnings,
407+
kAllowedInEnvironment);
404408
// Options after --prof-process are passed through to the prof processor.
405409
AddAlias("--prof-process", { "--prof-process", "--" });
406410
#if HAVE_INSPECTOR

0 commit comments

Comments
 (0)