Fix ParserCore#unexpected
MODIFY
packages/@romejs/codec-js-regexp/index.ts
packages/@romejs/codec-js-regexp/index.ts
@@ -146,11 +146,11 @@ export const createRegExpParser = createParser((ParserCore) =>
diagnostics: Diagnostics;
unicode: boolean;
- addDiagnostic(opts: ParserUnexpectedOptions) {
+ addDiagnostic(opts: ParserUnexpectedOptions): void {
this.diagnostics.push(this.createDiagnostic(opts));
}
- unexpected() {
+ unexpected(): never {
throw new Error('No throwing');
}

MODIFY
packages/@romejs/js-parser/parser.ts
packages/@romejs/js-parser/parser.ts
@@ -599,13 +599,13 @@ const createJSParser = createParser((ParserCore, ParserWithRequiredPath) =>
});
}
- unexpected() {
+ unexpected(): never {
throw new Error(
'js-parser should never throw an exception, use addDiagnostic or unexpectedToken instead',
);
}
- tokenize() {
+ tokenize(): never {
throw new Error('js-parser does not use the parser-core tokenizer');
}

MODIFY
packages/@romejs/parser-core/index.ts
packages/@romejs/parser-core/index.ts
@@ -24,7 +24,7 @@ import {
Diagnostic,
DiagnosticCategory,
DiagnosticDescription,
-
+ DiagnosticsError,
createBlessedDiagnosticMessage,
descriptions,
} from '@romejs/diagnostics';
@@ -348,7 +348,7 @@ export class ParserCore<Tokens extends TokensShape, State> {
}
// Get the end position of the current token
- getLastEndPosition() {
+ getLastEndPosition(): Position {
return this.getPositionFromIndex(this.prevToken.end);
}
@@ -525,12 +525,12 @@ export class ParserCore<Tokens extends TokensShape, State> {
}
// Return an error to indicate a parser error, this must be thrown at the callsite for refinement
- unexpected(opts: ParserUnexpectedOptions = {}) {
- throw createSingleDiagnosticError(this.createDiagnostic(opts));
+ unexpected(opts: ParserUnexpectedOptions = {}): DiagnosticsError {
+ return createSingleDiagnosticError(this.createDiagnostic(opts));
}
//# Token utility methods
- assertNoSpace() {
+ assertNoSpace(): void {
if (this.currentToken.start !== this.prevToken.end) {
throw this.unexpected({
description: descriptions.PARSER_CORE.EXPECTED_SPACE,
@@ -671,7 +671,7 @@ export class ParserCore<Tokens extends TokensShape, State> {
};
}
- finalize() {
+ finalize(): void {
if (!this.eatToken('EOF')) {
throw this.unexpected({
description: descriptions.PARSER_CORE.EXPECTED_EOF,

Fix ParserCore#unexpected