Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ export function getKeys(obj: {}): string[] {
*/
export const getDescriptor = (instance: any, propName: string): PropertyDescriptor | undefined =>
Object.getOwnPropertyDescriptor(instance, propName) ||
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(instance), propName);
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(instance) ?? {}, propName);
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ const serializable: Set<PropType> = new Set([
PropType.Unknown,
]);

const getConstructorName = (prop: object, fallback: string): string => {
const constructorName = (prop as {constructor?: {name?: unknown}}).constructor?.name;

return typeof constructorName === 'string' && constructorName.length > 0
? constructorName
: fallback;
};

const typeToDescriptorPreview: Formatter<string> = {
[PropType.Array]: (prop: Array<unknown>) => `Array(${prop.length})`,
[PropType.Set]: (prop: Set<unknown>) => `Set(${prop.size})`,
Expand All @@ -58,12 +66,16 @@ const typeToDescriptorPreview: Formatter<string> = {
[PropType.Boolean]: (prop: boolean) => truncate(prop.toString()),
[PropType.String]: (prop: string) => `"${prop}"`,
[PropType.Function]: (prop: Function) => `${prop.name ? 'ƒ ' : ''}(...)`,
[PropType.HTMLNode]: (prop: Node) => prop.constructor.name,
[PropType.HTMLNode]: (prop: Node) => getConstructorName(prop, 'Node'),
[PropType.Null]: (_: null) => 'null',
[PropType.Number]: (prop: any) => prop.toString(),
[PropType.Object]: (prop: Object) =>
(prop.constructor.name !== 'Object' ? `${prop.constructor.name} ` : '') +
(getKeys(prop).length > 0 ? '{...}' : '{}'),
[PropType.Object]: (prop: object) => {
const constructorName = getConstructorName(prop, 'Object');
return (
(constructorName !== 'Object' ? `${constructorName} ` : '') +
(getKeys(prop).length > 0 ? '{...}' : '{}')
);
},
[PropType.Symbol]: (symbol: symbol) => `Symbol(${symbol.description})`,
[PropType.Undefined]: (_: undefined) => 'undefined',
[PropType.Date]: (prop: unknown) => {
Expand Down Expand Up @@ -308,7 +320,10 @@ function getNestedDescriptorValue(
case PropType.Object:
return nodes.reduce(
(accumulator, nestedProp) => {
if (prop.hasOwnProperty(nestedProp.name) && !ignoreList.has(nestedProp.name)) {
if (
Object.prototype.hasOwnProperty.call(value, nestedProp.name) &&
!ignoreList.has(nestedProp.name)
) {
accumulator[nestedProp.name] = nestedSerializer(
value,
nestedProp.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {PropType} from '../../../../protocol';

import {getDescriptor, getKeys} from './object-utils';
import {deeplySerializeSelectedProperties} from './state-serializer';
import {deeplySerializeSelectedProperties, serializeDirectiveState} from './state-serializer';

const QUERY_1_1: any[] = [];

Expand Down Expand Up @@ -494,6 +494,50 @@ describe('deeplySerializeSelectedProperties', () => {
});
});

it('should preview objects without prototypes as plain objects', () => {
const grouped = Object.create(null);
grouped.foo = 1;

const result = serializeDirectiveState({grouped});

expect(result['grouped']).toEqual({
type: PropType.Object,
editable: false,
expandable: true,
preview: '{...}',
containerType: null,
});
});

it('should deeply serialize selected properties from objects without prototypes', () => {
const grouped = Object.create(null);
grouped.foo = 1;

const result = deeplySerializeSelectedProperties({grouped}, [
{name: 'grouped', children: [{name: 'foo', children: []}]},
]);

expect(result).toEqual({
grouped: {
type: PropType.Object,
editable: false,
expandable: true,
preview: '{...}',
value: {
foo: {
type: PropType.Number,
expandable: false,
editable: true,
preview: '1',
value: 1,
containerType: null,
},
},
containerType: null,
},
});
});

it('getDescriptor should get the descriptors for both getters and setters correctly from the prototype', () => {
const instance = {
__proto__: {
Expand Down Expand Up @@ -555,6 +599,12 @@ describe('deeplySerializeSelectedProperties', () => {
expect(getKeys(instance)).toEqual([]);
});

it('getDescriptor should not throw on object without prototype', () => {
const instance = Object.create(null);

expect(getDescriptor(instance, 'foo')).toBeUndefined();
});

it('getKeys would ignore getters and setters for "__proto__"', () => {
const instance = {
baz: 2,
Expand Down
Loading