Skip to content

Commit 9635257

Browse files
authored
[DevTools] Preserve -Infinity in inspected values (facebook#36347)
## Summary `getDataType` collapsed both `Infinity` and `-Infinity` to the `'infinity'` data type, so a `-Infinity` value coming from inspected props/state/hooks was rehydrated on the frontend as `Infinity`. This adds a `'-infinity'` `DataType`, routes it through `dehydrate`/`hydrate` alongside the existing `'infinity'` arm, and makes `smartParse`/`smartStringify` (used for editable hook values) symmetric. ## Files - `packages/react-devtools-shared/src/utils.js` — extend `DataType`, split sign in `getDataType`, route `'-infinity'` through `formatDataForPreview`. - `packages/react-devtools-shared/src/hydration.js` — `dehydrate` and `hydrate` cases for `'-infinity'`. - `packages/react-devtools-shared/src/devtools/utils.js` — `smartParse` accepts `'-Infinity'`; `smartStringify` returns `'-Infinity'` for negative infinite values. - `packages/react-devtools-shared/src/__tests__/inspectedElement-test.js` and `legacy/inspectElement-test.js` — added `minus_infinity={-Infinity}` to the simple-data-types tests + snapshots. - `packages/react-devtools-shell/src/app/InspectableElements/SimpleValues.js` — added `minusInfinity` to the dev shell so the path is exercised manually. ## Test plan - [x] `yarn prettier` / `yarn linc` - [x] `yarn flow dom-node` — no errors - [x] `yarn test --silent --no-watchman -t "should support simple data types"` (source channel) - [x] `yarn test-www --silent --no-watchman -t "should support simple data types"` (www-modern) - [ ] `yarn test-build-devtools` — relies on a built bundle; left to CI per repo policy. Fixes facebook#32552
1 parent 4f273bd commit 9635257

6 files changed

Lines changed: 14 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ describe('InspectedElement', () => {
582582
boolean_false={false}
583583
boolean_true={true}
584584
infinity={Infinity}
585+
minus_infinity={-Infinity}
585586
integer_zero={0}
586587
integer_one={1}
587588
float={1.23}
@@ -604,6 +605,7 @@ describe('InspectedElement', () => {
604605
"infinity": Infinity,
605606
"integer_one": 1,
606607
"integer_zero": 0,
608+
"minus_infinity": -Infinity,
607609
"nan": NaN,
608610
"string": "abc",
609611
"string_empty": "",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('InspectedElementContext', () => {
9898
boolean_false: false,
9999
boolean_true: true,
100100
infinity: Infinity,
101+
minus_infinity: -Infinity,
101102
integer_zero: 0,
102103
integer_one: 1,
103104
float: 1.23,
@@ -128,6 +129,7 @@ describe('InspectedElementContext', () => {
128129
"infinity": Infinity,
129130
"integer_one": 1,
130131
"integer_zero": 0,
132+
"minus_infinity": -Infinity,
131133
"nan": NaN,
132134
"string": "abc",
133135
"string_empty": "",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ export function smartParse(value: any): any | void | number {
235235
switch (value) {
236236
case 'Infinity':
237237
return Infinity;
238+
case '-Infinity':
239+
return -Infinity;
238240
case 'NaN':
239241
return NaN;
240242
case 'undefined':
@@ -249,7 +251,7 @@ export function smartStringify(value: any): string {
249251
if (Number.isNaN(value)) {
250252
return 'NaN';
251253
} else if (!Number.isFinite(value)) {
252-
return 'Infinity';
254+
return value > 0 ? 'Infinity' : '-Infinity';
253255
}
254256
} else if (value === undefined) {
255257
return 'undefined';

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ export function dehydrate(
596596
return value;
597597
}
598598
case 'infinity':
599+
case '-infinity':
599600
case 'nan':
600601
case 'undefined':
601602
// Some values are lossy when sent through a WebSocket.
@@ -704,6 +705,8 @@ export function hydrate(
704705
return;
705706
} else if (value.type === 'infinity') {
706707
parent[last] = Infinity;
708+
} else if (value.type === '-infinity') {
709+
parent[last] = -Infinity;
707710
} else if (value.type === 'nan') {
708711
parent[last] = NaN;
709712
} else if (value.type === 'undefined') {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ export type DataType =
708708
| 'html_all_collection'
709709
| 'html_element'
710710
| 'infinity'
711+
| '-infinity'
711712
| 'iterator'
712713
| 'opaque_iterator'
713714
| 'nan'
@@ -765,7 +766,7 @@ export function getDataType(data: Object): DataType {
765766
if (Number.isNaN(data)) {
766767
return 'nan';
767768
} else if (!Number.isFinite(data)) {
768-
return 'infinity';
769+
return data > 0 ? 'infinity' : '-infinity';
769770
} else {
770771
return 'number';
771772
}
@@ -1219,6 +1220,7 @@ export function formatDataForPreview(
12191220
case 'boolean':
12201221
case 'number':
12211222
case 'infinity':
1223+
case '-infinity':
12221224
case 'nan':
12231225
case 'null':
12241226
case 'undefined':

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class SimpleValues extends Component {
2525
null={null}
2626
nan={NaN}
2727
infinity={Infinity}
28+
minusInfinity={-Infinity}
2829
true={true}
2930
false={false}
3031
function={noop}

0 commit comments

Comments
 (0)