Skip to content

Commit 226e662

Browse files
committed
feat(parser): TemplateParser.tryParse() returns both the AST and errors
The language service (angular#7482) always needs the AST even if there are errors in the template. Closes angular#7858
1 parent 7a1a1b8 commit 226e662

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

modules/angular2/src/compiler/template_parser.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export class TemplateParseError extends ParseError {
8383
constructor(message: string, span: ParseSourceSpan) { super(span, message); }
8484
}
8585

86+
export class TemplateParseResult {
87+
constructor(public templateAst?: TemplateAst[], public errors?: ParseError[]) {}
88+
}
89+
8690
@Injectable()
8791
export class TemplateParser {
8892
constructor(private _exprParser: Parser, private _schemaRegistry: ElementSchemaRegistry,
@@ -91,20 +95,29 @@ export class TemplateParser {
9195

9296
parse(template: string, directives: CompileDirectiveMetadata[], pipes: CompilePipeMetadata[],
9397
templateUrl: string): TemplateAst[] {
98+
var result = this.tryParse(template, directives, pipes, templateUrl);
99+
if (isPresent(result.errors)) {
100+
var errorString = result.errors.join('\n');
101+
throw new BaseException(`Template parse errors:\n${errorString}`);
102+
}
103+
return result.templateAst;
104+
}
105+
106+
tryParse(template: string, directives: CompileDirectiveMetadata[], pipes: CompilePipeMetadata[],
107+
templateUrl: string): TemplateParseResult {
94108
var parseVisitor =
95109
new TemplateParseVisitor(directives, pipes, this._exprParser, this._schemaRegistry);
96110
var htmlAstWithErrors = this._htmlParser.parse(template, templateUrl);
97111
var result = htmlVisitAll(parseVisitor, htmlAstWithErrors.rootNodes, EMPTY_COMPONENT);
98112
var errors: ParseError[] = htmlAstWithErrors.errors.concat(parseVisitor.errors);
99113
if (errors.length > 0) {
100-
var errorString = errors.join('\n');
101-
throw new BaseException(`Template parse errors:\n${errorString}`);
114+
return new TemplateParseResult(result, errors);
102115
}
103116
if (isPresent(this.transforms)) {
104117
this.transforms.forEach(
105118
(transform: TemplateAstVisitor) => { result = templateVisitAll(transform, result); });
106119
}
107-
return result;
120+
return new TemplateParseResult(result);
108121
}
109122
}
110123

0 commit comments

Comments
 (0)