From 2f7944b18b04a67746926d8e7450765d883349ea Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 19 Nov 2020 16:31:05 +0100 Subject: [PATCH] util: fix module prefixes during inspection Signed-off-by: Ruben Bridgewater Backport-PR-URL: https://github.com/nodejs/node/pull/37100 PR-URL: https://github.com/nodejs/node/pull/36178 Fixes: https://github.com/nodejs/node/issues/35730 Fixes: https://github.com/nodejs/node/issues/36151 Refs: https://github.com/nodejs/node/pull/35754 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott Reviewed-By: Guy Bedford Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 4 ++-- .../es-module/test-esm-loader-custom-condition.mjs | 14 ++++++++++++++ test/parallel/test-repl-import-referrer.js | 5 ++++- test/parallel/test-util-inspect-namespace.js | 8 ++++++-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index f6b60ba872c67f..b8f2943961114f 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -645,7 +645,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) { function getPrefix(constructor, tag, fallback, size = '') { if (constructor === null) { - if (tag !== '') { + if (tag !== '' && fallback !== tag) { return `[${fallback}${size}: null prototype] [${tag}] `; } return `[${fallback}${size}: null prototype] `; @@ -979,7 +979,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { braces[0] = `${getPrefix(constructor, tag, 'WeakMap')}{`; formatter = ctx.showHidden ? formatWeakMap : formatWeakCollection; } else if (isModuleNamespaceObject(value)) { - braces[0] = `[${tag}] {`; + braces[0] = `${getPrefix(constructor, tag, 'Module')}{`; // Special handle keys for namespace objects. formatter = formatNamespaceObject.bind(null, keys); } else if (isBoxedPrimitive(value)) { diff --git a/test/es-module/test-esm-loader-custom-condition.mjs b/test/es-module/test-esm-loader-custom-condition.mjs index 26d1db4842f621..075e4fe2b66029 100644 --- a/test/es-module/test-esm-loader-custom-condition.mjs +++ b/test/es-module/test-esm-loader-custom-condition.mjs @@ -1,7 +1,21 @@ // Flags: --experimental-loader ./test/fixtures/es-module-loaders/loader-with-custom-condition.mjs import '../common/index.mjs'; import assert from 'assert'; +import util from 'util'; import * as ns from '../fixtures/es-modules/conditional-exports.mjs'; assert.deepStrictEqual({ ...ns }, { default: 'from custom condition' }); + +assert.strictEqual( + util.inspect(ns, { showHidden: false }), + "[Module: null prototype] { default: 'from custom condition' }" +); + +assert.strictEqual( + util.inspect(ns, { showHidden: true }), + '[Module: null prototype] {\n' + + " default: 'from custom condition',\n" + + " [Symbol(Symbol.toStringTag)]: 'Module'\n" + + '}' +); diff --git a/test/parallel/test-repl-import-referrer.js b/test/parallel/test-repl-import-referrer.js index 33bc442e6f69c8..d77d70a031a14a 100644 --- a/test/parallel/test-repl-import-referrer.js +++ b/test/parallel/test-repl-import-referrer.js @@ -16,7 +16,10 @@ child.stdout.on('data', (data) => { child.on('exit', common.mustCall(() => { const results = output.replace(/^> /mg, '').split('\n').slice(2); - assert.deepStrictEqual(results, ['[Module] { message: \'A message\' }', '']); + assert.deepStrictEqual( + results, + ['[Module: null prototype] { message: \'A message\' }', ''] + ); })); child.stdin.write('await import(\'./message.mjs\');\n'); diff --git a/test/parallel/test-util-inspect-namespace.js b/test/parallel/test-util-inspect-namespace.js index 89b26fcdbcf1b2..00c952c05cffb2 100644 --- a/test/parallel/test-util-inspect-namespace.js +++ b/test/parallel/test-util-inspect-namespace.js @@ -13,7 +13,11 @@ const { inspect } = require('util'); await m.link(() => 0); assert.strictEqual( inspect(m.namespace), - '[Module] { a: , b: undefined }'); + '[Module: null prototype] { a: , b: undefined }' + ); await m.evaluate(); - assert.strictEqual(inspect(m.namespace), '[Module] { a: 1, b: 2 }'); + assert.strictEqual( + inspect(m.namespace), + '[Module: null prototype] { a: 1, b: 2 }' + ); })();