fix(reactivity): mutating a readonly ref nested in a reactive object should fail. (#5048)
fix: #5042
This commit is contained in:
		
							parent
							
								
									72130ac7b5
								
							
						
					
					
						commit
						171f5e9c60
					
				@ -474,4 +474,15 @@ describe('reactivity/readonly', () => {
 | 
				
			|||||||
    expect(rr.foo).toBe(r)
 | 
					    expect(rr.foo).toBe(r)
 | 
				
			||||||
    expect(isReadonly(rr.foo)).toBe(true)
 | 
					    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
 | 
					    receiver: object
 | 
				
			||||||
  ): boolean {
 | 
					  ): boolean {
 | 
				
			||||||
    let oldValue = (target as any)[key]
 | 
					    let oldValue = (target as any)[key]
 | 
				
			||||||
 | 
					    if (isReadonly(oldValue) && isRef(oldValue)) {
 | 
				
			||||||
 | 
					      return false
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (!shallow && !isReadonly(value)) {
 | 
					    if (!shallow && !isReadonly(value)) {
 | 
				
			||||||
      if (!isShallow(value)) {
 | 
					      if (!isShallow(value)) {
 | 
				
			||||||
        value = toRaw(value)
 | 
					        value = toRaw(value)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user