Skip to content

Commit

Permalink
util: fix module prefixes during inspection
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Bridgewater <[email protected]>

Fixes: #36151

PR-URL: #36178
Fixes: #35730
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed Dec 21, 2020
1 parent e449571 commit 13d6597
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] `;
Expand Down Expand Up @@ -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)) {
Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-loader-custom-condition.mjs
Original file line number Diff line number Diff line change
@@ -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" +
'}'
);
5 changes: 4 additions & 1 deletion test/parallel/test-repl-import-referrer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-util-inspect-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

0 comments on commit 13d6597

Please sign in to comment.