Skip to content

Commit 9252b6e

Browse files
committed
recognize boolean literals in expression parser, fixes vuejs#715
1 parent c90c28a commit 9252b6e

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/parsers/expression.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var restoreRE = /"(\d+)"/g
2121
var pathTestRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\])*$/
2222
var pathReplaceRE = /[^\w$\.]([A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\])*)/g
2323
var keywordsRE = new RegExp('^(' + keywords.replace(/,/g, '\\b|') + '\\b)')
24+
var booleanLiteralRE = /^(true|false)$/
2425

2526
/**
2627
* Save / Rewrite / Restore
@@ -225,10 +226,14 @@ exports.parse = function (exp, needSet) {
225226
// we do a simple path check to optimize for them.
226227
// the check fails valid paths with unusal whitespaces,
227228
// but that's too rare and we don't care.
228-
// also skip paths that start with global "Math"
229-
var res = pathTestRE.test(exp) && exp.slice(0, 5) !== 'Math.'
230-
? compilePathFns(exp)
231-
: compileExpFns(exp, needSet)
229+
// also skip boolean literals and paths that start with
230+
// global "Math"
231+
var res =
232+
pathTestRE.test(exp) &&
233+
!booleanLiteralRE.test(exp) &&
234+
exp.slice(0, 5) !== 'Math.'
235+
? compilePathFns(exp)
236+
: compileExpFns(exp, needSet)
232237
expressionCache.put(exp, res)
233238
return res
234239
}

test/unit/specs/parsers/expression_spec.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,22 @@ var testCases = [
188188
},
189189
expected: Math.sin(1),
190190
paths: ['a']
191+
},
192+
{
193+
// boolean literal
194+
exp: 'true',
195+
scope: {
196+
true: false
197+
},
198+
expected: true,
199+
paths: []
191200
}
192201
]
193202

194203
describe('Expression Parser', function () {
195-
196-
it('parse getter', function () {
197-
testCases.forEach(function assertExp (testCase) {
204+
205+
testCases.forEach(function (testCase) {
206+
it('parse getter: ' + testCase.exp, function () {
198207
var res = expParser.parse(testCase.exp, true)
199208
expect(res.get(testCase.scope)).toEqual(testCase.expected)
200209
})

0 commit comments

Comments
 (0)