@nodesecure/js-x-ray
Version:
JavaScript AST XRay analysis
38 lines • 1.25 kB
JavaScript
// 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