Skip to content

Commit 2bc7c18

Browse files
pkaminskiaJean
authored andcommitted
fix(core): Make set/delete warning condition for undefined, null and (vuejs#7818)
primitive values more precise. Corrects vuejs#7452
1 parent 8196a00 commit 2bc7c18

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/core/observer/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
hasProto,
1111
isObject,
1212
isPlainObject,
13+
isPrimitive,
14+
isUndef,
1315
isValidArrayIndex,
1416
isServerRendering
1517
} from '../util/index'
@@ -195,10 +197,9 @@ export function defineReactive (
195197
*/
196198
export function set (target: Array<any> | Object, key: any, val: any): any {
197199
if (process.env.NODE_ENV !== 'production' &&
198-
!Array.isArray(target) &&
199-
!isObject(target)
200+
(isUndef(target) || isPrimitive(target))
200201
) {
201-
warn(`Cannot set reactive property on non-object/array value: ${target}`)
202+
warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`)
202203
}
203204
if (Array.isArray(target) && isValidArrayIndex(key)) {
204205
target.length = Math.max(target.length, key)
@@ -231,10 +232,9 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
231232
*/
232233
export function del (target: Array<any> | Object, key: any) {
233234
if (process.env.NODE_ENV !== 'production' &&
234-
!Array.isArray(target) &&
235-
!isObject(target)
235+
(isUndef(target) || isPrimitive(target))
236236
) {
237-
warn(`Cannot delete reactive property on non-object/array value: ${target}`)
237+
warn(`Cannot delete reactive property on undefined, null, or primitive value: ${(target: any)}`)
238238
}
239239
if (Array.isArray(target) && isValidArrayIndex(key)) {
240240
target.splice(key, 1)

test/unit/modules/observer/observer.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,12 @@ describe('Observer', () => {
360360
try {
361361
setProp(null, 'foo', 1)
362362
} catch (e) {}
363-
expect(`Cannot set reactive property on non-object/array value`).toHaveBeenWarned()
363+
expect(`Cannot set reactive property on undefined, null, or primitive value`).toHaveBeenWarned()
364364

365365
try {
366366
delProp(null, 'foo')
367367
} catch (e) {}
368-
expect(`Cannot delete reactive property on non-object/array value`).toHaveBeenWarned()
368+
expect(`Cannot delete reactive property on undefined, null, or primitive value`).toHaveBeenWarned()
369369
})
370370

371371
it('should lazy invoke existing getters', () => {

0 commit comments

Comments
 (0)