]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): ensure readonly on plain arrays doesn't track array methods. (...
authorThorsten Lünborg <t.luenborg@googlemail.com>
Fri, 27 Nov 2020 15:24:31 +0000 (16:24 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Nov 2020 15:24:31 +0000 (10:24 -0500)
fix #2493

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

index ff73e43bcf79d500ff465b59ca37cbe0d4738953..b9edb440fb7728ba241cfa4bc92b3cbb194b95c2 100644 (file)
@@ -375,6 +375,16 @@ describe('reactivity/readonly', () => {
     expect(dummy).toBe(1)
   })
 
+  test('readonly array should not track', () => {
+    const arr = [1]
+    const roArr = readonly(arr)
+
+    const eff = effect(() => {
+      roArr.includes(2)
+    })
+    expect(eff.deps.length).toBe(0)
+  })
+
   test('readonly should track and trigger if wrapping reactive original (collection)', () => {
     const a = reactive(new Map())
     const b = readonly(a)
index a246b6f31943d18a8d0c3737c04886f5d85e6a98..5f57a3c9f9012d457703dcb4c016d85ec5c0a333 100644 (file)
@@ -83,7 +83,8 @@ function createGetter(isReadonly = false, shallow = false) {
     }
 
     const targetIsArray = isArray(target)
-    if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
+
+    if (!isReadonly && targetIsArray && hasOwn(arrayInstrumentations, key)) {
       return Reflect.get(arrayInstrumentations, key, receiver)
     }