@@ -10,6 +10,8 @@ const prohibitedKeywordRE = new RegExp('\\b' + (
10
10
) . split ( ',' ) . join ( '\\b|\\b' ) + '\\b' )
11
11
// check valid identifier for v-for
12
12
const identRE = / [ A - Z a - z _ $ ] [ \w $ ] * /
13
+ // strip strings in expressions
14
+ const stripStringRE = / ' (?: [ ^ ' \\ ] | \\ .) * ' | " (?: [ ^ " \\ ] | \\ .) * " | ` (?: [ ^ ` \\ ] | \\ .) * \$ \{ | \} (?: [ ^ ` \\ ] | \\ .) * ` | ` (?: [ ^ ` \\ ] | \\ .) * ` / g
13
15
14
16
// detect problematic expressions in a template
15
17
export function detectErrors ( ast : ?ASTNode ) : Array < string > {
@@ -58,22 +60,17 @@ function checkIdentifier (ident: ?string, type: string, text: string, errors: Ar
58
60
}
59
61
60
62
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 {
72
73
errors . push ( `- invalid expression: ${ text } ` )
73
74
}
74
75
}
75
76
}
76
-
77
- function stripToString ( exp ) {
78
- return exp . replace ( / ^ _ s \( ( .* ) \) $ / , '$1' )
79
- }
0 commit comments