refactor(compiler): add optional visit() to html AST Visitor (#12135)

This commit is contained in:
Chuck Jazdzewski
2016-10-10 09:13:50 -07:00
committed by Tobias Bosch
parent e1faca6386
commit 4202936bbf
2 changed files with 73 additions and 1 deletions

View File

@ -54,6 +54,10 @@ export class Comment implements Node {
}
export interface Visitor {
// Returning a truthy value from `visit()` will prevent `visitAll()` from the call to the typed
// method and result returned will become the result included in `visitAll()`s result array.
visit?(node: Node, context: any): any;
visitElement(element: Element, context: any): any;
visitAttribute(attribute: Attribute, context: any): any;
visitText(text: Text, context: any): any;
@ -64,8 +68,12 @@ export interface Visitor {
export function visitAll(visitor: Visitor, nodes: Node[], context: any = null): any[] {
let result: any[] = [];
let visit = visitor.visit ?
(ast: Node) => visitor.visit(ast, context) || ast.visit(visitor, context) :
(ast: Node) => ast.visit(visitor, context);
nodes.forEach(ast => {
const astResult = ast.visit(visitor, context);
const astResult = visit(ast);
if (astResult) {
result.push(astResult);
}