mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(compiler-cli): more accurate diagnostics for host binding parser errors (#58870)
Currently host bindings are in a bit of a weird state, because their source spans all point to the root object literal, rather than the individual expression. This is tricky to handle at the moment, because the object is being passed around as a `Record<string, string>` since the compiler needs to support both JIT and non-JIT environments, and because the AOT compiler evaluates the entire literal rather than doing it expression-by-expression. As a result, when we report errors in one of the host bindings, we end up highlighting the entire expression which can be very noisy in an IDE. These changes aim to report a more accurate error for the most common case where the `host` object is initialized to a `string -> string` object literal by matching the failing expression to one of the property initializers. Note that this isn't 100% reliable, because we can't map cases like `host: SOME_CONST`, but it's still better than the current setup. PR Close #58870
This commit is contained in:
committed by
Jessica Janiuk
parent
ea0bf74e31
commit
fb1fa8b0fc
@@ -150,9 +150,17 @@ export enum ParseErrorLevel {
|
||||
|
||||
export class ParseError {
|
||||
constructor(
|
||||
public span: ParseSourceSpan,
|
||||
public msg: string,
|
||||
public level: ParseErrorLevel = ParseErrorLevel.ERROR,
|
||||
/** Location of the error. */
|
||||
readonly span: ParseSourceSpan,
|
||||
/** Error message. */
|
||||
readonly msg: string,
|
||||
/** Severity level of the error. */
|
||||
readonly level: ParseErrorLevel = ParseErrorLevel.ERROR,
|
||||
/**
|
||||
* Error that caused the error to be surfaced. For example, an error in a sub-expression that
|
||||
* couldn't be parsed. Not guaranteed to be defined, but can be used to provide more context.
|
||||
*/
|
||||
readonly relatedError?: unknown,
|
||||
) {}
|
||||
|
||||
contextualMessage(): string {
|
||||
|
||||
@@ -770,13 +770,14 @@ export class BindingParser {
|
||||
message: string,
|
||||
sourceSpan: ParseSourceSpan,
|
||||
level: ParseErrorLevel = ParseErrorLevel.ERROR,
|
||||
relatedError?: ParserError,
|
||||
) {
|
||||
this.errors.push(new ParseError(sourceSpan, message, level));
|
||||
this.errors.push(new ParseError(sourceSpan, message, level, relatedError));
|
||||
}
|
||||
|
||||
private _reportExpressionParserErrors(errors: ParserError[], sourceSpan: ParseSourceSpan) {
|
||||
for (const error of errors) {
|
||||
this._reportError(error.message, sourceSpan);
|
||||
this._reportError(error.message, sourceSpan, undefined, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user