Просмотр исходного кода

fix: improve error detector v-for identifier check

close #6971
Evan You 8 лет назад
Родитель
Сommit
d891cd1761
1 измененных файлов с 12 добавлено и 6 удалено
  1. 12 6
      src/compiler/error-detector.js

+ 12 - 6
src/compiler/error-detector.js

@@ -15,9 +15,6 @@ const unaryOperatorsRE = new RegExp('\\b' + (
   'delete,typeof,void'
 ).split(',').join('\\s*\\([^\\)]*\\)|\\b') + '\\s*\\([^\\)]*\\)')
 
-// check valid identifier for v-for
-const identRE = /[A-Za-z_$][\w$]*/
-
 // strip strings in expressions
 const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
 
@@ -75,9 +72,18 @@ function checkFor (node: ASTElement, text: string, errors: Array<string>) {
   checkIdentifier(node.iterator2, 'v-for iterator', text, errors)
 }
 
-function checkIdentifier (ident: ?string, type: string, text: string, errors: Array<string>) {
-  if (typeof ident === 'string' && !identRE.test(ident)) {
-    errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
+function checkIdentifier (
+  ident: ?string,
+  type: string,
+  text: string,
+  errors: Array<string>
+) {
+  if (typeof ident === 'string') {
+    try {
+      new Function(`var ${ident}`)
+    } catch (e) {
+      errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
+    }
   }
 }