concordancejs / concordance

Compare, format, diff and serialize any JavaScript value

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error thrown on `Proxy`s with undescribed own properties

ninevra opened this issue · comments

compare(), format(), serialize(), and diff() throw an error when called on objects which have own properties with no property descriptors. (As far as I know, the only such objects are Proxys.)

concordance assumes that, if Object.getOwnPropertyNames(input) returns e.g. ['name'], then Object.getOwnPropertyDescriptor(input, 'name') will return a property descriptor. This is true for non-Proxy inputs, but can be false for Proxys, which in most cases don't have to respect any relation between the ownKeys and ownPropertyDescriptor operations.

This can lead to throwing

TypeError {
  message: 'Cannot read property \'enumerable\' of undefined',
}

at

if (accept && Object.getOwnPropertyDescriptor(obj, name).enumerable) {

I encountered this when I accidentally tried to snapshot a jsdom Window object, which apparently has some ownKeys that lack descriptors.

I'm not sure whether there's a better way to handle this. The "value" of the property can still be retrieved with e.g. Reflect.get(), so that could be used for comparison; but enumerability and other property attributes are strange.

As far as I can tell, undescribed properties do not behave identically to any valid property descriptor, nor to the absence of a property. They are not themselves listed in enumerations, but neither do they mask the presence of enumerable prototype properties, as truly unenumerable properties do. They may or may not satisfy Reflect.has(object, property).

Should we ignore these? We already ignore non-enumerable properties.

That makes sense to me. These properties certainly aren't enumerable, which seems close enough to non-enumerable to similarly ignore.