From 34703082fd5afacee774ee92c11753119032ed1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20L=C3=BCnborg?= Date: Fri, 27 Nov 2020 16:24:31 +0100 Subject: [PATCH] fix(reactivity): ensure readonly on plain arrays doesn't track array methods. (#2506) fix #2493 --- packages/reactivity/__tests__/readonly.spec.ts | 10 ++++++++++ packages/reactivity/src/baseHandlers.ts | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/reactivity/__tests__/readonly.spec.ts b/packages/reactivity/__tests__/readonly.spec.ts index ff73e43b..b9edb440 100644 --- a/packages/reactivity/__tests__/readonly.spec.ts +++ b/packages/reactivity/__tests__/readonly.spec.ts @@ -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) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index a246b6f3..5f57a3c9 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -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) }