Skip to content

Commit

Permalink
[DevTools] Display RegExp values in props/state (#17690)
Browse files Browse the repository at this point in the history
Previously, when props/state contained a regexp, it was shown as an
empty object. This commit adds regexps as values in need of special
rehydration (like Symbols or TypedArrays), and display them as a user
might expect.

Co-authored-by: Zirak <[email protected]>
  • Loading branch information
Brian Vaughn and Zirak authored Dec 22, 2019
1 parent 1959671 commit f887d1a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ exports[`InspectedElementContext should support complex data types: 1: Inspected
"inner": {}
},
"react_element": {},
"regexp": {},
"set": {
"0": "abc",
"1": 123
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ describe('InspectedElementContext', () => {
map_of_maps={mapOfMaps}
object_of_objects={objectOfObjects}
react_element={<span />}
regexp={/abc/giu}
set={setShallow}
set_of_sets={setOfSets}
symbol={Symbol('symbol')}
Expand Down Expand Up @@ -604,6 +605,7 @@ describe('InspectedElementContext', () => {
map_of_maps,
object_of_objects,
react_element,
regexp,
set,
set_of_sets,
symbol,
Expand Down Expand Up @@ -699,6 +701,12 @@ describe('InspectedElementContext', () => {
expect(react_element[meta.preview_long]).toBe('<span />');
expect(react_element[meta.preview_short]).toBe('<span />');

expect(regexp[meta.inspectable]).toBe(false);
expect(regexp[meta.name]).toBe('/abc/giu');
expect(regexp[meta.preview_long]).toBe('/abc/giu');
expect(regexp[meta.preview_short]).toBe('/abc/giu');
expect(regexp[meta.type]).toBe('regexp');

expect(set[meta.inspectable]).toBeUndefined(); // Complex type
expect(set[meta.name]).toBe('Set');
expect(set[meta.type]).toBe('iterator');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Object {
"inner": {}
},
"react_element": {},
"regexp": {},
"set": {
"0": "abc",
"1": 123
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ describe('InspectedElementContext', () => {
map_of_maps={mapOfMaps}
object_of_objects={objectOfObjects}
react_element={<span />}
regexp={/abc/giu}
set={setShallow}
set_of_sets={setOfSets}
symbol={Symbol('symbol')}
Expand Down Expand Up @@ -212,6 +213,7 @@ describe('InspectedElementContext', () => {
map_of_maps,
object_of_objects,
react_element,
regexp,
set,
set_of_sets,
symbol,
Expand Down Expand Up @@ -279,6 +281,12 @@ describe('InspectedElementContext', () => {
expect(react_element[meta.name]).toBe('span');
expect(react_element[meta.type]).toBe('react_element');

expect(regexp[meta.inspectable]).toBe(false);
expect(regexp[meta.name]).toBe('/abc/giu');
expect(regexp[meta.preview_long]).toBe('/abc/giu');
expect(regexp[meta.preview_short]).toBe('/abc/giu');
expect(regexp[meta.type]).toBe('regexp');

expect(set[meta.inspectable]).toBeUndefined(); // Complex type
expect(set[meta.name]).toBe('Set');
expect(set[meta.type]).toBe('iterator');
Expand Down
10 changes: 10 additions & 0 deletions packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ export function dehydrate(
type,
};

case 'regexp':
cleaned.push(path);
return {
inspectable: false,
preview_short: formatDataForPreview(data, false),
preview_long: formatDataForPreview(data, true),
name: data.toString(),
type,
};

case 'object':
isPathWhitelistedCheck = isPathWhitelisted(path);
if (level >= LEVEL_THRESHOLD && !isPathWhitelistedCheck) {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-devtools-shared/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export type DataType =
| 'number'
| 'object'
| 'react_element'
| 'regexp'
| 'string'
| 'symbol'
| 'typed_array'
Expand Down Expand Up @@ -395,6 +396,8 @@ export function getDataType(data: Object): DataType {
return 'array_buffer';
} else if (typeof data[Symbol.iterator] === 'function') {
return 'iterator';
} else if (data.constructor.name === 'RegExp') {
return 'regexp';
} else if (Object.prototype.toString.call(data) === '[object Date]') {
return 'date';
}
Expand Down Expand Up @@ -504,6 +507,8 @@ export function formatDataForPreview(
return `"${data}"`;
case 'bigint':
return truncateForDisplay(data.toString() + 'n');
case 'regexp':
return truncateForDisplay(data.toString());
case 'symbol':
return truncateForDisplay(data.toString());
case 'react_element':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function SimpleValues() {
true={true}
false={false}
function={noop}
regex={/abc[123]+/i}
/>
);
}
Expand Down

0 comments on commit f887d1a

Please sign in to comment.