Skip to content

Commit

Permalink
fix(reactivity): mutating a readonly ref nested in a reactive object …
Browse files Browse the repository at this point in the history
…should fail. (#5048)

fix: #5042
  • Loading branch information
LinusBorg committed Jan 21, 2022
1 parent 72130ac commit 171f5e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/reactivity/__tests__/readonly.spec.ts
Expand Up @@ -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)
})
})
3 changes: 3 additions & 0 deletions packages/reactivity/src/baseHandlers.ts
Expand Up @@ -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)
Expand Down

0 comments on commit 171f5e9

Please sign in to comment.