Skip to content

Commit

Permalink
Merge pull request #13 from hildjj/warning
Browse files Browse the repository at this point in the history
Add warning if running in debug mode to prevent confusion when a peg$debugger statement is left in by mistake.
  • Loading branch information
hildjj authored Nov 21, 2024
2 parents 2cd3ae5 + e8c126e commit ba511fc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function checkParserStarts(grammar, starts, modified, counts) {
startRule,
...options,
});
fail("Expected error but none received");
fail(`Expected error but none received for invalidInput: "${start.invalidInput}"`);
} catch (er) {
if (!(er instanceof grammar.SyntaxError)) {
throw er;
Expand Down Expand Up @@ -424,9 +424,14 @@ export async function testPeggy(grammarUrl, starts, opts) {
const withMap = src.toStringWithSourceMap();
const map = Buffer.from(withMap.map.toString()).toString("base64");
let code = withMap.code;
const sm = (opts && Object.prototype.hasOwnProperty.call(opts, "noMap"))
? !opts.noMap
: starts.every(s => !s.options?.peg$debugger);
let sm = starts.every(s => !s.options?.peg$debugger);
if (opts && Object.prototype.hasOwnProperty.call(opts, "noMap")) {
sm = !opts.noMap;
} else {
if (!sm) {
console.error("WARNING: sourcemap disabled due to peg$debugger");
}
}
const start = "//#"; // c8: THIS file is not mapped.
if (sm) {
code += `
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"typedoc": "0.26.11",
"typescript": "5.6.3"
},
"packageManager": "[email protected]",
"packageManager": "[email protected]+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387",
"pnpm": {
"overrides": {
"@eslint/plugin-kit": "^0.2.3",
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,19 @@ test("thows errors", async() => {
},
]));
});

test("peg$debugger", async() => {
const old = console.error;
const stderr = [];
console.error = (...args) => stderr.push(args);
await testPeggy(MIN, [
{
validInput: "foo",
options: {
peg$debugger: true,
},
},
]);
console.error = old;
deepEqual(stderr, [["WARNING: sourcemap disabled due to peg$debugger"]]);
});

0 comments on commit ba511fc

Please sign in to comment.