Browse Source

handle es2015 template strings in expressions (fix #2364)

Evan You 10 years ago
parent
commit
142046fad4
2 changed files with 10 additions and 1 deletions
  1. 1 1
      src/parsers/expression.js
  2. 9 0
      test/unit/specs/parsers/expression_spec.js

+ 1 - 1
src/parsers/expression.js

@@ -23,7 +23,7 @@ const improperKeywordsRE =
 
 const wsRE = /\s/g
 const newlineRE = /\n/g
-const saveRE = /[\{,]\s*[\w\$_]+\s*:|('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")|new |typeof |void /g
+const saveRE = /[\{,]\s*[\w\$_]+\s*:|('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`)|new |typeof |void /g
 const restoreRE = /"(\d+)"/g
 const pathTestRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/
 const identRE = /[^\w$\.](?:[A-Za-z_$][\w$]*)/g

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

@@ -286,6 +286,15 @@ describe('Expression Parser', function () {
     expect(res1).toBe(res2)
   })
 
+  it('ES2015 template string handling', function () {
+    var res = expParser.parseExpression('a + `hi ${ b }` + c')
+    expect(res.get.toString().indexOf('scope.a+`hi ${scope.b}`+scope.c') > -1).toBe(true)
+    res = expParser.parseExpression('`hi ${ b + `${ d }` }`')
+    expect(res.get.toString().indexOf('`hi ${scope.b+`${scope.d}`}`') > -1).toBe(true)
+    res = expParser.parseExpression('{transform:`rotate(${x}deg)`}')
+    expect(res.get.toString().indexOf('{transform:`rotate(${scope.x}deg)`}') > -1).toBe(true)
+  })
+
   describe('invalid expression', function () {
     beforeEach(function () {
       spyWarns()