UNPKG

@nodesecure/js-x-ray

Version:
38 lines 1.25 kB
// Import Third-party Dependencies import { parseScript } from "meriyah"; // CONSTANTS const kParsingOptions = { next: true, loc: true, raw: true, jsx: true }; export class JsSourceParser { parse(source, options = {}) { const { isEcmaScriptModule } = options; try { const { body } = parseScript(source, { ...kParsingOptions, module: isEcmaScriptModule, globalReturn: !isEcmaScriptModule }); return body; } catch (error) { const syntaxError = error; const isIllegalReturn = syntaxError.description.includes("Illegal return statement"); if (syntaxError.name === "SyntaxError" && (syntaxError.description.includes("The import keyword") || syntaxError.description.includes("The export keyword") || isIllegalReturn)) { const { body } = parseScript(source, { ...kParsingOptions, module: true, globalReturn: isIllegalReturn }); return body; } throw error; } } } //# sourceMappingURL=JsSourceParser.js.map