Skip to content

Commit

Permalink
Fix definition of DiagnosticCallback. Fixes #283.
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Jun 10, 2022
1 parent e3c63ec commit e879502
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Released: TBD

### Bug Fixes

- None
- [#283](https://github.com/peggyjs/peggy/issues/283) Fix incorrect type
information for DiagnosticCallback, from @hildjj

2.0.1
-----
Expand Down
58 changes: 14 additions & 44 deletions lib/peg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,50 +997,20 @@ export interface Session {
): void;
}

export interface DiagnosticCallback {
/**
* Called when compiler reports an error.
*
* @param stage Stage in which this diagnostic was originated
* @param message Main message, which should describe error objectives
* @param location If defined, this is location described in the `message`
* @param notes Additional messages with context information
*/
error?(
stage: Stage,
message: string,
location?: LocationRange,
notes?: DiagnosticNote[]
): void;
/**
* Called when compiler reports a warning.
*
* @param stage Stage in which this diagnostic was originated
* @param message Main message, which should describe warning objectives
* @param location If defined, this is location described in the `message`
* @param notes Additional messages with context information
*/
warning?(
stage: Stage,
message: string,
location?: LocationRange,
notes?: DiagnosticNote[]
): void;
/**
* Called when compiler reports an informational message.
*
* @param stage Stage in which this diagnostic was originated
* @param message Main message, which gives information about an event
* @param location If defined, this is location described in the `message`
* @param notes Additional messages with context information
*/
info?(
stage: Stage,
message: string,
location?: LocationRange,
notes?: DiagnosticNote[]
): void;
}
/**
* Called when compiler reports an error, warning, or info.
*
* @param stage Stage in which this diagnostic was originated
* @param message Main message, which should describe error objectives
* @param location If defined, this is location described in the `message`
* @param notes Additional messages with context information
*/
export type DiagnosticCallback = (
stage: Stage,
message: string,
location?: LocationRange,
notes?: DiagnosticNote[]
) => void;

/**
* Parser dependencies, is an object which maps variables used to access the
Expand Down
43 changes: 41 additions & 2 deletions test/types/peg.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ const src = readFileSync(
"utf8"
);

const problems: peggy.Problem[] = [];

function error(
stage: peggy.Stage,
message: string,
location?: peggy.LocationRange,
notes?: peggy.DiagnosticNote[]
): void {
problems.push(["error", message, location, notes]);
}

function info(
stage: peggy.Stage,
message: string,
location?: peggy.LocationRange,
notes?: peggy.DiagnosticNote[]
): void {
problems.push(["info", message, location, notes]);
}

function warning(
stage: peggy.Stage,
message: string,
location?: peggy.LocationRange,
notes?: peggy.DiagnosticNote[]
): void {
problems.push(["warning", message, location, notes]);
}

describe("peg.d.ts", () => {
it("executes a grammar", () => {
expectType<string>(src);
Expand Down Expand Up @@ -45,7 +74,12 @@ describe("peg.d.ts", () => {
});

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

parser.parse(" /**/ 1\n", {
Expand Down Expand Up @@ -379,7 +413,12 @@ describe("peg.d.ts", () => {
expectType<peggy.ast.Grammar>(ast);
const parser = peggy.compiler.compile(
ast,
peggy.compiler.passes
peggy.compiler.passes,
{
error,
info,
warning,
}
);
expectType<peggy.Parser>(parser);
expectType<peggy.ast.MatchResult | undefined>(ast.rules[0].match);
Expand Down

0 comments on commit e879502

Please sign in to comment.