| 14 | export type ScriptParseFn = (source: string) => void |
| 15 | |
| 16 | function walk( |
| 17 | node: |
| 18 | | RootNode |
| 19 | | TemplateChildNode |
| 20 | | SimpleExpressionNode |
| 21 | | CompoundExpressionNode |
| 22 | | InterpolationNode |
| 23 | | TextNode |
| 24 | | string |
| 25 | | symbol |
| 26 | | undefined, |
| 27 | visitor: ( |
| 28 | node: |
| 29 | | ElementNode |
| 30 | | InterpolationNode |
| 31 | | CompoundExpressionNode |
| 32 | | SimpleExpressionNode |
| 33 | ) => void |
| 34 | ) { |
| 35 | if (typeof node !== 'object' || node == null) { |
| 36 | return |
| 37 | } |
| 38 | if (node.type === NodeTypes.ROOT) { |
| 39 | node.children.forEach(n => walk(n, visitor)) |
| 40 | return |
| 41 | } |
| 42 | if ( |
| 43 | node.type !== NodeTypes.ELEMENT && |
| 44 | node.type !== NodeTypes.COMPOUND_EXPRESSION && |
| 45 | node.type !== NodeTypes.INTERPOLATION |
| 46 | ) { |
| 47 | return |
| 48 | } |
| 49 | visitor(node) |
| 50 | if (node.type === NodeTypes.INTERPOLATION) { |
| 51 | visitor(node.content) |
| 52 | } else if (node.type === NodeTypes.ELEMENT) { |
| 53 | node.children.forEach(n => walk(n, visitor)) |
| 54 | node.props |
| 55 | .filter( |
| 56 | (prop): prop is DirectiveNode => prop.type === NodeTypes.DIRECTIVE |
| 57 | ) |
| 58 | .filter(prop => !!prop.exp) |
| 59 | .forEach(prop => visitor(prop.exp!)) |
| 60 | } else { |
| 61 | node.children.forEach(n => walk(n, visitor)) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function templateSimpleExpressionNodeVisitor(parseScriptFn: ScriptParseFn) { |
| 66 | return ( |