Skip to content
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

fix(core): Give warning for set/delete methods on null, undefined, String, Number and Boolean values. Fixes #7381 #7452

Merged
merged 5 commits into from Mar 8, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/core/observer/index.js
Expand Up @@ -194,6 +194,12 @@ export function defineReactive (
* already exist.
*/
export function set (target: Array<any> | Object, key: any, val: any): any {
if (process.env.NODE_ENV !== 'production' &&
!Array.isArray(target) &&
!isObject(target)
) {
warn(`Cannot set reactive property on non-object/array value: ${target}`)
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.length = Math.max(target.length, key)
target.splice(key, 1, val)
Expand Down Expand Up @@ -224,6 +230,12 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
* Delete a property and trigger change if necessary.
*/
export function del (target: Array<any> | Object, key: any) {
if (process.env.NODE_ENV !== 'production' &&
!Array.isArray(target) &&
!isObject(target)
) {
warn(`Cannot delete reactive property on non-object/array value: ${target}`)
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.splice(key, 1)
return
Expand Down
12 changes: 12 additions & 0 deletions test/unit/modules/observer/observer.spec.js
Expand Up @@ -356,6 +356,18 @@ describe('Observer', () => {
})
})

it('warn set/delete on non valid values', () => {
try {
setProp(null, 'foo', 1)
} catch (e) {}
expect(`Cannot set reactive property on non-object/array value`).toHaveBeenWarned()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late to the party, but why use ES6 template literals here (and the next expect())?


try {
delProp(null, 'foo')
} catch (e) {}
expect(`Cannot delete reactive property on non-object/array value`).toHaveBeenWarned()
})

it('should lazy invoke existing getters', () => {
const obj = {}
let called = false
Expand Down