Skip to content

Commit a203214

Browse files
Jake ChampionJakeChampion
authored andcommitted
Report syntax errors prior to running wizer so that we can get custom error reporting
1 parent 1318e7d commit a203214

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

integration-tests/cli/invalid.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ test('should return non-zero exit code on syntax errors', async function (t) {
99
t.teardown(async function () {
1010
await cleanup();
1111
});
12-
await writeFile('./bin/index.js', '@')
13-
const { code } = await execute('node', cli);
14-
12+
await writeFile('./bin/index.js', '\n\n\n"hello";@')
13+
const { code, stdout, stderr } = await execute('node', cli);
14+
t.alike(stdout, []);
15+
t.alike(stderr, [
16+
'{{base}}/bin/index.js:4',
17+
'"hello";@',
18+
'^',
19+
'SyntaxError: Invalid or unexpected token'
20+
]);
21+
1522
t.is(code, 1);
1623
});

src/compileApplicationToWasm.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isFile } from "./isFile.js";
55
import { isFileOrDoesNotExist } from "./isFileOrDoesNotExist.js";
66
import wizer from "@jakechampion/wizer";
77
import { precompile } from "./precompile.js";
8+
import { containsSyntaxErrors } from "./containsSyntaxErrors.js";
89

910
export async function compileApplicationToWasm(input, output, wasmEngine) {
1011
try {
@@ -31,7 +32,7 @@ export async function compileApplicationToWasm(input, output, wasmEngine) {
3132
);
3233
process.exit(1);
3334
}
34-
// TODO: Enhancement - Check that input is valid JavaScript syntax before Wizening
35+
3536
try {
3637
if (!(await isFile(wasmEngine))) {
3738
console.error(
@@ -72,6 +73,10 @@ export async function compileApplicationToWasm(input, output, wasmEngine) {
7273
process.exit(1);
7374
}
7475

76+
if (containsSyntaxErrors(input)) {
77+
process.exit(1);
78+
}
79+
7580
let application = precompile(inputContents);
7681

7782
try {

src/containsSyntaxErrors.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { spawnSync } from "node:child_process";
2+
3+
// TODO: We should check that the syntax used is supported by our version of SpiderMonkey
4+
export function containsSyntaxErrors(input) {
5+
let nodeProcess = spawnSync(
6+
process.execPath,
7+
[
8+
"--check",
9+
input,
10+
],
11+
{
12+
stdio: [null, null, null],
13+
shell: true,
14+
encoding: "utf-8",
15+
}
16+
);
17+
if (nodeProcess.status === 0) {
18+
return false;
19+
} else {
20+
console.error(nodeProcess.stderr.split('\nSyntaxError: Invalid or unexpected token\n')[0] + '\nSyntaxError: Invalid or unexpected token\n')
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)