Closed remorses closed 3 years ago
Ok, i found you can do this
function tryParseImports(source, filename = '') {
try {
const [imports] = parse(source)
return imports
} catch (e) {
const line = source.slice(0, e.idx).split('\n').length
throw new Error(
`cannot parse ES imports in '${filename}', code is:\n${source
.split('\n')
.slice(abs(line - 1), line + 1)
.join(' \n')}\n`,
)
}
}
If you don't add the try catch, you can just do parse(source, filename)
and it will give you an error stack too.
Having those values makes it easier to identify the made the lexer fail, using code like