Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/public-coats-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/js-x-ray": minor
---

refactor(probes): isFetch detect fetch re-assigment
16 changes: 14 additions & 2 deletions workspaces/js-x-ray/src/probes/isFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ import type { ESTree } from "meriyah";
import { SourceFile } from "../SourceFile.js";

function validateNode(
node: ESTree.Node
node: ESTree.Node,
{ tracer }: SourceFile
): [boolean, any?] {
const id = getCallExpressionIdentifier(node);

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

const data = tracer.getDataFromIdentifier(id);

return [data !== null && data.identifierOrMemberExpr === "fetch"];
}

function initialize(sourceFile: SourceFile) {
sourceFile.tracer.trace("fetch", { followConsecutiveAssignment: true });
}

function main(
Expand All @@ -23,6 +34,7 @@ function main(
export default {
name: "isFetch",
validateNode,
initialize,
main,
breakOnMatch: false
};
10 changes: 10 additions & 0 deletions workspaces/js-x-ray/test/probes/isFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ test("it should detect native fetch", () => {
assert.ok(flags.has("fetch"));
assert.strictEqual(flags.size, 1);
});

test("it should detect a re-assigned native fetch", () => {
const code = `const fetchBis = fetch
await fetchBis(url);
`;
const { flags } = new AstAnalyser().analyse(code);

assert.ok(flags.has("fetch"));
assert.strictEqual(flags.size, 1);
});