Skip to content

Commit de0b4b8

Browse files
committed
fix error detection on string interpolations (fix #3137)
1 parent 634a073 commit de0b4b8

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/compiler/error-detector.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const prohibitedKeywordRE = new RegExp('\\b' + (
1010
).split(',').join('\\b|\\b') + '\\b')
1111
// check valid identifier for v-for
1212
const identRE = /[A-Za-z_$][\w$]*/
13+
// strip strings in expressions
14+
const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
1315

1416
// detect problematic expressions in a template
1517
export function detectErrors (ast: ?ASTNode): Array<string> {
@@ -58,22 +60,17 @@ function checkIdentifier (ident: ?string, type: string, text: string, errors: Ar
5860
}
5961

6062
function checkExpression (exp: string, text: string, errors: Array<string>) {
61-
exp = stripToString(exp)
62-
const keywordMatch = exp.match(prohibitedKeywordRE)
63-
if (keywordMatch) {
64-
errors.push(
65-
`- avoid using JavaScript keyword as property name: ` +
66-
`"${keywordMatch[0]}" in expression ${text}`
67-
)
68-
} else {
69-
try {
70-
new Function(`return ${exp}`)
71-
} catch (e) {
63+
try {
64+
new Function(`return ${exp}`)
65+
} catch (e) {
66+
const keywordMatch = exp.replace(stripStringRE, '').match(prohibitedKeywordRE)
67+
if (keywordMatch) {
68+
errors.push(
69+
`- avoid using JavaScript keyword as property name: ` +
70+
`"${keywordMatch[0]}" in expression ${text}`
71+
)
72+
} else {
7273
errors.push(`- invalid expression: ${text}`)
7374
}
7475
}
7576
}
76-
77-
function stripToString (exp) {
78-
return exp.replace(/^_s\((.*)\)$/, '$1')
79-
}

0 commit comments

Comments
 (0)