refactor(compiler): expose template parser phases (#12210)

This commit is contained in:
Chuck Jazdzewski 2016-10-11 15:45:27 -07:00 committed by Tobias Bosch
parent e6e007e2f1
commit 12ba62e5e2
1 changed files with 30 additions and 16 deletions

View File

@ -118,22 +118,18 @@ export class TemplateParser {
component: CompileDirectiveMetadata, template: string, directives: CompileDirectiveMetadata[], component: CompileDirectiveMetadata, template: string, directives: CompileDirectiveMetadata[],
pipes: CompilePipeMetadata[], schemas: SchemaMetadata[], pipes: CompilePipeMetadata[], schemas: SchemaMetadata[],
templateUrl: string): TemplateParseResult { templateUrl: string): TemplateParseResult {
let interpolationConfig: any; return this.tryParseHtml(
if (component.template) { this.expandHtml(this._htmlParser.parse(
interpolationConfig = InterpolationConfig.fromArray(component.template.interpolation); template, templateUrl, true, this.getInterpolationConfig(component))),
} component, template, directives, pipes, schemas, templateUrl);
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);
} }
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) { if (htmlAstWithErrors.rootNodes.length > 0) {
const uniqDirectives = removeIdentifierDuplicates(directives); const uniqDirectives = removeIdentifierDuplicates(directives);
const uniqPipes = removeIdentifierDuplicates(pipes); const uniqPipes = removeIdentifierDuplicates(pipes);
@ -147,7 +143,6 @@ export class TemplateParser {
} else { } else {
result = []; result = [];
} }
this._assertNoReferenceDuplicationOnTemplate(result, errors); this._assertNoReferenceDuplicationOnTemplate(result, errors);
if (errors.length > 0) { if (errors.length > 0) {
@ -162,6 +157,24 @@ export class TemplateParser {
return new TemplateParseResult(result, errors); 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 */ /** @internal */
_assertNoReferenceDuplicationOnTemplate(result: TemplateAst[], errors: TemplateParseError[]): _assertNoReferenceDuplicationOnTemplate(result: TemplateAst[], errors: TemplateParseError[]):
void { void {
@ -443,6 +456,7 @@ class TemplateParseVisitor implements html.Visitor {
providerContext.transformedDirectiveAsts, providerContext.transformProviders, providerContext.transformedDirectiveAsts, providerContext.transformProviders,
providerContext.transformedHasViewContainer, children, providerContext.transformedHasViewContainer, children,
hasInlineTemplates ? null : ngContentIndex, element.sourceSpan, element.endSourceSpan); hasInlineTemplates ? null : ngContentIndex, element.sourceSpan, element.endSourceSpan);
this._findComponentDirectives(directiveAsts) this._findComponentDirectives(directiveAsts)
.forEach( .forEach(
componentDirectiveAst => this._validateElementAnimationInputOutputs( componentDirectiveAst => this._validateElementAnimationInputOutputs(