From 12ba62e5e296bb2012900bc761d55e5da85fe71c Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Tue, 11 Oct 2016 15:45:27 -0700 Subject: [PATCH] refactor(compiler): expose template parser phases (#12210) --- .../src/template_parser/template_parser.ts | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/modules/@angular/compiler/src/template_parser/template_parser.ts b/modules/@angular/compiler/src/template_parser/template_parser.ts index b3c42efc74..b02c84b0f8 100644 --- a/modules/@angular/compiler/src/template_parser/template_parser.ts +++ b/modules/@angular/compiler/src/template_parser/template_parser.ts @@ -118,22 +118,18 @@ export class TemplateParser { component: CompileDirectiveMetadata, template: string, directives: CompileDirectiveMetadata[], pipes: CompilePipeMetadata[], schemas: SchemaMetadata[], templateUrl: string): TemplateParseResult { - let interpolationConfig: any; - if (component.template) { - interpolationConfig = InterpolationConfig.fromArray(component.template.interpolation); - } - let htmlAstWithErrors = - this._htmlParser.parse(template, templateUrl, true, interpolationConfig); - const errors: ParseError[] = htmlAstWithErrors.errors; - let result: TemplateAst[]; - - if (errors.length == 0) { - // Transform ICU messages to angular directives - const expandedHtmlAst = expandNodes(htmlAstWithErrors.rootNodes); - errors.push(...expandedHtmlAst.errors); - htmlAstWithErrors = new ParseTreeResult(expandedHtmlAst.nodes, errors); - } + return this.tryParseHtml( + this.expandHtml(this._htmlParser.parse( + template, templateUrl, true, this.getInterpolationConfig(component))), + component, template, directives, pipes, schemas, templateUrl); + } + tryParseHtml( + htmlAstWithErrors: ParseTreeResult, component: CompileDirectiveMetadata, template: string, + directives: CompileDirectiveMetadata[], pipes: CompilePipeMetadata[], + schemas: SchemaMetadata[], templateUrl: string): TemplateParseResult { + var result: TemplateAst[]; + var errors = htmlAstWithErrors.errors; if (htmlAstWithErrors.rootNodes.length > 0) { const uniqDirectives = removeIdentifierDuplicates(directives); const uniqPipes = removeIdentifierDuplicates(pipes); @@ -147,7 +143,6 @@ export class TemplateParser { } else { result = []; } - this._assertNoReferenceDuplicationOnTemplate(result, errors); if (errors.length > 0) { @@ -162,6 +157,24 @@ export class TemplateParser { return new TemplateParseResult(result, errors); } + expandHtml(htmlAstWithErrors: ParseTreeResult, forced: boolean = false): ParseTreeResult { + const errors: ParseError[] = htmlAstWithErrors.errors; + + if (errors.length == 0 || forced) { + // Transform ICU messages to angular directives + const expandedHtmlAst = expandNodes(htmlAstWithErrors.rootNodes); + errors.push(...expandedHtmlAst.errors); + htmlAstWithErrors = new ParseTreeResult(expandedHtmlAst.nodes, errors); + } + return htmlAstWithErrors; + } + + getInterpolationConfig(component: CompileDirectiveMetadata): InterpolationConfig { + if (component.template) { + return InterpolationConfig.fromArray(component.template.interpolation); + } + } + /** @internal */ _assertNoReferenceDuplicationOnTemplate(result: TemplateAst[], errors: TemplateParseError[]): void { @@ -443,6 +456,7 @@ class TemplateParseVisitor implements html.Visitor { providerContext.transformedDirectiveAsts, providerContext.transformProviders, providerContext.transformedHasViewContainer, children, hasInlineTemplates ? null : ngContentIndex, element.sourceSpan, element.endSourceSpan); + this._findComponentDirectives(directiveAsts) .forEach( componentDirectiveAst => this._validateElementAnimationInputOutputs(