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

[DevTools] Display RegExp values in props/state #17690

Merged
merged 2 commits into from
Dec 22, 2019
Merged
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 @@ -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