Skip to content

Commit 8c18e91

Browse files
committed
process: remove undocumented now argument from emitWarning()
process.emitWarning() "now" option is undocumented and a Boolean trap. Remove it before people start adopting it. We only need it in one place internally. Replace it with an internal-only emitWarningSync() function. PR-URL: #31643 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Myles Borins <myles.borins@gmail.com>
1 parent f368210 commit 8c18e91

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const assert = require('internal/assert');
5757
const fs = require('fs');
5858
const internalFS = require('internal/fs/utils');
5959
const path = require('path');
60+
const { emitWarningSync } = require('internal/process/warning');
6061
const {
6162
internalModuleReadJSON,
6263
internalModuleStat
@@ -122,13 +123,13 @@ function enrichCJSError(err) {
122123
*/
123124
if (err.message.startsWith('Unexpected token \'export\'') ||
124125
(/^\s*import(?=[ {'"*])\s*(?![ (])/).test(lineWithErr)) {
125-
process.emitWarning(
126+
// Emit the warning synchronously because we are in the middle of handling
127+
// a SyntaxError that will throw and likely terminate the process before an
128+
// asynchronous warning would be emitted.
129+
emitWarningSync(
126130
'To load an ES module, set "type": "module" in the package.json or use ' +
127-
'the .mjs extension.',
128-
undefined,
129-
undefined,
130-
undefined,
131-
true);
131+
'the .mjs extension.'
132+
);
132133
}
133134
}
134135

@@ -839,11 +840,8 @@ Module._resolveLookupPaths = function(request, parent) {
839840
function emitCircularRequireWarning(prop) {
840841
process.emitWarning(
841842
`Accessing non-existent property '${String(prop)}' of module exports ` +
842-
'inside circular dependency',
843-
'Warning',
844-
undefined, // code
845-
undefined, // ctor
846-
true); // emit now
843+
'inside circular dependency'
844+
);
847845
}
848846

849847
// A Proxy that can be used as the prototype of a module.exports object and

lib/internal/process/warning.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
Error,
66
} = primordials;
77

8+
const assert = require('internal/assert');
89
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
910

1011
// Lazily loaded
@@ -87,7 +88,7 @@ function onWarning(warning) {
8788
// process.emitWarning(error)
8889
// process.emitWarning(str[, type[, code]][, ctor])
8990
// process.emitWarning(str[, options])
90-
function emitWarning(warning, type, code, ctor, now) {
91+
function emitWarning(warning, type, code, ctor) {
9192
let detail;
9293
if (type !== null && typeof type === 'object' && !ArrayIsArray(type)) {
9394
ctor = type.ctor;
@@ -110,18 +111,7 @@ function emitWarning(warning, type, code, ctor, now) {
110111
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
111112
}
112113
if (typeof warning === 'string') {
113-
// Improve error creation performance by skipping the error frames.
114-
// They are added in the `captureStackTrace()` function below.
115-
const tmpStackLimit = Error.stackTraceLimit;
116-
Error.stackTraceLimit = 0;
117-
// eslint-disable-next-line no-restricted-syntax
118-
warning = new Error(warning);
119-
Error.stackTraceLimit = tmpStackLimit;
120-
warning.name = String(type || 'Warning');
121-
if (code !== undefined) warning.code = code;
122-
if (detail !== undefined) warning.detail = detail;
123-
// eslint-disable-next-line no-restricted-syntax
124-
Error.captureStackTrace(warning, ctor || process.emitWarning);
114+
warning = createWarningObject(warning, type, code, ctor, detail);
125115
} else if (!(warning instanceof Error)) {
126116
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
127117
}
@@ -131,11 +121,32 @@ function emitWarning(warning, type, code, ctor, now) {
131121
if (process.throwDeprecation)
132122
throw warning;
133123
}
134-
if (now) process.emit('warning', warning);
135-
else process.nextTick(doEmitWarning(warning));
124+
process.nextTick(doEmitWarning(warning));
125+
}
126+
127+
function emitWarningSync(warning) {
128+
process.emit('warning', createWarningObject(warning));
129+
}
130+
131+
function createWarningObject(warning, type, code, ctor, detail) {
132+
assert(typeof warning === 'string');
133+
// Improve error creation performance by skipping the error frames.
134+
// They are added in the `captureStackTrace()` function below.
135+
const tmpStackLimit = Error.stackTraceLimit;
136+
Error.stackTraceLimit = 0;
137+
// eslint-disable-next-line no-restricted-syntax
138+
warning = new Error(warning);
139+
Error.stackTraceLimit = tmpStackLimit;
140+
warning.name = String(type || 'Warning');
141+
if (code !== undefined) warning.code = code;
142+
if (detail !== undefined) warning.detail = detail;
143+
// eslint-disable-next-line no-restricted-syntax
144+
Error.captureStackTrace(warning, ctor || process.emitWarning);
145+
return warning;
136146
}
137147

138148
module.exports = {
149+
emitWarning,
150+
emitWarningSync,
139151
onWarning,
140-
emitWarning
141152
};

0 commit comments

Comments
 (0)