Skip to content

fix #8862: cannot watch unicode properties like 'a.中文' #8925

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
20 changes: 14 additions & 6 deletions src/core/observer/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ export default class Watcher {
} else {
this.getter = parsePath(expOrFn)
if (!this.getter) {
if (this.getter === false) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't like that we are relying on magic return values (false) here, as the logic is separated in two places. Also the check is warning only so it's better to place it right here inside a if (process.env.NODE_ENV !== 'production') { ... } block, using an inline regexp.

Copy link
Author

Choose a reason for hiding this comment

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

@yyx990803 Thanks for taking the time to review this PR.

I've gone ahead and made the commits to stop relying on random return values. I also tried your suggestion with having the check in an if (process.env.NODE_ENV !== 'production') { ... } block, but felt that it was unclear to have two regex checks in different places (watcher and parsePath) and did not resolve your comment on the logic being separated, so I grouped them together.

Thanks again and please let me know what you think!

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
Expand Down
4 changes: 4 additions & 0 deletions src/core/util/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')
Expand Down
5 changes: 5 additions & 0 deletions test/unit/modules/observer/watcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})