Skip to content

Commit

Permalink
Add SyntaxErrorConstructor type, fixes peggyjs#216
Browse files Browse the repository at this point in the history
  • Loading branch information
cmfcmf committed Nov 22, 2021
1 parent ddb35b4 commit 1573cba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Released: TBD

- [#164](https://github.com/peggyjs/peggy/pull/164): Fix some errors in the typescript definitions
- [#169](https://github.com/peggyjs/peggy/issues/169): Expose string escape functions
- [#216](https://github.com/peggyjs/peggy/issues/216): Fix typescript definition of SyntaxError

1.2.0
-----
Expand Down
29 changes: 20 additions & 9 deletions lib/peg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,25 @@ export namespace parser {
| EndExpectation
| OtherExpectation;

interface SyntaxErrorConstructor {
new (
message: string,
expected: Expectation[] | null,
found: string | null,
location: LocationRange
): SyntaxError;
readonly prototype: SyntaxError;

// Static methods
/**
* Constructs the human-readable message from the machine representation.
*
* @param expected Array of expected items, generated by the parser
* @param found Any text that will appear as found in the input instead of expected
*/
buildMessage(expected: Expectation[], found: string): string;
}

/** Thrown if the grammar contains a syntax error. */
class SyntaxError extends Error {
/** Location where error was originated. */
Expand Down Expand Up @@ -427,14 +446,6 @@ export namespace parser {
* @returns the formatted error
*/
format(sources: SourceText[]): string;

/**
* Constructs the human-readable message from the machine representation.
*
* @param expected Array of expected items, generated by the parser
* @param found Any text that will appear as found in the input instead of expected
*/
static buildMessage(expected: Expectation[], found: string): string;
}
}

Expand Down Expand Up @@ -760,7 +771,7 @@ export interface ParserOptions {
export interface Parser {
parse(input: string, options?: ParserOptions): any;

SyntaxError: parser.SyntaxError;
SyntaxError: parser.SyntaxErrorConstructor;
}

export interface ParserTracer {
Expand Down
13 changes: 13 additions & 0 deletions test/types/peg.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ describe("peg.d.ts", () => {
expect(res).toStrictEqual(["buzz", 11, "fizz"]);
});

it("types SyntaxError correctly", () => {
const parser = peggy.generate(src);

expectType<peggy.parser.SyntaxErrorConstructor>(parser.SyntaxError);
expectType<peggy.parser.SyntaxError>(new parser.SyntaxError("", null, null, {
source: null,
start: { line: 0, column: 0, offset: 0 },
end: { line: 0, column: 0, offset: 0 },
}));

expectType<string>(parser.SyntaxError.buildMessage([], ""));
});

it("takes a valid tracer", () => {
const parser = peggy.generate(src, { trace: true });
expectType<peggy.Parser>(parser);
Expand Down

0 comments on commit 1573cba

Please sign in to comment.