Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 12, 2023
1 parent dd0c75c commit c2abcbe
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/js/benchmark-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/test-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/vendor/peggy/peggy.min.js

Large diffs are not rendered by default.

33 changes: 21 additions & 12 deletions lib/compiler/asts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ const visitor = require("./visitor");
* in the order [...a, ...b].
*
* @template T
* @param {T | T[] | undefined} a
* @param {T | T[] | undefined} b
* @returns {T | T[] | undefined}
* @param {T | T[]} a
* @param {T | T[]} b
* @returns {T | T[]}
*/
function combinePossibleArrays(a, b) {
if (a) {
if (!b) {
return a;
}
const aa = Array.isArray(a) ? a : [a];
const bb = Array.isArray(b) ? b : [b];
return aa.concat(bb);
}
return b;
const aa = Array.isArray(a) ? a : [a];
const bb = Array.isArray(b) ? b : [b];
return aa.concat(bb);
}

// AST utilities.
const asts = {
/**
* Find the rule with the given name, if it exists.
*
* @param {PEG.ast.Grammar} ast
* @param {string} name
* @returns {PEG.ast.Rule | undefined}
*/
findRule(ast, name) {
for (let i = 0; i < ast.rules.length; i++) {
if (ast.rules[i].name === name) {
Expand All @@ -35,6 +36,14 @@ const asts = {
return undefined;
},

/**
* Find the index of the rule with the given name, if it exists.
* Otherwise returns -1.
*
* @param {PEG.ast.Grammar} ast
* @param {string} name
* @returns {number}
*/
indexOfRule(ast, name) {
for (let i = 0; i < ast.rules.length; i++) {
if (ast.rules[i].name === name) {
Expand Down
6 changes: 4 additions & 2 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,12 @@ function generateJS(ast, options) {
if (Array.isArray(ast.topLevelInitializer)) {
for (const tli of ast.topLevelInitializer) {
parts.push(ast2SourceNode(tli));
parts.push("");
}
} else {
parts.push(ast2SourceNode(ast.topLevelInitializer));
parts.push("");
}
parts.push("");
}

parts.push(
Expand Down Expand Up @@ -1292,11 +1293,12 @@ function generateJS(ast, options) {
if (Array.isArray(ast.initializer)) {
for (const init of ast.initializer) {
parts.push(ast2SourceNode(init));
parts.push("");
}
} else {
parts.push(ast2SourceNode(ast.initializer));
parts.push("");
}
parts.push("");
}

parts.push(
Expand Down
18 changes: 18 additions & 0 deletions test/cli/fixtures/imports1.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{
// imports1 topLevelInitializer import
import {
basename,
dirname,
parse
} from "path"
// imports1 topLevelInitializer code
const tli1 = dirname(__filename);
}}

{
// imports1 initializer
const pf = path.parse(__filename);
}
full
= '/'
/ f:('/' segment )+ '/'? { return parse(f) }
12 changes: 12 additions & 0 deletions test/cli/fixtures/imports2.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{
// imports2 topLevelInitializer import
import fs from 'node:fs';

// imports2 topLevelInitializer code
const tli2 = 'top';
}}
{
// imports2 initializer
const contents = fs.readFileSync(__filename);
}
segment = [^/]
13 changes: 13 additions & 0 deletions test/cli/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,19 @@ Options:
})).resolves.toMatch("DefaultTracer: peg$DefaultTracer");
});

it("handles multiple files", async() => {
const input1 = path.join(__dirname, "fixtures", "imports1.peggy");
const input2 = path.join(__dirname, "fixtures", "imports2.peggy");
const out = path.join(__dirname, "fixtures", "imports1.js");

await expect(exec({
args: [input1, input2],
exitCode: 0,
})).resolves.toBe("");

fs.unlinkSync(out);
});

describe("handles source map", () => {
describe("with default name without --output", () => {
const sourceMap = path.resolve(__dirname, "..", "..", "source.map");
Expand Down

0 comments on commit c2abcbe

Please sign in to comment.