Skip to content

Commit e2c0cc7

Browse files
committed
fix parsing dynamic sub path (fix #1935)
1 parent f858a5d commit e2c0cc7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/parsers/expression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const newlineRE = /\n/g
2626
const saveRE = /[\{,]\s*[\w\$_]+\s*:|('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")|new |typeof |void /g
2727
const restoreRE = /"(\d+)"/g
2828
const pathTestRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/
29-
const pathReplaceRE = /[^\w$\.](?:[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\])*)/g
29+
const identRE = /[^\w$\.](?:[A-Za-z_$][\w$]*)/g
3030
const booleanLiteralRE = /^(?:true|false)$/
3131

3232
/**
@@ -119,7 +119,7 @@ function compileGetter (exp) {
119119
// rewrite all paths
120120
// pad 1 space here becaue the regex matches 1 extra char
121121
body = (' ' + body)
122-
.replace(pathReplaceRE, rewrite)
122+
.replace(identRE, rewrite)
123123
.replace(restoreRE, restore)
124124
return makeGetterFn(body)
125125
}

test/unit/specs/parsers/expression_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ var testCases = [
181181
expected: "a\'b\'c",
182182
paths: ['c']
183183
},
184+
{
185+
// dynamic sub path
186+
exp: "a['b' + i + 'c']",
187+
scope: {
188+
i: 0,
189+
a: {
190+
'b0c': 123
191+
}
192+
},
193+
expected: 123,
194+
paths: ['a', 'i']
195+
},
184196
{
185197
// Math global, simple path
186198
exp: 'Math.PI',

0 commit comments

Comments
 (0)