Skip to content

fix(shallowReadonly): align behavior with vue-next #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/reactivity/readonly.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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