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:
parent
1b438a7737
commit
700344e388
@ -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');
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user