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

fix(render): Prevent error when displaying symbol keys on a map #9731

Merged
merged 3 commits into from
Dec 7, 2023
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
22 changes: 22 additions & 0 deletions packages/shared/__tests__/toDisplayString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,26 @@ describe('toDisplayString', () => {
}"
`)
})

//#9727
test('Map with Symbol keys', () => {
const m = new Map<any, any>([
[Symbol(), 'foo'],
[Symbol(), 'bar'],
[Symbol('baz'), 'baz']
])
expect(toDisplayString(m)).toMatchInlineSnapshot(`
"{
\\"Map(3)\\": {
\\"Symbol(0) =>\\": \\"foo\\",
\\"Symbol(1) =>\\": \\"bar\\",
\\"Symbol(baz) =>\\": \\"baz\\"
}
}"
`)
// confirming the symbol renders Symbol(foo)
expect(toDisplayString(new Map([[Symbol('foo'), 'foo']]))).toContain(
String(Symbol('foo'))
)
})
})
16 changes: 11 additions & 5 deletions packages/shared/src/toDisplayString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
isPlainObject,
isSet,
objectToString,
isString
isString,
isSymbol
} from './general'

/**
Expand All @@ -31,10 +32,15 @@ const replacer = (_key: string, val: any): any => {
return replacer(_key, val.value)
} else if (isMap(val)) {
return {
[`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val]) => {
;(entries as any)[`${key} =>`] = val
return entries
}, {})
[`Map(${val.size})`]: [...val.entries()].reduce(
(entries, [key, val], i) => {
entries[
`${isSymbol(key) ? `Symbol(${key.description ?? i})` : key} =>`
] = val
return entries
},
{} as Record<string, any>
)
}
} else if (isSet(val)) {
return {
Expand Down