Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: refactor emitWarning #20726

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function setupProcessWarnings() {
// process.emitWarning(str[, type[, code]][, ctor])
// process.emitWarning(str[, options])
process.emitWarning = function(warning, type, code, ctor, now) {
var detail;
let detail;
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
ctor = type.ctor;
code = type.code;
Expand All @@ -117,23 +117,23 @@ function setupProcessWarnings() {
code = undefined;
type = 'Warning';
}
if (type !== undefined && typeof type !== 'string') {
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
}
if (typeof code === 'function') {
ctor = code;
code = undefined;
}
if (code !== undefined && typeof code !== 'string')
} else if (code !== undefined && typeof code !== 'string') {
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
if (type !== undefined && typeof type !== 'string')
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
if (warning === undefined || typeof warning === 'string') {
}
if (typeof warning === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the possibility of an undefined warning that has the other properties specified.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not officially supported.

Copy link
Member

@apapirovski apapirovski May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While not documented, as far as I can tell this was requested in #4782

It has also worked this way for two years... I don't know what usage people have for this or what they're passing to it. It might very well be a breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @apapirovski on this. This is a breaking change even if not properly documented.

Copy link
Member Author

@BridgeAR BridgeAR May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.emitWarning() produces: (node:XXXX) Warning. I am not convinced that this is useful at all. This seems like a bugfix as no args should result in an error. Therefore I would say it is a patch. However, I would also keep this if it makes this semver-major.

Ping @nodejs/tsc please also give an opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.emitWarning() produces: (node:XXXX) Warning. I am not convinced that this is useful at all.

process.emitWarning(undefined, 'fhqwhgads', 'come-on') produces the slightly more useful (node:91812) [come-on] fhqwhgads.

I have no idea if anyone uses that pattern or not. Maybe it's time for me to figure out how to use Gzemnid.

In the spirit of the onboarding-extras.md:

be conservative – that is, if a change has the remote chance of breaking something, go for semver-major

It seems like this should be semver-major as it has a "remote change of breaking something".

(If we're not going to call this semver-major, I won't push back, but we really ought to remove the quoted sentence above from the doc since it is clearly not something we abide by in that case.)

// eslint-disable-next-line no-restricted-syntax
warning = new Error(warning);
warning.name = String(type || 'Warning');
if (code !== undefined) warning.code = code;
if (detail !== undefined) warning.detail = detail;
Error.captureStackTrace(warning, ctor || process.emitWarning);
}
if (!(warning instanceof Error)) {
} else if (!(warning instanceof Error)) {
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
}
if (warning.name === 'DeprecationWarning') {
Expand Down
39 changes: 23 additions & 16 deletions test/parallel/test-process-emitwarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class CustomWarning extends Error {
[testMsg, { type: testType, code: testCode, detail: [] }],
[testMsg, { type: testType, code: testCode, detail: null }],
[testMsg, { type: testType, code: testCode, detail: 1 }]
].forEach((i) => {
process.emitWarning.apply(null, i);
].forEach((args) => {
process.emitWarning(...args);
});

const warningNoToString = new CustomWarning();
Expand All @@ -57,18 +57,25 @@ warningThrowToString.toString = function() {
};
process.emitWarning(warningThrowToString);

const expectedError =
common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }, 11);

// TypeError is thrown on invalid input
assert.throws(() => process.emitWarning(1), expectedError);
assert.throws(() => process.emitWarning({}), expectedError);
assert.throws(() => process.emitWarning(true), expectedError);
assert.throws(() => process.emitWarning([]), expectedError);
assert.throws(() => process.emitWarning('', '', {}), expectedError);
assert.throws(() => process.emitWarning('', 1), expectedError);
assert.throws(() => process.emitWarning('', '', 1), expectedError);
assert.throws(() => process.emitWarning('', true), expectedError);
assert.throws(() => process.emitWarning('', '', true), expectedError);
assert.throws(() => process.emitWarning('', []), expectedError);
assert.throws(() => process.emitWarning('', '', []), expectedError);
[
[1],
[{}],
[true],
[[]],
['', '', {}],
['', 1],
['', '', 1],
['', true],
['', '', true],
['', []],
['', '', []],
[],
[undefined, 'foo', 'bar'],
[undefined]
].forEach((args) => {
common.expectsError(
() => process.emitWarning(...args),
{ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }
);
});