Explorar el Código

improve expression parser literal check

Evan You hace 10 años
padre
commit
4d4059b48f
Se han modificado 2 ficheros con 9 adiciones y 3 borrados
  1. 3 3
      src/parsers/expression.js
  2. 6 0
      test/unit/specs/parsers/expression_spec.js

+ 3 - 3
src/parsers/expression.js

@@ -27,7 +27,7 @@ const saveRE = /[\{,]\s*[\w\$_]+\s*:|('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^
 const restoreRE = /"(\d+)"/g
 const restoreRE = /"(\d+)"/g
 const pathTestRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/
 const pathTestRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/
 const identRE = /[^\w$\.](?:[A-Za-z_$][\w$]*)/g
 const identRE = /[^\w$\.](?:[A-Za-z_$][\w$]*)/g
-const booleanLiteralRE = /^(?:true|false)$/
+const literalValueRE = /^(?:true|false|null|undefined|Infinity|NaN)$/
 
 
 function noop () {}
 function noop () {}
 
 
@@ -222,8 +222,8 @@ export function parseExpression (exp, needSet) {
 
 
 export function isSimplePath (exp) {
 export function isSimplePath (exp) {
   return pathTestRE.test(exp) &&
   return pathTestRE.test(exp) &&
-    // don't treat true/false as paths
-    !booleanLiteralRE.test(exp) &&
+    // don't treat literal values as paths
+    !literalValueRE.test(exp) &&
     // Math constants e.g. Math.PI, Math.E etc.
     // Math constants e.g. Math.PI, Math.E etc.
     exp.slice(0, 5) !== 'Math.'
     exp.slice(0, 5) !== 'Math.'
 }
 }

+ 6 - 0
test/unit/specs/parsers/expression_spec.js

@@ -224,6 +224,12 @@ var testCases = [
     expected: null,
     expected: null,
     paths: []
     paths: []
   },
   },
+  {
+    exp: 'undefined',
+    scope: { undefined: 1 },
+    expected: undefined,
+    paths: []
+  },
   {
   {
     // Date global
     // Date global
     exp: 'Date.now() > new Date("2000-01-01")',
     exp: 'Date.now() > new Date("2000-01-01")',