Skip to content

Commit 98ab333

Browse files
yyx990803lovelope
authored andcommitted
fix: improve error detector v-for identifier check
close vuejs#6971
1 parent 6b9a511 commit 98ab333

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/compiler/error-detector.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const unaryOperatorsRE = new RegExp('\\b' + (
1515
'delete,typeof,void'
1616
).split(',').join('\\s*\\([^\\)]*\\)|\\b') + '\\s*\\([^\\)]*\\)')
1717

18-
// check valid identifier for v-for
19-
const identRE = /[A-Za-z_$][\w$]*/
20-
2118
// strip strings in expressions
2219
const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
2320

@@ -75,9 +72,18 @@ function checkFor (node: ASTElement, text: string, errors: Array<string>) {
7572
checkIdentifier(node.iterator2, 'v-for iterator', text, errors)
7673
}
7774

78-
function checkIdentifier (ident: ?string, type: string, text: string, errors: Array<string>) {
79-
if (typeof ident === 'string' && !identRE.test(ident)) {
80-
errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
75+
function checkIdentifier (
76+
ident: ?string,
77+
type: string,
78+
text: string,
79+
errors: Array<string>
80+
) {
81+
if (typeof ident === 'string') {
82+
try {
83+
new Function(`var ${ident}`)
84+
} catch (e) {
85+
errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
86+
}
8187
}
8288
}
8389

0 commit comments

Comments
 (0)