Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix module inspection & instanceof check during inspect #36178

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/internal/util/inspect.js
Expand Up @@ -534,6 +534,14 @@ function getEmptyFormatArray() {
return [];
}

function isInstanceof(object, proto) {
try {
return object instanceof proto;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: instanceof can throw

} catch {
return false;
}
}

function getConstructorName(obj, ctx, recurseTimes, protoProps) {
let firstProto;
const tmp = obj;
Expand All @@ -542,7 +550,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
if (descriptor !== undefined &&
typeof descriptor.value === 'function' &&
descriptor.value.name !== '' &&
tmp instanceof descriptor.value) {
isInstanceof(tmp, descriptor.value)) {
if (protoProps !== undefined &&
(firstProto !== obj ||
!builtInObjects.has(descriptor.value.name))) {
Expand Down Expand Up @@ -644,7 +652,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 @@ -978,7 +986,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
7 changes: 5 additions & 2 deletions test/parallel/test-util-inspect-namespace.js
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());
11 changes: 11 additions & 0 deletions test/parallel/test-util-inspect.js
Expand Up @@ -2866,6 +2866,17 @@ assert.strictEqual(
);
}

// Check that prototypes with a null prototype are inspectable.
// Regression test for https://github.com/nodejs/node/issues/35730
{
function Func() {}
Func.prototype = null;
const object = {};
object.constructor = Func;

assert.strictEqual(util.inspect(object), '{ constructor: [Function: Func] }');
}

// Test changing util.inspect.colors colors and aliases.
{
const colors = util.inspect.colors;
Expand Down