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.

fix: #5042
  • Loading branch information
LinusBorg committed Dec 5, 2021
1 parent 2d4f455 commit 9797485
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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)) {
value = toRaw(value)
oldValue = toRaw(oldValue)
Expand Down

0 comments on commit 9797485

Please sign in to comment.