From 810c4ea41b94637a24fcdb05b9e1992395c0ece1 Mon Sep 17 00:00:00 2001 From: derek-li Date: Tue, 9 Oct 2018 16:52:28 -0700 Subject: [PATCH 1/6] Update test/unit/modules/observer/watcher.spec.js --- test/unit/modules/observer/watcher.spec.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/unit/modules/observer/watcher.spec.js b/test/unit/modules/observer/watcher.spec.js index 724a3cc8637..90b815a4fa1 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 warns', () => { + new Watcher(vm, '中文', spy) + expect('Failed watching path:').toHaveBeenWarned() + }) }) From 68c8322d77972133f4f7e3666d8f2c97fecf82f0 Mon Sep 17 00:00:00 2001 From: derek-li Date: Tue, 9 Oct 2018 16:52:35 -0700 Subject: [PATCH 2/6] Update src/core/util/lang.js --- src/core/util/lang.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/util/lang.js b/src/core/util/lang.js index 96b8219a045..7eec1303faf 100644 --- a/src/core/util/lang.js +++ b/src/core/util/lang.js @@ -24,8 +24,12 @@ export function def (obj: Object, key: string, val: any, enumerable?: boolean) { * Parse simple path. */ const bailRE = /[^\w.$]/ +const delimiterRE = /[ ,/\\]/ export function parsePath (path: string): any { if (bailRE.test(path)) { + if (delimiterRE.test(path)) { + return false + } return } const segments = path.split('.') From 175cd0e0252b66995e5e8375e06dd659ca216117 Mon Sep 17 00:00:00 2001 From: derek-li Date: Tue, 9 Oct 2018 16:52:48 -0700 Subject: [PATCH 3/6] Update src/core/observer/watcher.js --- src/core/observer/watcher.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/core/observer/watcher.js b/src/core/observer/watcher.js index 3c4e533c08b..c78063f8db5 100644 --- a/src/core/observer/watcher.js +++ b/src/core/observer/watcher.js @@ -80,13 +80,21 @@ export default class Watcher { } else { this.getter = parsePath(expOrFn) if (!this.getter) { + if (this.getter === false) { + 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 { + process.env.NODE_ENV !== 'production' && 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 - ) } } this.value = this.lazy From fe763275b5f58bb21d40e7ad6cab7cccf1e61746 Mon Sep 17 00:00:00 2001 From: derek-li Date: Tue, 23 Oct 2018 12:14:15 -0700 Subject: [PATCH 4/6] test(watcher): fix typo 'warns' -> 'warning' --- test/unit/modules/observer/watcher.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/modules/observer/watcher.spec.js b/test/unit/modules/observer/watcher.spec.js index 90b815a4fa1..323bc563e71 100644 --- a/test/unit/modules/observer/watcher.spec.js +++ b/test/unit/modules/observer/watcher.spec.js @@ -179,7 +179,7 @@ describe('Watcher', () => { expect('Failed watching path:').toHaveBeenWarned() }) - it('does not support non-alphanumeric paths and throws a warns', () => { + it('does not support non-alphanumeric paths and throws a warning', () => { new Watcher(vm, '中文', spy) expect('Failed watching path:').toHaveBeenWarned() }) From 9deb10c6ff625d4ecaf93d74777a627b968bf2b8 Mon Sep 17 00:00:00 2001 From: derek-li Date: Tue, 23 Oct 2018 14:18:58 -0700 Subject: [PATCH 5/6] refactor(lang): regrouping regex --- src/core/util/lang.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/core/util/lang.js b/src/core/util/lang.js index 7eec1303faf..783a412545a 100644 --- a/src/core/util/lang.js +++ b/src/core/util/lang.js @@ -23,15 +23,7 @@ export function def (obj: Object, key: string, val: any, enumerable?: boolean) { /** * Parse simple path. */ -const bailRE = /[^\w.$]/ -const delimiterRE = /[ ,/\\]/ export function parsePath (path: string): any { - if (bailRE.test(path)) { - if (delimiterRE.test(path)) { - return false - } - return - } const segments = path.split('.') return function (obj) { for (let i = 0; i < segments.length; i++) { From 48ef25634379d4e024a79cc2e50c770451245065 Mon Sep 17 00:00:00 2001 From: derek-li Date: Tue, 23 Oct 2018 14:20:04 -0700 Subject: [PATCH 6/6] refactor(watcher): move regex logic from helper function --- src/core/observer/watcher.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/core/observer/watcher.js b/src/core/observer/watcher.js index c78063f8db5..ab0c040d554 100644 --- a/src/core/observer/watcher.js +++ b/src/core/observer/watcher.js @@ -78,23 +78,28 @@ export default class Watcher { if (typeof expOrFn === 'function') { this.getter = expOrFn } else { - this.getter = parsePath(expOrFn) - if (!this.getter) { - if (this.getter === false) { - 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 { - process.env.NODE_ENV !== 'production' && warn( - `Failed watching path: "${expOrFn}" ` + - 'Vue watchers only accept alphanumeric, $, or underscore naming of properties.', - vm - ) + 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 () {} + } else { + this.getter = parsePath(expOrFn) } } this.value = this.lazy