diff --git a/src/core/observer/watcher.js b/src/core/observer/watcher.js index 3c4e533c08b..ab0c040d554 100644 --- a/src/core/observer/watcher.js +++ b/src/core/observer/watcher.js @@ -78,15 +78,28 @@ export default class Watcher { if (typeof expOrFn === 'function') { this.getter = expOrFn } else { - this.getter = parsePath(expOrFn) - if (!this.getter) { + const bailRE = /[^\w.$]/ + const delimiterRE = /[ ,/\\]/ + if (bailRE.test(expOrFn)) { + if (process.env.NODE_ENV !== 'production') { + if (delimiterRE.test(expOrFn)) { + warn( + `Failed watching path: "${expOrFn}" ` + + 'Watcher only accepts simple dot-delimited paths. ' + + 'For full control, use a function instead.', + vm + ) + } else { + warn( + `Failed watching path: "${expOrFn}" ` + + 'Vue watchers only accept alphanumeric, $, or underscore naming of properties.', + vm + ) + } + } this.getter = function () {} - process.env.NODE_ENV !== 'production' && warn( - `Failed watching path: "${expOrFn}" ` + - 'Watcher only accepts simple dot-delimited paths. ' + - 'For full control, use a function instead.', - vm - ) + } else { + this.getter = parsePath(expOrFn) } } this.value = this.lazy diff --git a/src/core/util/lang.js b/src/core/util/lang.js index 96b8219a045..783a412545a 100644 --- a/src/core/util/lang.js +++ b/src/core/util/lang.js @@ -23,11 +23,7 @@ export function def (obj: Object, key: string, val: any, enumerable?: boolean) { /** * Parse simple path. */ -const bailRE = /[^\w.$]/ export function parsePath (path: string): any { - if (bailRE.test(path)) { - return - } const segments = path.split('.') return function (obj) { for (let i = 0; i < segments.length; i++) { diff --git a/test/unit/modules/observer/watcher.spec.js b/test/unit/modules/observer/watcher.spec.js index 724a3cc8637..323bc563e71 100644 --- a/test/unit/modules/observer/watcher.spec.js +++ b/test/unit/modules/observer/watcher.spec.js @@ -178,4 +178,9 @@ describe('Watcher', () => { new Watcher(vm, 'd.e + c', spy) expect('Failed watching path:').toHaveBeenWarned() }) + + it('does not support non-alphanumeric paths and throws a warning', () => { + new Watcher(vm, '中文', spy) + expect('Failed watching path:').toHaveBeenWarned() + }) })