Skip to content

Commit 8886a73

Browse files
fix(client): resolve nested custom object (#167)
1 parent d3225fb commit 8886a73

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as format from '../format'
2+
import { INFINITY, NAN, NEGATIVE_INFINITY, UNDEFINED } from '../constants'
3+
4+
describe('format: displayText and rawValue can be calculated by formatInspectorStateValue, getRawValue', () => {
5+
describe('type: literals', () => {
6+
// eslint-disable-next-line test/consistent-test-it
7+
test.each([
8+
{ literal: 'test-string', displayText: 'test-string' },
9+
{ literal: 123, displayText: 123 },
10+
{ literal: true, displayText: true },
11+
{ literal: null, displayText: 'null' },
12+
// Tokenlized values
13+
{ literal: INFINITY, displayText: 'Infinity' },
14+
{ literal: NAN, displayText: 'NaN' },
15+
{ literal: NEGATIVE_INFINITY, displayText: '-Infinity' },
16+
{ literal: UNDEFINED, displayText: 'undefined' },
17+
])('type: %s', (value) => {
18+
const displayText = format.formatInspectorStateValue(value.literal)
19+
const rawValue = format.getRawValue(value.literal).value
20+
21+
expect(displayText).toBe(value.displayText)
22+
expect(rawValue).toBe(value.literal)
23+
})
24+
})
25+
26+
it('type: plain object', () => {
27+
const value = { foo: 'bar' }
28+
const displayText = format.formatInspectorStateValue(value)
29+
const rawValue = format.getRawValue(value).value
30+
31+
expect(displayText).toBe('Object')
32+
expect(rawValue).toEqual(value)
33+
})
34+
35+
it('type: array', () => {
36+
const value = ['foo', { bar: 'baz' }]
37+
const displayText = format.formatInspectorStateValue(value)
38+
const rawValue = format.getRawValue(value).value
39+
40+
expect(displayText).toBe('Array[2]')
41+
expect(rawValue).toEqual(value)
42+
})
43+
44+
describe('type: custom', () => {
45+
it('type: common custom', () => {
46+
const value = { _custom: { displayText: 'custom-display', value: Symbol(123) } }
47+
const displayText = format.formatInspectorStateValue(value)
48+
const rawValue = format.getRawValue(value).value
49+
50+
expect(displayText).toBe(value._custom.displayText)
51+
expect(rawValue).toEqual(value._custom.value)
52+
})
53+
54+
it('type: nested custom', () => {
55+
const value = {
56+
_custom: {
57+
displayText: 'custom-display',
58+
value: {
59+
_custom: { displayText: 'nested-custom-display', value: Symbol(123) },
60+
},
61+
},
62+
}
63+
64+
const displayText = format.formatInspectorStateValue(value)
65+
const rawValue = format.getRawValue(value).value
66+
67+
expect(displayText).toBe(value._custom.value._custom.displayText)
68+
expect(rawValue).toEqual(value._custom.value._custom.value)
69+
})
70+
})
71+
})

packages/devtools-kit/src/core/component/state/format.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ export function formatInspectorStateValue(value, quotes = false) {
5656
return result
5757
}
5858
else if (type === 'custom') {
59-
return value._custom.displayText ?? value._custom.display
59+
// For digging out nested custom name.
60+
const nestedName = value._custom.value?._custom && formatInspectorStateValue(value._custom.value)
61+
return nestedName || value._custom.displayText || value._custom.display
6062
}
6163
else if (type === 'array') {
6264
return `Array[${value.length}]`
@@ -89,8 +91,11 @@ export function getRawValue(value: InspectorState['value']) {
8991
let inherit = {}
9092
if (isCustom) {
9193
const data = value as InspectorCustomState
92-
inherit = data._custom?.fields || {}
93-
value = data._custom?.value as string
94+
const nestedCustom = typeof data._custom?.value === 'object' && '_custom' in data._custom.value
95+
? getRawValue(data._custom?.value)
96+
: { inherit: undefined, value: undefined }
97+
inherit = nestedCustom.inherit || data._custom?.fields || {}
98+
value = nestedCustom.value || data._custom?.value as string
9499
}
95100
// @ts-expect-error @TODO: type
96101
if (value && value._isArray)

packages/devtools-kit/src/core/component/types/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface InspectorCustomState {
55
type?: string
66
displayText?: string
77
tooltipText?: string
8-
value?: string
8+
value?: string | InspectorCustomState
99
stateTypeName?: string
1010
fields?: {
1111
abstract?: boolean
@@ -15,7 +15,7 @@ export interface InspectorCustomState {
1515

1616
export interface InspectorState {
1717
key: string
18-
value: string | number | Record<string, unknown> | InspectorCustomState | Array<unknown>
18+
value: string | number | boolean | null | Record<string, unknown> | InspectorCustomState | Array<unknown>
1919
type: string
2020
stateType?: string
2121
stateTypeName?: string

0 commit comments

Comments
 (0)