fix: throw better error when JS parse fails

This should help debugging challenges with invalid syntax.
This commit is contained in:
Oliver Eyton-Williams
2021-10-27 16:08:44 +02:00
committed by Mrugesh Mohapatra
parent da9e6ad0c1
commit 3e5cffba0f

View File

@ -6,8 +6,14 @@ const parser = acorn.Parser;
function extractComments(js) {
let comments = [];
const file = { data: {} };
parser.parse(js, { onComment: comments, ecmaVersion: 2020 });
try {
parser.parse(js, { onComment: comments, ecmaVersion: 2020 });
} catch {
throw Error(`extract-js-comments could not parse the code below, this challenge have invalid syntax:
${js}
`);
}
comments
.map(({ value }) => value.trim())
.forEach(comment => commentToData(file, comment));