Skip to content

Commit

Permalink
Merge branch 'bandaloo-never-token'
Browse files Browse the repository at this point in the history
  • Loading branch information
kach committed Nov 29, 2020
2 parents b6909ff + 751cf45 commit f7a7c42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}

options = options || {};
tabulated = lines.map(function addIndent(line, i) {
var tabulated = lines.map(function addIndent(line, i) {
var shouldIndent = true;

if(i == 0 && !options.indentFirst) {
Expand Down Expand Up @@ -193,7 +193,7 @@
output += parser.customTokens.map(function (token) { return "declare var " + token + ": any;\n" }).join("")
output += parser.body.join('\n');
output += "\n";
output += "interface NearleyToken {";
output += "interface NearleyToken {\n";
output += " value: any;\n";
output += " [key: string]: any;\n";
output += "};\n";
Expand All @@ -202,7 +202,7 @@
output += " reset: (chunk: string, info: any) => void;\n";
output += " next: () => NearleyToken | undefined;\n";
output += " save: () => any;\n";
output += " formatError: (token: NearleyToken) => string;\n";
output += " formatError: (token: never) => string;\n";
output += " has: (tokenType: string) => boolean;\n";
output += "};\n";
output += "\n";
Expand Down
22 changes: 16 additions & 6 deletions test/nearleyc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ function prettyPrint(grammar) {
return grammar.rules.map(g => g.toString())
}

function typeScriptCheck(isStrict) {
const {outPath, stdout, stderr} = externalNearleyc("grammars/typescript-test.ne", ".ts");
expect(stderr).toBe("");
expect(stdout).toBe("");
const spawnSync = sh(`tsc ${isStrict ? "--strict" : ""} ${outPath}.ts`);
expect(spawnSync.stdout).toBe(""); // type errors get logged to stdout, not stderr
const grammar = nearley.Grammar.fromCompiled(require(`./${outPath}.js`).default);
expect(parse(grammar, "<123>")).toEqual([ [ '<', '123', '>' ] ]);
}


describe("bin/nearleyc", function() {
after(cleanup)
Expand Down Expand Up @@ -61,12 +71,12 @@ describe("bin/nearleyc", function() {

it('builds for TypeScript', function() {
this.timeout(10000); // It takes a while to run tsc!
const {outPath, stdout, stderr} = externalNearleyc("grammars/typescript-test.ne", ".ts");
expect(stderr).toBe("");
expect(stdout).toBe("");
sh(`tsc ${outPath}.ts`);
const grammar = nearley.Grammar.fromCompiled(require(`./${outPath}.js`).default);
expect(parse(grammar, "<123>")).toEqual([ [ '<', '123', '>' ] ]);
typeScriptCheck(false);
});

it('builds for TypeScript with `--strict` with no type errors', function() {
this.timeout(10000);
typeScriptCheck(true);
});

it('builds modules in folders', function() {
Expand Down

0 comments on commit f7a7c42

Please sign in to comment.