console: fix class inheritance regression

Due to a return statement with new inside the function, extending
Console class does not work when passing in just the stdout arg.

PR-URL: https://github.com/nodejs/node/pull/20158
Fixes: https://github.com/nodejs/node/issues/20157
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Anatoli Papirovski 2018-04-20 00:37:54 +02:00
parent 1b438a7737
commit 700344e388
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 17 additions and 11 deletions

View File

@ -62,22 +62,21 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
return new Console(...arguments);
}
let stdout, stderr, ignoreErrors, colorMode;
if (options && typeof options.write !== 'function') {
({
stdout,
stderr = stdout,
ignoreErrors = true,
colorMode = 'auto'
} = options);
} else {
return new Console({
if (!options || typeof options.write === 'function') {
options = {
stdout: options,
stderr: arguments[1],
ignoreErrors: arguments[2]
});
};
}
const {
stdout,
stderr = stdout,
ignoreErrors = true,
colorMode = 'auto'
} = options;
if (!stdout || typeof stdout.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
}

View File

@ -89,6 +89,13 @@ out.write = common.mustCall((d) => {
// Console() detects if it is called without `new` keyword.
Console(out, err);
// Extending Console works.
class MyConsole extends Console {
hello() {}
}
const myConsole = new MyConsole(process.stdout);
assert.strictEqual(typeof myConsole.hello, 'function');
// Instance that does not ignore the stream errors.
const c2 = new Console(out, err, false);