Skip to content

Commit

Permalink
fix(shallowReadonly): align behavior with vue-next (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jul 2, 2021
1 parent efb4195 commit 14d1c7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/reactivity/readonly.ts
@@ -1,5 +1,5 @@
import { reactive, Ref, UnwrapRef } from '.'
import { isArray, isPlainObject, warn } from '../utils'
import { isArray, isPlainObject, isObject, warn } from '../utils'
import { readonlySet } from '../utils/sets'
import { isReactive, observe } from './reactive'
import { isRef, RefImpl } from './ref'
Expand Down Expand Up @@ -49,6 +49,13 @@ export function readonly<T extends object>(

export function shallowReadonly<T extends object>(obj: T): Readonly<T>
export function shallowReadonly(obj: any): any {
if (!isObject(obj)) {
if (__DEV__) {
warn(`value cannot be made reactive: ${String(obj)}`)
}
return obj
}

if (
!(isPlainObject(obj) || isArray(obj)) ||
(!Object.isExtensible(obj) && !isRef(obj))
Expand Down
6 changes: 6 additions & 0 deletions test/v3/reactivity/readonly.spec.ts
Expand Up @@ -382,6 +382,12 @@ describe('reactivity/readonly', () => {
).not.toHaveBeenWarned()
})

test('should not process non-object data', () => {
// @ts-ignore
shallowReadonly(25)
expect(`value cannot be made reactive: 25`).toHaveBeenWarned()
})

// #669
test('shallowReadonly should work for refs', () => {
const vm = new Vue({
Expand Down

0 comments on commit 14d1c7b

Please sign in to comment.