Skip to content

Commit

Permalink
fix: improve error detector v-for identifier check
Browse files Browse the repository at this point in the history
close #6971
  • Loading branch information
yyx990803 committed Nov 20, 2017
1 parent c264335 commit d891cd1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/compiler/error-detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ const unaryOperatorsRE = new RegExp('\\b' + (
'delete,typeof,void'
).split(',').join('\\s*\\([^\\)]*\\)|\\b') + '\\s*\\([^\\)]*\\)')

// check valid identifier for v-for
const identRE = /[A-Za-z_$][\w$]*/

// strip strings in expressions
const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g

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

function checkIdentifier (ident: ?string, type: string, text: string, errors: Array<string>) {
if (typeof ident === 'string' && !identRE.test(ident)) {
errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
function checkIdentifier (
ident: ?string,
type: string,
text: string,
errors: Array<string>
) {
if (typeof ident === 'string') {
try {
new Function(`var ${ident}`)
} catch (e) {
errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
}
}
}

Expand Down

0 comments on commit d891cd1

Please sign in to comment.