]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): unwrap non-index accessed refs on reactive arrays (#1859)
authorᴜɴвʏтᴇ <i@shangyes.net>
Fri, 21 Aug 2020 17:36:41 +0000 (01:36 +0800)
committerGitHub <noreply@github.com>
Fri, 21 Aug 2020 17:36:41 +0000 (13:36 -0400)
close #1846

packages/reactivity/__tests__/ref.spec.ts
packages/reactivity/src/baseHandlers.ts

index bae4ec35a3192d080e5958bf15540e3942fdfe80..9ef6c4e4b1589736d34931231b859b15079648cc 100644 (file)
@@ -115,6 +115,19 @@ describe('reactivity/ref', () => {
     expect((arr[1] as Ref).value).toBe(3)
   })
 
+  it('should unwrap ref types as props of arrays', () => {
+    const arr = [ref(0)]
+    const symbolKey = Symbol('')
+    arr['' as any] = ref(1)
+    arr[symbolKey as any] = ref(2)
+    const arrRef = ref(arr).value
+    expect(isRef(arrRef[0])).toBe(true)
+    expect(isRef(arrRef['' as any])).toBe(false)
+    expect(isRef(arrRef[symbolKey as any])).toBe(false)
+    expect(arrRef['' as any]).toBe(1)
+    expect(arrRef[symbolKey as any]).toBe(2)
+  })
+
   it('should keep tuple types', () => {
     const tuple: [number, string, { a: number }, () => number, Ref<number>] = [
       0,
index 11a7b1cc04e372fbadb7add00cc345c91669d5ab..8d8112e926ad4410be5cbf81009e40a035c5666e 100644 (file)
@@ -63,9 +63,10 @@ function createGetter(isReadonly = false, shallow = false) {
 
     const res = Reflect.get(target, key, receiver)
 
+    const keyIsSymbol = isSymbol(key)
     if (
-      isSymbol(key)
-        ? builtInSymbols.has(key)
+      keyIsSymbol
+        ? builtInSymbols.has(key as symbol)
         : key === `__proto__` || key === `__v_isRef`
     ) {
       return res
@@ -80,8 +81,12 @@ function createGetter(isReadonly = false, shallow = false) {
     }
 
     if (isRef(res)) {
-      // ref unwrapping, only for Objects, not for Arrays.
-      return targetIsArray ? res : res.value
+      // ref unwrapping - does not apply for Array + integer key.
+      const shouldUnwrap =
+        !targetIsArray ||
+        keyIsSymbol ||
+        '' + parseInt(key as string, 10) !== key
+      return shouldUnwrap ? res.value : res
     }
 
     if (isObject(res)) {