Skip to content

Support input sourceMaps #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* original.
* @prop {boolean} [noOriginal] Do not run tests on the original code, only
* on the generated code.
* @prop {boolean} [inputSourceMap] Read sourceMap information from the input
* file. Use it to filter the generated sourceMap information.
*/
/**
* Test the basic functionality of a Peggy grammar, to make coverage easier.
Expand Down Expand Up @@ -113,4 +115,9 @@ export type TestPeggyOptions = {
* on the generated code.
*/
noOriginal?: boolean | undefined;
/**
* Read sourceMap information from the input
* file. Use it to filter the generated sourceMap information.
*/
inputSourceMap?: boolean | undefined;
};
57 changes: 47 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { SourceMapConsumer, SourceNode } from "source-map";
import {
deepEqual,
equal,
fail,
ok,
throws,
} from "node:assert/strict";
import { SourceNode } from "source-map";
import { fileURLToPath } from "node:url";
import fs from "node:fs/promises";
import path from "node:path";

const INVALID = "\uffff";
const START = "//#"; // c8: THIS file is not mapped.
const sourceMapRE = new RegExp(`^${START}\\s*sourceMappingURL\\s*=\\s*([^\r\n]+)`, "m");
const startRuleRE = /^\s*peg\$result = peg\$startRuleFunction\(\);/;
let counter = 0;

/**
Expand Down Expand Up @@ -258,6 +261,8 @@ function checkParserStarts(grammar, starts, modified, counts) {
* original.
* @prop {boolean} [noOriginal] Do not run tests on the original code, only
* on the generated code.
* @prop {boolean} [inputSourceMap] Read sourceMap information from the input
* file. Use it to filter the generated sourceMap information.
*/

/**
Expand Down Expand Up @@ -333,13 +338,47 @@ export async function testPeggy(grammarUrl, starts, opts) {
ok(grammarJs);
equal(typeof grammarJs, "string");

/** @type {SourceMapConsumer|null} */
let ism = null;
if (opts?.inputSourceMap) {
const m = grammarJs.match(sourceMapRE);
if (m) {
const map = m[1].startsWith("data:")
? await (await fetch(m[1])).json()
: JSON.parse(
await fs.readFile(
path.resolve(process.cwd(), path.dirname(grammarPath), m[1]),
"utf8"
)
);
ism = await new SourceMapConsumer(map);
}
}

// Approach: generate a new file next to the existing grammar file, with
// test code injected just before the parser runs. Source map information
// embedded in the new file will make coverage show up on the original file.
const src = new SourceNode();
let lineNum = 1;
for (const line of grammarJs.split(/(?<=\n)/)) {
if (/^\s*peg\$result = peg\$startRuleFunction\(\);/.test(line)) {
let sn = null;
if (ism) {
const pos = ism.originalPositionFor({
line: lineNum,
column: 0,
});
if (pos) {
sn = new SourceNode(pos.line, pos.column, pos.source, line);
}
}
if (!sn) {
sn = new SourceNode(lineNum, 0, grammarPath, line);
}
lineNum++;

if (sourceMapRE.test(line)) {
// No-op, never output sourcemapurl line.
} else if (startRuleRE.test(line)) {
src.add(`\
//#region Inserted by @peggyjs/coverage
let replacements = Object.create(null);
Expand Down Expand Up @@ -440,7 +479,7 @@ export async function testPeggy(grammarUrl, starts, opts) {
//#endregion

`);
src.add(new SourceNode(lineNum++, 0, grammarPath, line));
src.add(sn);
src.add(`
//#region Inserted by @peggyjs/coverage
for (const [name, orig] of Object.entries(replacements)) {
Expand All @@ -450,7 +489,7 @@ export async function testPeggy(grammarUrl, starts, opts) {
//#endregion
`);
} else {
src.add(new SourceNode(lineNum++, 0, grammarPath, line));
src.add(sn);
}
}

Expand All @@ -460,17 +499,15 @@ export async function testPeggy(grammarUrl, starts, opts) {
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");
}
} else if (!sm) {
console.error("WARNING: sourcemap disabled due to peg$debugger");
}
const start = "//#"; // c8: THIS file is not mapped.
if (sm) {
code += `
${start} sourceMappingURL=data:application/json;charset=utf-8;base64,${map}
${START} sourceMappingURL=data:application/json;charset=utf-8;base64,${map}
`;
}
ism?.destroy();
await fs.writeFile(modifiedPath, code);
try {
const agrammar = /** @type {Parser} */ (
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
},
"devDependencies": {
"@peggyjs/eslint-config": "6.0.0",
"@types/node": "22.15.3",
"@types/node": "22.15.14",
"c8": "10.1.3",
"eslint": "9.26.0",
"typedoc": "0.28.3",
"typedoc": "0.28.4",
"typescript": "5.8.3"
},
"packageManager": "[email protected]",
Expand Down
Loading