From ea06a70570a885bda5f6edc1c2129af953908431 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Fri, 1 Dec 2023 08:46:06 +0000 Subject: [PATCH 1/3] fix(render): Prevent error when displaying symbol keys on a map --- packages/shared/__tests__/toDisplayString.spec.ts | 12 ++++++++++++ packages/shared/src/toDisplayString.ts | 11 +++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/shared/__tests__/toDisplayString.spec.ts b/packages/shared/__tests__/toDisplayString.spec.ts index 5255c0e400b..81bf7f0afc9 100644 --- a/packages/shared/__tests__/toDisplayString.spec.ts +++ b/packages/shared/__tests__/toDisplayString.spec.ts @@ -171,4 +171,16 @@ describe('toDisplayString', () => { }" `) }) + + //#9727 + test('Map with Symbol keys', () => { + const m = new Map([[Symbol(), 'foo']]) + expect(toDisplayString(m)).toMatchInlineSnapshot(` + "{ + \\"Map(1)\\": { + \\"Symbol() =>\\": \\"foo\\" + } + }" + `) + }) }) diff --git a/packages/shared/src/toDisplayString.ts b/packages/shared/src/toDisplayString.ts index d7a96c06c1e..1a1f7256b09 100644 --- a/packages/shared/src/toDisplayString.ts +++ b/packages/shared/src/toDisplayString.ts @@ -31,10 +31,13 @@ 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]) => { + entries[`${String(key)} =>`] = val + return entries + }, + {} as Record + ) } } else if (isSet(val)) { return { From b49a28c32be9cdc6a51c08ed72565d0812d0d6ec Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Fri, 1 Dec 2023 08:55:50 +0000 Subject: [PATCH 2/3] chore: add index to symbol, to differentiate --- packages/shared/__tests__/toDisplayString.spec.ts | 10 +++++++--- packages/shared/src/toDisplayString.ts | 7 ++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/shared/__tests__/toDisplayString.spec.ts b/packages/shared/__tests__/toDisplayString.spec.ts index 81bf7f0afc9..56c00c4387e 100644 --- a/packages/shared/__tests__/toDisplayString.spec.ts +++ b/packages/shared/__tests__/toDisplayString.spec.ts @@ -174,11 +174,15 @@ describe('toDisplayString', () => { //#9727 test('Map with Symbol keys', () => { - const m = new Map([[Symbol(), 'foo']]) + const m = new Map([ + [Symbol(), 'foo'], + [Symbol(), 'bar'] + ]) expect(toDisplayString(m)).toMatchInlineSnapshot(` "{ - \\"Map(1)\\": { - \\"Symbol() =>\\": \\"foo\\" + \\"Map(2)\\": { + \\"Symbol(0) =>\\": \\"foo\\", + \\"Symbol(1) =>\\": \\"bar\\" } }" `) diff --git a/packages/shared/src/toDisplayString.ts b/packages/shared/src/toDisplayString.ts index 1a1f7256b09..69de72492cd 100644 --- a/packages/shared/src/toDisplayString.ts +++ b/packages/shared/src/toDisplayString.ts @@ -6,7 +6,8 @@ import { isPlainObject, isSet, objectToString, - isString + isString, + isSymbol } from './general' /** @@ -32,8 +33,8 @@ const replacer = (_key: string, val: any): any => { } else if (isMap(val)) { return { [`Map(${val.size})`]: [...val.entries()].reduce( - (entries, [key, val]) => { - entries[`${String(key)} =>`] = val + (entries, [key, val], i) => { + entries[`${isSymbol(key) ? `Symbol(${i})` : key} =>`] = val return entries }, {} as Record From 6dc2b09bc15f299c2737a4277f400c5f320dea0e Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Sat, 2 Dec 2023 08:55:26 +0000 Subject: [PATCH 3/3] chore: handle description --- packages/shared/__tests__/toDisplayString.spec.ts | 12 +++++++++--- packages/shared/src/toDisplayString.ts | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/shared/__tests__/toDisplayString.spec.ts b/packages/shared/__tests__/toDisplayString.spec.ts index 56c00c4387e..4edbf2361ec 100644 --- a/packages/shared/__tests__/toDisplayString.spec.ts +++ b/packages/shared/__tests__/toDisplayString.spec.ts @@ -176,15 +176,21 @@ describe('toDisplayString', () => { test('Map with Symbol keys', () => { const m = new Map([ [Symbol(), 'foo'], - [Symbol(), 'bar'] + [Symbol(), 'bar'], + [Symbol('baz'), 'baz'] ]) expect(toDisplayString(m)).toMatchInlineSnapshot(` "{ - \\"Map(2)\\": { + \\"Map(3)\\": { \\"Symbol(0) =>\\": \\"foo\\", - \\"Symbol(1) =>\\": \\"bar\\" + \\"Symbol(1) =>\\": \\"bar\\", + \\"Symbol(baz) =>\\": \\"baz\\" } }" `) + // confirming the symbol renders Symbol(foo) + expect(toDisplayString(new Map([[Symbol('foo'), 'foo']]))).toContain( + String(Symbol('foo')) + ) }) }) diff --git a/packages/shared/src/toDisplayString.ts b/packages/shared/src/toDisplayString.ts index 69de72492cd..83f340e9257 100644 --- a/packages/shared/src/toDisplayString.ts +++ b/packages/shared/src/toDisplayString.ts @@ -34,7 +34,9 @@ const replacer = (_key: string, val: any): any => { return { [`Map(${val.size})`]: [...val.entries()].reduce( (entries, [key, val], i) => { - entries[`${isSymbol(key) ? `Symbol(${i})` : key} =>`] = val + entries[ + `${isSymbol(key) ? `Symbol(${key.description ?? i})` : key} =>` + ] = val return entries }, {} as Record