Skip to content

Commit

Permalink
Require grammarSource when generating source maps. Fixes #285.
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Jun 10, 2022
1 parent 46878cd commit 715e4f7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Released: 2022-05-28
definition of SyntaxError, from @cmfcmf
- [#220](https://github.com/peggyjs/peggy/issues/220): Fix rollup warnings,
from @hildjj
- [#285](https://github.com/peggyjs/peggy/issues/285): Work around source-map
bug by throwing an exception if no grammarSource is given when generating
source maps, from @hildjj.

1.2.0
-----
Expand Down
17 changes: 8 additions & 9 deletions docs/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,14 @@ <h3 id="generating-a-parser-javascript-api">JavaScript API</h3>
</ul>
<p>(default: <code>"parser"</code>)</p>
<blockquote>
<p><strong>Note</strong>: because of bug <a
href="https://github.com/mozilla/source-map/issues/444">source-map/444</a>
you should also set <code>grammarSource</code> to a not-empty string if
you set this value to <code>"source-and-map"</code> or
<code>"source-with-inline-map"</code>. The path should be relative to
the location where the generated parser code will be stored. For
example, if you are generating <code>lib/parser.js</code> from
<code>src/parser.peggy</code>, then your options should be:
<code>{ grammarSource: "../src/parser.peggy" }</code></p>
<p><strong>Note</strong>: You should also set <code>grammarSource</code>
to a not-empty string if you set this value to
<code>"source-and-map"</code> or
<code>"source-with-inline-map"</code>. The path should be relative to
the location where the generated parser code will be stored. For
example, if you are generating <code>lib/parser.js</code> from
<code>src/parser.peggy</code>, then your options should be:
<code>{ grammarSource: "../src/parser.peggy" }</code></p>
</blockquote>
</dd>

Expand Down
6 changes: 6 additions & 0 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ const compiler = {
}
}
}
if (((options.output === "source-and-map")
|| (options.output === "source-with-inline-map"))
&& ((typeof options.grammarSource !== "string")
|| (options.grammarSource.length === 0))) {
throw new Error("Must provide grammarSource in order to generate source maps");
}

const session = new Session(options);
Object.keys(passes).forEach(stage => {
Expand Down
27 changes: 21 additions & 6 deletions test/types/peg.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,22 @@ describe("peg.d.ts", () => {
const p1 = peggy.generate(src, { output: "source" });
expectType<string>(p1);

const p2 = peggy.generate(src, { output: "source-and-map" });
const p2 = peggy.generate(src, {
output: "source-and-map",
grammarSource: "src.peggy",
});
expectType<SourceNode>(p2);

const p3 = peggy.generate(src, { output: true as boolean ? "source-and-map" : "source" });
const p3 = peggy.generate(src, {
output: true as boolean ? "source-and-map" : "source",
grammarSource: "src.peggy",
});
expectType<string | SourceNode>(p3);

const p4 = peggy.generate(src, { output: "source-with-inline-map" });
const p4 = peggy.generate(src, {
output: "source-with-inline-map",
grammarSource: "src.peggy",
});
expectType<string>(p4);
});

Expand All @@ -134,21 +143,27 @@ describe("peg.d.ts", () => {
const p2 = peggy.compiler.compile(
ast,
peggy.compiler.passes,
{ output: "source-and-map" }
{ output: "source-and-map", grammarSource: "src.peggy" }
);
expectType<SourceNode>(p2);

const p3 = peggy.compiler.compile(
ast,
peggy.compiler.passes,
{ output: true as boolean ? "source-and-map" : "source" }
{
output: true as boolean ? "source-and-map" : "source",
grammarSource: "src.peggy",
}
);
expectType<string | SourceNode>(p3);

const p4 = peggy.compiler.compile(
ast,
peggy.compiler.passes,
{ output: "source-with-inline-map" }
{
output: "source-with-inline-map",
grammarSource: "src.peggy",
}
);
expectType<string>(p4);
});
Expand Down
2 changes: 2 additions & 0 deletions test/unit/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe("Peggy compiler", () => {
if (typeof TextEncoder === "function") {
expect(compiler.compile(ast, compiler.passes, {
output: "source-with-inline-map",
grammarSource: "src.peggy",
})).to.match(
/^\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,/m
);
Expand All @@ -50,6 +51,7 @@ describe("Peggy compiler", () => {
delete globalThis.TextEncoder;
expect(() => compiler.compile(ast, compiler.passes, {
output: "source-with-inline-map",
grammarSource: "src.peggy",
})).to.throw("TextEncoder is not supported by this platform");
globalThis.TextEncoder = TE;
}
Expand Down

0 comments on commit 715e4f7

Please sign in to comment.