From 226e662cf1b9b1b8a5edc87863f04cba637d5a25 Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Thu, 31 Mar 2016 12:14:08 -0700 Subject: [PATCH] feat(parser): TemplateParser.tryParse() returns both the AST and errors The language service (#7482) always needs the AST even if there are errors in the template. Closes #7858 --- .../angular2/src/compiler/template_parser.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/compiler/template_parser.ts b/modules/angular2/src/compiler/template_parser.ts index af05bb5e2e..75e28bd8a9 100644 --- a/modules/angular2/src/compiler/template_parser.ts +++ b/modules/angular2/src/compiler/template_parser.ts @@ -83,6 +83,10 @@ export class TemplateParseError extends ParseError { constructor(message: string, span: ParseSourceSpan) { super(span, message); } } +export class TemplateParseResult { + constructor(public templateAst?: TemplateAst[], public errors?: ParseError[]) {} +} + @Injectable() export class TemplateParser { constructor(private _exprParser: Parser, private _schemaRegistry: ElementSchemaRegistry, @@ -91,20 +95,29 @@ export class TemplateParser { parse(template: string, directives: CompileDirectiveMetadata[], pipes: CompilePipeMetadata[], templateUrl: string): TemplateAst[] { + var result = this.tryParse(template, directives, pipes, templateUrl); + if (isPresent(result.errors)) { + var errorString = result.errors.join('\n'); + throw new BaseException(`Template parse errors:\n${errorString}`); + } + return result.templateAst; + } + + tryParse(template: string, directives: CompileDirectiveMetadata[], pipes: CompilePipeMetadata[], + templateUrl: string): TemplateParseResult { var parseVisitor = new TemplateParseVisitor(directives, pipes, this._exprParser, this._schemaRegistry); var htmlAstWithErrors = this._htmlParser.parse(template, templateUrl); var result = htmlVisitAll(parseVisitor, htmlAstWithErrors.rootNodes, EMPTY_COMPONENT); var errors: ParseError[] = htmlAstWithErrors.errors.concat(parseVisitor.errors); if (errors.length > 0) { - var errorString = errors.join('\n'); - throw new BaseException(`Template parse errors:\n${errorString}`); + return new TemplateParseResult(result, errors); } if (isPresent(this.transforms)) { this.transforms.forEach( (transform: TemplateAstVisitor) => { result = templateVisitAll(transform, result); }); } - return result; + return new TemplateParseResult(result); } }