Evan You
9 years ago
8 changed files with 96 additions and 11 deletions
@ -0,0 +1,38 @@ |
|||
/* @flow */ |
|||
|
|||
import { dirRE } from './parser/index' |
|||
|
|||
// detect problematic expressions in a template
|
|||
export function detectErrors (ast: ASTNode): Array<string> { |
|||
const errors: Array<string> = [] |
|||
checkNode(ast, errors) |
|||
return errors |
|||
} |
|||
|
|||
function checkNode (node: ASTNode, errors: Array<string>) { |
|||
if (node.type === 1) { |
|||
for (const name in node.attrsMap) { |
|||
if (dirRE.test(name)) { |
|||
const value = node.attrsMap[name] |
|||
if (value) { |
|||
checkExpression(value, `${name}="${value}"`, errors) |
|||
} |
|||
} |
|||
} |
|||
if (node.children) { |
|||
for (let i = 0; i < node.children.length; i++) { |
|||
checkNode(node.children[i], errors) |
|||
} |
|||
} |
|||
} else if (node.type === 2) { |
|||
checkExpression(node.expression, node.text, errors) |
|||
} |
|||
} |
|||
|
|||
function checkExpression (exp: string, text: string, errors: Array<string>) { |
|||
try { |
|||
new Function(exp) |
|||
} catch (e) { |
|||
errors.push(`- invalid expression: ${text}`) |
|||
} |
|||
} |
Loading…
Reference in new issue