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 <ruben@bridgewater.de>

Backport-PR-URL: #37100
PR-URL: #36178
Fixes: #35730
Fixes: #36151
Refs: #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>
  • Loading branch information
BridgeAR authored and BethGriggs committed Feb 4, 2021
1 parent 408c7a6 commit 2f7944b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/internal/util/inspect.js
Expand Up @@ -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] `;
Expand Down Expand Up @@ -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)) {
Expand Down
14 changes: 14 additions & 0 deletions 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" +
'}'
);
5 changes: 4 additions & 1 deletion test/parallel/test-repl-import-referrer.js
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
8 changes: 6 additions & 2 deletions test/parallel/test-util-inspect-namespace.js
Expand Up @@ -13,7 +13,11 @@ 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 }'
);
})();

0 comments on commit 2f7944b

Please sign in to comment.