Skip to content

Commit

Permalink
validateNode as array in isRequire probe & update test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapacan committed Jan 1, 2024
1 parent cd5e7d4 commit adc936a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
39 changes: 19 additions & 20 deletions src/probes/isRequire.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,34 @@ import {
getCallExpressionArguments
} from "@nodesecure/estree-ast-utils";

function validateNode(node, option) {
function validateNodeRequire(node, { tracer }) {
const id = getCallExpressionIdentifier(node);
const argument = getCallExpressionArguments(node);

if (id === null) {
return [false];
}

const data = option.tracer.getDataFromIdentifier(id);
const data = tracer.getDataFromIdentifier(id);

if (data !== null && data.name === "require") {
return [
true,
data?.identifierOrMemberExpr ?? void 0
];
}
else if (id === "eval" && argument[0] === "require") {
return [
true,
option?.identifiersName[0].name ?? void 0
];
return [
data !== null && data.name === "require",
data?.identifierOrMemberExpr ?? void 0
];
}

function validateNodeEvalRequire(node, options) {
const id = getCallExpressionIdentifier(node);
const argument = getCallExpressionArguments(node);
if (id === null) {
return [false];
}

return [
false,
null
id === "eval" && argument[0] === "require",
options?.identifiersName[0]?.name ?? void 0
];
}


function main(node, options) {
const { analysis, data: calleeName } = options;
const { tracer } = analysis;
Expand Down Expand Up @@ -70,14 +69,14 @@ function main(node, options) {
}
break;

// require("http")
// require("http")
case "Literal":
if (arg.value !== "require") {
analysis.addDependency(arg.value, node.loc);
}
break;

// require(["ht", "tp"])
// require(["ht", "tp"])
case "ArrayExpression": {
const value = [...arrayExpressionToString(arg, { tracer })]
.join("")
Expand Down Expand Up @@ -184,5 +183,5 @@ function walkRequireCallExpression(nodeToWalk, tracer) {

export default {
name: "isRequire",
validateNode, main, breakOnMatch: true, breakGroup: "import"
validateNode: [validateNodeRequire, validateNodeEvalRequire], main, breakOnMatch: true, breakGroup: "import"
};
15 changes: 9 additions & 6 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ export function parseScript(str) {
}

function runOnProbes(node, analysis, probe) {
const [isMatching, data = null] = probe.validateNode(node, analysis);
const validationFns = Array.isArray(probe.validateNode) ?
probe.validateNode : [probe.validateNode];

if (isMatching) {
const result = probe.main(node, { analysis, data });

if (result === Symbol.for("skipWalk")) {
return "skip";
for (const validateNode of validationFns) {
const [isMatching, data = null] = validateNode(node, analysis);
if (isMatching) {
const result = probe.main(node, { analysis, data });
if (result === Symbol.for("skipWalk")) {
return "skip";
}
}
}

Expand Down

0 comments on commit adc936a

Please sign in to comment.