|
2 | 2 |
|
3 | 3 | import { dirRE } from './parser/index'
|
4 | 4 |
|
| 5 | +const keywordRE = new RegExp('\\b' + ( |
| 6 | + 'do,if,in,for,let,new,try,var,case,else,with,await,break,catch,class,const,' + |
| 7 | + 'super,throw,while,yield,delete,export,import,return,switch,typeof,default,' + |
| 8 | + 'extends,finally,continue,debugger,function,arguments,instanceof' |
| 9 | +).split(',').join('\\b|\\b') + '\\b') |
| 10 | + |
5 | 11 | // detect problematic expressions in a template
|
6 | 12 | export function detectErrors (ast: ?ASTNode): Array<string> {
|
7 | 13 | const errors: Array<string> = []
|
@@ -32,9 +38,22 @@ function checkNode (node: ASTNode, errors: Array<string>) {
|
32 | 38 | }
|
33 | 39 |
|
34 | 40 | function checkExpression (exp: string, text: string, errors: Array<string>) {
|
35 |
| - try { |
36 |
| - new Function(exp) |
37 |
| - } catch (e) { |
38 |
| - errors.push(`- invalid expression: ${text}`) |
| 41 | + exp = stripToString(exp) |
| 42 | + const keywordMatch = exp.match(keywordRE) |
| 43 | + if (keywordMatch) { |
| 44 | + errors.push( |
| 45 | + `- avoid using JavaScript keyword as property name: ` + |
| 46 | + `"${keywordMatch[0]}" in expression ${text}` |
| 47 | + ) |
| 48 | + } else { |
| 49 | + try { |
| 50 | + new Function(exp) |
| 51 | + } catch (e) { |
| 52 | + errors.push(`- invalid expression: ${text}`) |
| 53 | + } |
39 | 54 | }
|
40 | 55 | }
|
| 56 | + |
| 57 | +function stripToString (exp) { |
| 58 | + return exp.replace(/^_s\((.*)\)$/, '$1') |
| 59 | +} |
0 commit comments