diff --git a/index.js b/index.js index ec873a0..6682834 100644 --- a/index.js +++ b/index.js @@ -122,15 +122,19 @@ function parseScriptExtended(strToAnalyze, options = {}) { return body; } catch (error) { + const isIllegalReturn = error.description.includes("Illegal return statement"); + if (error.name === "SyntaxError" && ( error.description.includes("The import keyword") || - error.description.includes("The export keyword") + error.description.includes("The export keyword") || + isIllegalReturn )) { const { body } = meriyah.parseScript( cleanedStrToAnalyze, { ...kMeriyahDefaultOptions, - module: true + module: true, + globalReturn: isIllegalReturn } ); diff --git a/test/illegalReturnStatement.spec.js b/test/illegalReturnStatement.spec.js new file mode 100644 index 0000000..84e67cf --- /dev/null +++ b/test/illegalReturnStatement.spec.js @@ -0,0 +1,31 @@ +// Import Node.js Dependencies +import { test } from "node:test"; +import assert from "node:assert"; + +// Import Internal Dependencies +import { runASTAnalysis } from "../index.js"; + +/** + * @see https://github.com/NodeSecure/js-x-ray/issues/163 + */ +// CONSTANTS +const kIncriminedCodeSample = ` +const argv = process.argv.slice(2); + +function foobar() { + console.log("foobar"); +} + +if (!argv.length) { + return foobar(); +} +`; + +test("it should not throw error whatever module is true or false", () => { + assert.doesNotThrow(() => { + runASTAnalysis(kIncriminedCodeSample, { module: false }); + }); + assert.doesNotThrow(() => { + runASTAnalysis(kIncriminedCodeSample, { module: true }); + }); +});