Przeglądaj źródła

recognize boolean literals in expression parser, fixes #715

Evan You 11 lat temu
rodzic
commit
9252b6ec35

+ 9 - 4
src/parsers/expression.js

@@ -21,6 +21,7 @@ var restoreRE = /"(\d+)"/g
 var pathTestRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\])*$/
 var pathReplaceRE = /[^\w$\.]([A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\])*)/g
 var keywordsRE = new RegExp('^(' + keywords.replace(/,/g, '\\b|') + '\\b)')
+var booleanLiteralRE = /^(true|false)$/
 
 /**
  * Save / Rewrite / Restore
@@ -225,10 +226,14 @@ exports.parse = function (exp, needSet) {
   // we do a simple path check to optimize for them.
   // the check fails valid paths with unusal whitespaces,
   // but that's too rare and we don't care.
-  // also skip paths that start with global "Math"
-  var res = pathTestRE.test(exp) && exp.slice(0, 5) !== 'Math.'
-    ? compilePathFns(exp)
-    : compileExpFns(exp, needSet)
+  // also skip boolean literals and paths that start with
+  // global "Math"
+  var res =
+    pathTestRE.test(exp) &&
+    !booleanLiteralRE.test(exp) &&
+    exp.slice(0, 5) !== 'Math.'
+      ? compilePathFns(exp)
+      : compileExpFns(exp, needSet)
   expressionCache.put(exp, res)
   return res
 }

+ 12 - 3
test/unit/specs/parsers/expression_spec.js

@@ -188,13 +188,22 @@ var testCases = [
     },
     expected: Math.sin(1),
     paths: ['a']
+  },
+  {
+    // boolean literal
+    exp: 'true',
+    scope: {
+      true: false
+    },
+    expected: true,
+    paths: []
   }
 ]
 
 describe('Expression Parser', function () {
-  
-  it('parse getter', function () {
-    testCases.forEach(function assertExp (testCase) {
+
+  testCases.forEach(function (testCase) {
+    it('parse getter: ' + testCase.exp, function () {
       var res = expParser.parse(testCase.exp, true)
       expect(res.get(testCase.scope)).toEqual(testCase.expected)
     })