refactor(compiler): ensure compiler passes closure conformance checks (#62500)

This commit adjusts some code of the compiler that currently results in
conformance checks failures with JSCompiler/Closure.

PR Close #62500
This commit is contained in:
Paul Gschwendtner
2025-07-07 14:55:12 +00:00
committed by Jessica Janiuk
parent 2c18043240
commit 403ba91327
3 changed files with 22 additions and 7 deletions
@@ -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 {
+8 -2
View File
@@ -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);
}
}
+10 -3
View File
@@ -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}`;
}