refactor(compiler-cli): make template parsing errors into diagnostics (#38576)

Previously, the compiler was not able to display template parsing errors as
true `ts.Diagnostic`s that point inside the template. Instead, it would
throw an actual `Error`, and "crash" with a stack trace containing the
template errors.

Not only is this a poor user experience, but it causes the Language Service
to also crash as the user is editing a template (in actuality the LS has to
work around this bug).

With this commit, such parsing errors are converted to true template
diagnostics with appropriate span information to be displayed contextually
along with all other diagnostics. This majorly improves the user experience
and unblocks the Language Service from having to deal with the compiler
"crashing" to report errors.

PR Close #38576
This commit is contained in:
Alex Rickabaugh
2020-08-25 09:46:40 -04:00
committed by Andrew Scott
parent 3e97435f1c
commit c90eb5450d
6 changed files with 84 additions and 10 deletions

View File

@ -62,12 +62,6 @@ export function htmlAstToRender3Ast(
// Errors might originate in either the binding parser or the html to ivy transformer
const allErrors = bindingParser.errors.concat(transformer.errors);
const errors: ParseError[] = allErrors.filter(e => e.level === ParseErrorLevel.ERROR);
if (errors.length > 0) {
const errorString = errors.join('\n');
throw syntaxError(`Template parse errors:\n${errorString}`, errors);
}
return {
nodes: ivyNodes,

View File

@ -101,7 +101,14 @@ export function parseR3(
['onEvent'], ['onEvent']);
const bindingParser =
new BindingParser(expressionParser, DEFAULT_INTERPOLATION_CONFIG, schemaRegistry, null, []);
return htmlAstToRender3Ast(htmlNodes, bindingParser);
const r3Result = htmlAstToRender3Ast(htmlNodes, bindingParser);
if (r3Result.errors.length > 0) {
const msg = r3Result.errors.map(e => e.toString()).join('\n');
throw new Error(msg);
}
return r3Result;
}
export function processI18nMeta(