Skip to content

Commit 4f68133

Browse files
apapirovskijasnell
authored andcommitted
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: #20158 Fixes: #20157 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent bed57ef commit 4f68133

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/console.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,21 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
6262
return new Console(...arguments);
6363
}
6464

65-
let stdout, stderr, ignoreErrors, colorMode;
66-
if (options && typeof options.write !== 'function') {
67-
({
68-
stdout,
69-
stderr = stdout,
70-
ignoreErrors = true,
71-
colorMode = 'auto'
72-
} = options);
73-
} else {
74-
return new Console({
65+
if (!options || typeof options.write === 'function') {
66+
options = {
7567
stdout: options,
7668
stderr: arguments[1],
7769
ignoreErrors: arguments[2]
78-
});
70+
};
7971
}
8072

73+
const {
74+
stdout,
75+
stderr = stdout,
76+
ignoreErrors = true,
77+
colorMode = 'auto'
78+
} = options;
79+
8180
if (!stdout || typeof stdout.write !== 'function') {
8281
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
8382
}

test/parallel/test-console-instance.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ out.write = common.mustCall((d) => {
8989
// Console() detects if it is called without `new` keyword.
9090
Console(out, err);
9191

92+
// Extending Console works.
93+
class MyConsole extends Console {
94+
hello() {}
95+
}
96+
const myConsole = new MyConsole(process.stdout);
97+
assert.strictEqual(typeof myConsole.hello, 'function');
98+
9299
// Instance that does not ignore the stream errors.
93100
const c2 = new Console(out, err, false);
94101

0 commit comments

Comments
 (0)