Skip to content

Commit f887d1a

Browse files
Brian VaughnZirak
andauthored
[DevTools] Display RegExp values in props/state (#17690)
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]>
1 parent 1959671 commit f887d1a

File tree

7 files changed

+34
-0
lines changed

7 files changed

+34
-0
lines changed

packages/react-devtools-shared/src/__tests__/__snapshots__/inspectedElementContext-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ exports[`InspectedElementContext should support complex data types: 1: Inspected
511511
"inner": {}
512512
},
513513
"react_element": {},
514+
"regexp": {},
514515
"set": {
515516
"0": "abc",
516517
"1": 123

packages/react-devtools-shared/src/__tests__/inspectedElementContext-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ describe('InspectedElementContext', () => {
555555
map_of_maps={mapOfMaps}
556556
object_of_objects={objectOfObjects}
557557
react_element={<span />}
558+
regexp={/abc/giu}
558559
set={setShallow}
559560
set_of_sets={setOfSets}
560561
symbol={Symbol('symbol')}
@@ -604,6 +605,7 @@ describe('InspectedElementContext', () => {
604605
map_of_maps,
605606
object_of_objects,
606607
react_element,
608+
regexp,
607609
set,
608610
set_of_sets,
609611
symbol,
@@ -699,6 +701,12 @@ describe('InspectedElementContext', () => {
699701
expect(react_element[meta.preview_long]).toBe('<span />');
700702
expect(react_element[meta.preview_short]).toBe('<span />');
701703

704+
expect(regexp[meta.inspectable]).toBe(false);
705+
expect(regexp[meta.name]).toBe('/abc/giu');
706+
expect(regexp[meta.preview_long]).toBe('/abc/giu');
707+
expect(regexp[meta.preview_short]).toBe('/abc/giu');
708+
expect(regexp[meta.type]).toBe('regexp');
709+
702710
expect(set[meta.inspectable]).toBeUndefined(); // Complex type
703711
expect(set[meta.name]).toBe('Set');
704712
expect(set[meta.type]).toBe('iterator');

packages/react-devtools-shared/src/__tests__/legacy/__snapshots__/inspectElement-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Object {
152152
"inner": {}
153153
},
154154
"react_element": {},
155+
"regexp": {},
155156
"set": {
156157
"0": "abc",
157158
"1": 123

packages/react-devtools-shared/src/__tests__/legacy/inspectElement-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ describe('InspectedElementContext', () => {
185185
map_of_maps={mapOfMaps}
186186
object_of_objects={objectOfObjects}
187187
react_element={<span />}
188+
regexp={/abc/giu}
188189
set={setShallow}
189190
set_of_sets={setOfSets}
190191
symbol={Symbol('symbol')}
@@ -212,6 +213,7 @@ describe('InspectedElementContext', () => {
212213
map_of_maps,
213214
object_of_objects,
214215
react_element,
216+
regexp,
215217
set,
216218
set_of_sets,
217219
symbol,
@@ -279,6 +281,12 @@ describe('InspectedElementContext', () => {
279281
expect(react_element[meta.name]).toBe('span');
280282
expect(react_element[meta.type]).toBe('react_element');
281283

284+
expect(regexp[meta.inspectable]).toBe(false);
285+
expect(regexp[meta.name]).toBe('/abc/giu');
286+
expect(regexp[meta.preview_long]).toBe('/abc/giu');
287+
expect(regexp[meta.preview_short]).toBe('/abc/giu');
288+
expect(regexp[meta.type]).toBe('regexp');
289+
282290
expect(set[meta.inspectable]).toBeUndefined(); // Complex type
283291
expect(set[meta.name]).toBe('Set');
284292
expect(set[meta.type]).toBe('iterator');

packages/react-devtools-shared/src/hydration.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ export function dehydrate(
271271
type,
272272
};
273273

274+
case 'regexp':
275+
cleaned.push(path);
276+
return {
277+
inspectable: false,
278+
preview_short: formatDataForPreview(data, false),
279+
preview_long: formatDataForPreview(data, true),
280+
name: data.toString(),
281+
type,
282+
};
283+
274284
case 'object':
275285
isPathWhitelistedCheck = isPathWhitelisted(path);
276286
if (level >= LEVEL_THRESHOLD && !isPathWhitelistedCheck) {

packages/react-devtools-shared/src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ export type DataType =
340340
| 'number'
341341
| 'object'
342342
| 'react_element'
343+
| 'regexp'
343344
| 'string'
344345
| 'symbol'
345346
| 'typed_array'
@@ -395,6 +396,8 @@ export function getDataType(data: Object): DataType {
395396
return 'array_buffer';
396397
} else if (typeof data[Symbol.iterator] === 'function') {
397398
return 'iterator';
399+
} else if (data.constructor.name === 'RegExp') {
400+
return 'regexp';
398401
} else if (Object.prototype.toString.call(data) === '[object Date]') {
399402
return 'date';
400403
}
@@ -504,6 +507,8 @@ export function formatDataForPreview(
504507
return `"${data}"`;
505508
case 'bigint':
506509
return truncateForDisplay(data.toString() + 'n');
510+
case 'regexp':
511+
return truncateForDisplay(data.toString());
507512
case 'symbol':
508513
return truncateForDisplay(data.toString());
509514
case 'react_element':

packages/react-devtools-shell/src/app/InspectableElements/SimpleValues.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default function SimpleValues() {
2424
true={true}
2525
false={false}
2626
function={noop}
27+
regex={/abc[123]+/i}
2728
/>
2829
);
2930
}

0 commit comments

Comments
 (0)