Skip to content
Permalink
Browse files
fix(reactivity): mutating a readonly ref nested in a reactive object …
…should fail. (#5048)

fix: #5042
  • Loading branch information
LinusBorg committed Jan 21, 2022
1 parent 72130ac commit 171f5e9c6008bf0dff565991f34b214da016dde0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
@@ -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)
})
})
@@ -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)

0 comments on commit 171f5e9

Please sign in to comment.