UNPKG

3.56 kBJavaScriptView Raw
1/* @flow */
2
3import { dirRE, onRE } from './parser/index'
4
5type Range = { start?: number, end?: number };
6
7// these keywords should not appear inside expressions, but operators like
8// typeof, instanceof and in are allowed
9const prohibitedKeywordRE = new RegExp('\\b' + (
10 'do,if,for,let,new,try,var,case,else,with,await,break,catch,class,const,' +
11 'super,throw,while,yield,delete,export,import,return,switch,default,' +
12 'extends,finally,continue,debugger,function,arguments'
13).split(',').join('\\b|\\b') + '\\b')
14
15// these unary operators should not be used as property/method names
16const unaryOperatorsRE = new RegExp('\\b' + (
17 'delete,typeof,void'
18).split(',').join('\\s*\\([^\\)]*\\)|\\b') + '\\s*\\([^\\)]*\\)')
19
20// strip strings in expressions
21const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
22
23// detect problematic expressions in a template
24export function detectErrors (ast: ?ASTNode, warn: Function) {
25 if (ast) {
26 checkNode(ast, warn)
27 }
28}
29
30function checkNode (node: ASTNode, warn: Function) {
31 if (node.type === 1) {
32 for (const name in node.attrsMap) {
33 if (dirRE.test(name)) {
34 const value = node.attrsMap[name]
35 if (value) {
36 const range = node.rawAttrsMap[name]
37 if (name === 'v-for') {
38 checkFor(node, `v-for="${value}"`, warn, range)
39 } else if (onRE.test(name)) {
40 checkEvent(value, `${name}="${value}"`, warn, range)
41 } else {
42 checkExpression(value, `${name}="${value}"`, warn, range)
43 }
44 }
45 }
46 }
47 if (node.children) {
48 for (let i = 0; i < node.children.length; i++) {
49 checkNode(node.children[i], warn)
50 }
51 }
52 } else if (node.type === 2) {
53 checkExpression(node.expression, node.text, warn, node)
54 }
55}
56
57function checkEvent (exp: string, text: string, warn: Function, range?: Range) {
58 const stipped = exp.replace(stripStringRE, '')
59 const keywordMatch: any = stipped.match(unaryOperatorsRE)
60 if (keywordMatch && stipped.charAt(keywordMatch.index - 1) !== '$') {
61 warn(
62 `avoid using JavaScript unary operator as property name: ` +
63 `"${keywordMatch[0]}" in expression ${text.trim()}`,
64 range
65 )
66 }
67 checkExpression(exp, text, warn, range)
68}
69
70function checkFor (node: ASTElement, text: string, warn: Function, range?: Range) {
71 checkExpression(node.for || '', text, warn, range)
72 checkIdentifier(node.alias, 'v-for alias', text, warn, range)
73 checkIdentifier(node.iterator1, 'v-for iterator', text, warn, range)
74 checkIdentifier(node.iterator2, 'v-for iterator', text, warn, range)
75}
76
77function checkIdentifier (
78 ident: ?string,
79 type: string,
80 text: string,
81 warn: Function,
82 range?: Range
83) {
84 if (typeof ident === 'string') {
85 try {
86 new Function(`var ${ident}=_`)
87 } catch (e) {
88 warn(`invalid ${type} "${ident}" in expression: ${text.trim()}`, range)
89 }
90 }
91}
92
93function checkExpression (exp: string, text: string, warn: Function, range?: Range) {
94 try {
95 new Function(`return ${exp}`)
96 } catch (e) {
97 const keywordMatch = exp.replace(stripStringRE, '').match(prohibitedKeywordRE)
98 if (keywordMatch) {
99 warn(
100 `avoid using JavaScript keyword as property name: ` +
101 `"${keywordMatch[0]}"\n Raw expression: ${text.trim()}`,
102 range
103 )
104 } else {
105 warn(
106 `invalid expression: ${e.message} in\n\n` +
107 ` ${exp}\n\n` +
108 ` Raw expression: ${text.trim()}\n`,
109 range
110 )
111 }
112 }
113}