소스 검색

fix error detection on string interpolations (fix #3137)

Evan You 10 년 전
부모
커밋
de0b4b87bc
1개의 변경된 파일12개의 추가작업 그리고 15개의 파일을 삭제
  1. 12 15
      src/compiler/error-detector.js

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

@@ -10,6 +10,8 @@ const prohibitedKeywordRE = new RegExp('\\b' + (
 ).split(',').join('\\b|\\b') + '\\b')
 // check valid identifier for v-for
 const identRE = /[A-Za-z_$][\w$]*/
+// strip strings in expressions
+const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
 
 // detect problematic expressions in a template
 export function detectErrors (ast: ?ASTNode): Array<string> {
@@ -58,22 +60,17 @@ function checkIdentifier (ident: ?string, type: string, text: string, errors: Ar
 }
 
 function checkExpression (exp: string, text: string, errors: Array<string>) {
-  exp = stripToString(exp)
-  const keywordMatch = exp.match(prohibitedKeywordRE)
-  if (keywordMatch) {
-    errors.push(
-      `- avoid using JavaScript keyword as property name: ` +
-      `"${keywordMatch[0]}" in expression ${text}`
-    )
-  } else {
-    try {
-      new Function(`return ${exp}`)
-    } catch (e) {
+  try {
+    new Function(`return ${exp}`)
+  } catch (e) {
+    const keywordMatch = exp.replace(stripStringRE, '').match(prohibitedKeywordRE)
+    if (keywordMatch) {
+      errors.push(
+        `- avoid using JavaScript keyword as property name: ` +
+        `"${keywordMatch[0]}" in expression ${text}`
+      )
+    } else {
       errors.push(`- invalid expression: ${text}`)
     }
   }
 }
-
-function stripToString (exp) {
-  return exp.replace(/^_s\((.*)\)$/, '$1')
-}