Skip to content

Commit 99fd4fe

Browse files
authored
refactor(probes): isFetch detect fetch re-assigment (#380)
1 parent 8f64e95 commit 99fd4fe

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

.changeset/public-coats-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/js-x-ray": minor
3+
---
4+
5+
refactor(probes): isFetch detect fetch re-assigment

workspaces/js-x-ray/src/probes/isFetch.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@ import type { ESTree } from "meriyah";
66
import { SourceFile } from "../SourceFile.js";
77

88
function validateNode(
9-
node: ESTree.Node
9+
node: ESTree.Node,
10+
{ tracer }: SourceFile
1011
): [boolean, any?] {
1112
const id = getCallExpressionIdentifier(node);
1213

13-
return [id === "fetch"];
14+
if (id === null) {
15+
return [false];
16+
}
17+
18+
const data = tracer.getDataFromIdentifier(id);
19+
20+
return [data !== null && data.identifierOrMemberExpr === "fetch"];
21+
}
22+
23+
function initialize(sourceFile: SourceFile) {
24+
sourceFile.tracer.trace("fetch", { followConsecutiveAssignment: true });
1425
}
1526

1627
function main(
@@ -23,6 +34,7 @@ function main(
2334
export default {
2435
name: "isFetch",
2536
validateNode,
37+
initialize,
2638
main,
2739
breakOnMatch: false
2840
};

workspaces/js-x-ray/test/probes/isFetch.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ test("it should detect native fetch", () => {
1212
assert.ok(flags.has("fetch"));
1313
assert.strictEqual(flags.size, 1);
1414
});
15+
16+
test("it should detect a re-assigned native fetch", () => {
17+
const code = `const fetchBis = fetch
18+
await fetchBis(url);
19+
`;
20+
const { flags } = new AstAnalyser().analyse(code);
21+
22+
assert.ok(flags.has("fetch"));
23+
assert.strictEqual(flags.size, 1);
24+
});

0 commit comments

Comments
 (0)