fix(reactivity): mutating a readonly ref nested in a reactive object should fail. (#5048)

fix: #5042
This commit is contained in:
Thorsten Lünborg 2022-01-21 08:33:18 +01:00 committed by GitHub
parent 72130ac7b5
commit 171f5e9c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -474,4 +474,15 @@ describe('reactivity/readonly', () => {
expect(rr.foo).toBe(r)
expect(isReadonly(rr.foo)).toBe(true)
})
test('attemptingt to write to a readonly ref nested in a reactive object should fail', () => {
const r = ref(false)
const ror = readonly(r)
const obj = reactive({ ror })
try {
obj.ror = true
} catch (e) {}
expect(obj.ror).toBe(false)
})
})

View File

@ -150,6 +150,9 @@ function createSetter(shallow = false) {
receiver: object
): boolean {
let oldValue = (target as any)[key]
if (isReadonly(oldValue) && isRef(oldValue)) {
return false
}
if (!shallow && !isReadonly(value)) {
if (!isShallow(value)) {
value = toRaw(value)