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 5 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 @@ -541,6 +541,9 @@ exports[`InspectedElementContext should support complex data types: 1: Inspected
"object_of_objects": {
"inner": {}
},
"object_with_symbol": {
"Symbol(name)": "hello"
},
"proxy": {},
"react_element": {},
"regexp": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ describe('InspectedElementContext', () => {
const objectOfObjects = {
inner: {string: 'abc', number: 123, boolean: true},
};
const objectWithSymbol = {
[Symbol('name')]: 'hello',
};
const typedArray = Int8Array.from([100, -100, 0]);
const arrayBuffer = typedArray.buffer;
const dataView = new DataView(arrayBuffer);
Expand Down Expand Up @@ -575,6 +578,7 @@ describe('InspectedElementContext', () => {
map={mapShallow}
map_of_maps={mapOfMaps}
object_of_objects={objectOfObjects}
object_with_symbol={objectWithSymbol}
proxy={proxyInstance}
react_element={<span />}
regexp={/abc/giu}
Expand Down Expand Up @@ -628,6 +632,7 @@ describe('InspectedElementContext', () => {
map,
map_of_maps,
object_of_objects,
object_with_symbol,
proxy,
react_element,
regexp,
Expand Down Expand Up @@ -732,6 +737,8 @@ describe('InspectedElementContext', () => {
);
expect(object_of_objects.inner[meta.preview_short]).toBe('{…}');

expect(object_with_symbol['Symbol(name)']).toBe('hello');
bvaughn marked this conversation as resolved.
Show resolved Hide resolved

expect(proxy[meta.inspectable]).toBe(false);
expect(proxy[meta.name]).toBe('function');
expect(proxy[meta.type]).toBe('function');
Expand Down
8 changes: 5 additions & 3 deletions packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import {
getDataType,
getDisplayNameForReactElement,
getAllEnumerableKeys,
getInObject,
formatDataForPreview,
setInObject,
Expand Down Expand Up @@ -291,16 +292,17 @@ export function dehydrate(
return createDehydrated(type, true, data, cleaned, path);
} else {
const object = {};
for (const name in data) {
getAllEnumerableKeys(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
27 changes: 22 additions & 5 deletions packages/react-devtools-shared/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,30 @@ 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;
}
}

export function getAllEnumerableKeys(
obj: Object,
): Array<string | number | Symbol> {
const keys = [];
for (const key in obj) {
keys.push(key);
}

return keys.concat(Object.getOwnPropertySymbols(obj));
}
Copy link
Contributor

@bvaughn bvaughn Sep 11, 2020

Choose a reason for hiding this comment

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

for...in includes inherited non-symbol keys, but Object.getOwnPropertySymbols does not include inherited symbol keys.

If we wanted to not include any inherited keys (including symbols) we could just do:

return [...Object.keys(object), ...Object.getOwnPropertySymbols(object)];

Or if we wanted to include both, I think we could:

export function getAllEnumerableKeys(
  object: Object,
): Array<string | number | Symbol> {
  let keys = [];
  let current = object;
  while (current != null) {
    const currentKeys = [
      ...Object.keys(current),
      ...Object.getOwnPropertySymbols(current),
    ];
    const descriptors = Object.getOwnPropertyDescriptors(current);
    for (let key of currentKeys) {
      if (descriptors[key].enumerable) {
        keys.push(key);
      }
    }
    current = current.__proto__;
  }
  return keys;
}

Which is best? I guess including inherited keys would be preferable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes @bvaughn you are right , I'm going to change it. Thank you

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 :)


export function getDisplayName(
type: Function,
fallbackName: string = 'Anonymous',
Expand Down Expand Up @@ -657,15 +671,18 @@ export function formatDataForPreview(
return data.toString();
case 'object':
if (showFormattedValue) {
const keys = Object.keys(data).sort(alphaSortKeys);
const keys = getAllEnumerableKeys(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