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

fix: support canonical module #5040

Merged
merged 5 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function emptyRepresentation(value, typeHint) {
* canonicalType(global) // 'global'
* canonicalType(new String('foo') // 'object'
* canonicalType(async function() {}) // 'asyncfunction'
* canonicalType(await import(name)) // 'module'
* canonicalType(Object.create(null)) // 'null-prototype'
*/
var canonicalType = (exports.canonicalType = function canonicalType(value) {
if (value === undefined) {
Expand All @@ -147,7 +147,10 @@ var canonicalType = (exports.canonicalType = function canonicalType(value) {
return 'null';
} else if (Buffer.isBuffer(value)) {
return 'buffer';
} else if (Object.getPrototypeOf(value) === null) {
return 'null-prototype';
}

return Object.prototype.toString
.call(value)
.replace(/^\[.+\s(.+?)]$/, '$1')
Expand Down Expand Up @@ -213,7 +216,7 @@ exports.type = function type(value) {
exports.stringify = function (value) {
var typeHint = canonicalType(value);

if (!~['object', 'array', 'function'].indexOf(typeHint)) {
if (!~['object', 'array', 'function', 'null-prototype'].indexOf(typeHint)) {
if (typeHint === 'buffer') {
var json = Buffer.prototype.toJSON.call(value);
// Based on the toJSON result
Expand Down Expand Up @@ -399,8 +402,12 @@ exports.canonicalize = function canonicalize(value, stack, typeHint) {
break;
}
/* falls through */
case 'null-prototype':
case 'object':
canonicalizedObj = canonicalizedObj || {};
if (typeHint === 'null-prototype' && Symbol.toStringTag in value) {
canonicalizedObj['[Symbol.toStringTag]'] = value[Symbol.toStringTag];
}
withStack(value, function () {
Object.keys(value)
.sort()
Expand Down
38 changes: 38 additions & 0 deletions test/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,44 @@ describe('lib/utils', function () {
].join('\n');
expect(stringify(expected), 'to be', actual);
});

describe('should represent null prototypes', function () {
it('With explicit names', function () {
const foo = Object.create(null, {
[Symbol.toStringTag]: {value: 'Foo'},
bing: {get: () => 'bong', enumerable: true}
});
const expected = [
'{',
' "[Symbol.toStringTag]": "Foo"',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we wanted to either:

  1. Always expose the toStringTag if present
  2. Never expose the toStringTag

Then we could just return object instead of null-prototype and the logic would work.

Only really need a special case if we are conditionally exposing it...

Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong preference either way. What's working now fixes the issue and is nicely informative on my end.

' "bing": "bong"',
'}'
].join('\n');

expect(stringify(foo), 'to be', expected);
});

it('Without names', function () {
const unnamed = {
bing: 'bong',
abc: 123
};
unnamed.self = unnamed;
const expected = [
'{',
' "abc": 123',
' "bing": "bong"',
' "self": [Circular]',
'}'
].join('\n');

expect(
stringify(Object.setPrototypeOf(unnamed, null)),
'to be',
expected
);
});
});
});

it('should canonicalize the object', function () {
Expand Down
Loading