]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(shared): handle Map with symbol keys in toDisplayString (#9731)
authorCarlos Rodrigues <carlos@hypermob.co.uk>
Thu, 7 Dec 2023 02:33:48 +0000 (02:33 +0000)
committerGitHub <noreply@github.com>
Thu, 7 Dec 2023 02:33:48 +0000 (10:33 +0800)
close #9727

packages/shared/__tests__/toDisplayString.spec.ts
packages/shared/src/toDisplayString.ts

index 4d28714252c737dc9eda582ba255cfee6b62e61d..33c0af8df32498d4344d020d34f83744e3aebc0c 100644 (file)
@@ -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'))
+    )
+  })
 })
index d7a96c06c1e4de6b7a4b547e6684a9001418908a..83f340e9257d5d11c2ddf019ecb803d66222e6f4 100644 (file)
@@ -6,7 +6,8 @@ import {
   isPlainObject,
   isSet,
   objectToString,
-  isString
+  isString,
+  isSymbol
 } from './general'
 
 /**
@@ -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 {