From 700344e388b57651c190222f570d658504b8c06f Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Fri, 20 Apr 2018 00:37:54 +0200 Subject: [PATCH] console: fix class inheritance regression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Luigi Pinca Reviewed-By: Daniel Bevenius Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung --- lib/console.js | 21 ++++++++++----------- test/parallel/test-console-instance.js | 7 +++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/console.js b/lib/console.js index f4610f745ea870..39bcf701bf83eb 100644 --- a/lib/console.js +++ b/lib/console.js @@ -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'); } diff --git a/test/parallel/test-console-instance.js b/test/parallel/test-console-instance.js index 2fd07ac41219a9..5a2e10b419cb03 100644 --- a/test/parallel/test-console-instance.js +++ b/test/parallel/test-console-instance.js @@ -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);