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

React DevTools: Show symbols used as keys in state #19786

Merged
merged 9 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -530,7 +530,7 @@ describe('InspectedElementContext', () => {
['second', mapShallow],
]);
const objectOfObjects = {
inner: {string: 'abc', number: 123, boolean: true},
inner: {str: 'ab', nb: 12, bool: true, [Symbol('sb')]: 'sb'},
Copy link
Collaborator

Choose a reason for hiding this comment

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

For easier review you could just add the additional symbol. Changing the other keys and values can be distracting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done :)

};
const typedArray = Int8Array.from([100, -100, 0]);
const arrayBuffer = typedArray.buffer;
Expand Down Expand Up @@ -723,12 +723,12 @@ describe('InspectedElementContext', () => {
);
expect(map_of_maps[meta.preview_short]).toBe('Map(2)');

expect(object_of_objects.inner[meta.size]).toBe(3);
expect(object_of_objects.inner[meta.size]).toBe(4);
expect(object_of_objects.inner[meta.inspectable]).toBe(true);
expect(object_of_objects.inner[meta.name]).toBe('');
expect(object_of_objects.inner[meta.type]).toBe('object');
expect(object_of_objects.inner[meta.preview_long]).toBe(
'{boolean: true, number: 123, string: "abc"}',
'{Symbol(sb): "sb", bool: true, nb: 12, str: "ab"}',
);
expect(object_of_objects.inner[meta.preview_short]).toBe('{…}');

Expand Down
9 changes: 5 additions & 4 deletions packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function createDehydrated(
if (type === 'array' || type === 'typed_array') {
dehydrated.size = data.length;
} else if (type === 'object') {
dehydrated.size = Object.keys(data).length;
dehydrated.size = Reflect.ownKeys(data).length;
}

if (type === 'iterator' || type === 'typed_array') {
Expand Down Expand Up @@ -291,16 +291,17 @@ export function dehydrate(
return createDehydrated(type, true, data, cleaned, path);
} else {
const object = {};
for (const name in data) {
Reflect.ownKeys(data).forEach(key => {
const name = key.toString();
object[name] = dehydrate(
data[name],
data[key],
cleaned,
unserializable,
path.concat([name]),
isPathAllowed,
isPathAllowedCheck ? 1 : level + 1,
);
}
});
return object;
}

Expand Down
16 changes: 11 additions & 5 deletions packages/react-devtools-shared/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ const cachedDisplayNames: WeakMap<Function, string> = new WeakMap();
// Try to reuse the already encoded strings.
const encodedStringCache = new LRU({max: 1000});

export function alphaSortKeys(a: string, b: string): number {
if (a > b) {
export function alphaSortKeys(
a: string | number | Symbol,
b: string | number | Symbol,
): number {
if (a.toString() > b.toString()) {
return 1;
} else if (b > a) {
} else if (b.toString() > a.toString()) {
return -1;
} else {
return 0;
Expand Down Expand Up @@ -657,15 +660,18 @@ export function formatDataForPreview(
return data.toString();
case 'object':
if (showFormattedValue) {
const keys = Object.keys(data).sort(alphaSortKeys);
const keys = Reflect.ownKeys(data).sort(alphaSortKeys);

let formatted = '';
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (i > 0) {
formatted += ', ';
}
formatted += `${key}: ${formatDataForPreview(data[key], false)}`;
formatted += `${key.toString()}: ${formatDataForPreview(
data[key],
false,
)}`;
if (formatted.length > MAX_PREVIEW_STRING_LENGTH) {
// Prevent doing a lot of unnecessary iteration...
break;
Expand Down