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,21 +62,20 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
|
|||||||
return new Console(...arguments);
|
return new Console(...arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
let stdout, stderr, ignoreErrors, colorMode;
|
if (!options || typeof options.write === 'function') {
|
||||||
if (options && typeof options.write !== 'function') {
|
options = {
|
||||||
({
|
stdout: options,
|
||||||
|
stderr: arguments[1],
|
||||||
|
ignoreErrors: arguments[2]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
stdout,
|
stdout,
|
||||||
stderr = stdout,
|
stderr = stdout,
|
||||||
ignoreErrors = true,
|
ignoreErrors = true,
|
||||||
colorMode = 'auto'
|
colorMode = 'auto'
|
||||||
} = options);
|
} = options;
|
||||||
} else {
|
|
||||||
return new Console({
|
|
||||||
stdout: options,
|
|
||||||
stderr: arguments[1],
|
|
||||||
ignoreErrors: arguments[2]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stdout || typeof stdout.write !== 'function') {
|
if (!stdout || typeof stdout.write !== 'function') {
|
||||||
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
|
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() detects if it is called without `new` keyword.
|
||||||
Console(out, err);
|
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.
|
// Instance that does not ignore the stream errors.
|
||||||
const c2 = new Console(out, err, false);
|
const c2 = new Console(out, err, false);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user