Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: illegal return statement parsing error #164

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
);

Expand Down
31 changes: 31 additions & 0 deletions test/illegalReturnStatement.spec.js
Original file line number Diff line number Diff line change
@@ -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 });
});
});