util: fix module prefixes during inspection

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: https://github.com/nodejs/node/issues/36151

PR-URL: https://github.com/nodejs/node/pull/36178
Fixes: https://github.com/nodejs/node/issues/35730
Refs: https://github.com/nodejs/node/pull/35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2020-11-19 16:31:05 +01:00 committed by Node.js GitHub Bot
parent 738cd60418
commit f78da00ab7
4 changed files with 25 additions and 5 deletions

View File

@ -637,7 +637,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] `;
@ -971,7 +971,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)) {

View File

@ -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" +
'}'
);

View File

@ -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');

View File

@ -11,7 +11,10 @@ const { inspect } = require('util');
await m.link(() => 0);
assert.strictEqual(
inspect(m.namespace),
'[Module] { a: <uninitialized>, b: undefined }');
'[Module: null prototype] { a: <uninitialized>, 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 }'
);
})().then(common.mustCall());