]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(shared): handle more Symbol cases in toDisplayString
authorEvan You <yyx990803@gmail.com>
Thu, 7 Dec 2023 02:40:27 +0000 (10:40 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 7 Dec 2023 02:40:27 +0000 (10:40 +0800)
packages/shared/__tests__/toDisplayString.spec.ts
packages/shared/src/toDisplayString.ts

index 33c0af8df32498d4344d020d34f83744e3aebc0c..3b02911b8f0ef914e2faaa5c7596befa0525b429 100644 (file)
@@ -181,10 +181,10 @@ describe('toDisplayString', () => {
     ])
     expect(toDisplayString(m)).toMatchInlineSnapshot(`
       "{
-        \\"Map(3)\\": {
-          \\"Symbol(0) =>\\": \\"foo\\",
-          \\"Symbol(1) =>\\": \\"bar\\",
-          \\"Symbol(baz) =>\\": \\"baz\\"
+        "Map(3)": {
+          "Symbol(0) =>": "foo",
+          "Symbol(1) =>": "bar",
+          "Symbol(baz) =>": "baz"
         }
       }"
     `)
@@ -193,4 +193,27 @@ describe('toDisplayString', () => {
       String(Symbol('foo'))
     )
   })
+
+  test('Set with Symbol values', () => {
+    const s = new Set([Symbol('foo'), Symbol('bar'), Symbol()])
+    expect(toDisplayString(s)).toMatchInlineSnapshot(`
+      "{
+        "Set(3)": [
+          "Symbol(foo)",
+          "Symbol(bar)",
+          "Symbol()"
+        ]
+      }"
+    `)
+  })
+
+  test('Object with Symbol values', () => {
+    expect(toDisplayString({ foo: Symbol('x'), bar: Symbol() }))
+      .toMatchInlineSnapshot(`
+      "{
+        "foo": "Symbol(x)",
+        "bar": "Symbol()"
+      }"
+    `)
+  })
 })
index 83f340e9257d5d11c2ddf019ecb803d66222e6f4..c002287c839b0e6094c511546c82516e2baaafa4 100644 (file)
@@ -34,9 +34,7 @@ const replacer = (_key: string, val: any): any => {
     return {
       [`Map(${val.size})`]: [...val.entries()].reduce(
         (entries, [key, val], i) => {
-          entries[
-            `${isSymbol(key) ? `Symbol(${key.description ?? i})` : key} =>`
-          ] = val
+          entries[stringiySymbol(key, i) + ' =>'] = val
           return entries
         },
         {} as Record<string, any>
@@ -44,10 +42,16 @@ const replacer = (_key: string, val: any): any => {
     }
   } else if (isSet(val)) {
     return {
-      [`Set(${val.size})`]: [...val.values()]
+      [`Set(${val.size})`]: [...val.values()].map(v => stringiySymbol(v))
     }
+  } else if (isSymbol(val)) {
+    return stringiySymbol(val)
   } else if (isObject(val) && !isArray(val) && !isPlainObject(val)) {
+    // native elements
     return String(val)
   }
   return val
 }
+
+const stringiySymbol = (v: unknown, i: number | string = ''): any =>
+  isSymbol(v) ? `Symbol(${v.description ?? i})` : v