diff --git a/packages/compiler/src/expression_parser/lexer.ts b/packages/compiler/src/expression_parser/lexer.ts index e88fa3f2460..75123a03096 100644 --- a/packages/compiler/src/expression_parser/lexer.ts +++ b/packages/compiler/src/expression_parser/lexer.ts @@ -133,11 +133,13 @@ export class Token { } isTemplateLiteralPart(): this is StringToken { - return this.isString() && this.kind === StringTokenKind.TemplateLiteralPart; + // Note: Explicit type is needed for Closure. + return this.isString() && (this as StringToken).kind === StringTokenKind.TemplateLiteralPart; } isTemplateLiteralEnd(): this is StringToken { - return this.isString() && this.kind === StringTokenKind.TemplateLiteralEnd; + // Note: Explicit type is needed for Closure. + return this.isString() && (this as StringToken).kind === StringTokenKind.TemplateLiteralEnd; } isTemplateLiteralInterpolationStart(): boolean { diff --git a/packages/compiler/src/ml_parser/lexer.ts b/packages/compiler/src/ml_parser/lexer.ts index 02da91b00e1..b7296c70668 100644 --- a/packages/compiler/src/ml_parser/lexer.ts +++ b/packages/compiler/src/ml_parser/lexer.ts @@ -1725,9 +1725,15 @@ class EscapedCharacterCursor extends PlainCharacterCursor { } } -export class CursorError { +export class CursorError extends Error { constructor( public msg: string, public cursor: CharacterCursor, - ) {} + ) { + super(msg); + + // Extending `Error` does not always work when code is transpiled. See: + // https://stackoverflow.com/questions/41102060/typescript-extending-error-class + Object.setPrototypeOf(this, new.target.prototype); + } } diff --git a/packages/compiler/src/parse_util.ts b/packages/compiler/src/parse_util.ts index ad89ebad811..0bdbe3ce2e6 100644 --- a/packages/compiler/src/parse_util.ts +++ b/packages/compiler/src/parse_util.ts @@ -148,7 +148,7 @@ export enum ParseErrorLevel { ERROR, } -export class ParseError { +export class ParseError extends Error { constructor( /** Location of the error. */ readonly span: ParseSourceSpan, @@ -161,7 +161,14 @@ export class ParseError { * couldn't be parsed. Not guaranteed to be defined, but can be used to provide more context. */ readonly relatedError?: unknown, - ) {} + ) { + super(msg); + + // Extending `Error` ends up breaking some internal tests. This appears to be a known issue + // when extending errors in TS and the workaround is to explicitly set the prototype. + // https://stackoverflow.com/questions/41102060/typescript-extending-error-class + Object.setPrototypeOf(this, new.target.prototype); + } contextualMessage(): string { const ctx = this.span.start.getContext(100, 3); @@ -170,7 +177,7 @@ export class ParseError { : this.msg; } - toString(): string { + override toString(): string { const details = this.span.details ? `, ${this.span.details}` : ''; return `${this.contextualMessage()}: ${this.span.start}${details}`; }