Skip to content

Commit

Permalink
console: fix class inheritance regression
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
apapirovski committed Apr 20, 2018
1 parent 1b438a7 commit 700344e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 700344e

Please sign in to comment.