Skip to content

Commit

Permalink
fix: res.set check
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jul 21, 2023
1 parent 38d3ece commit 807732b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ const findRouting = async ({ AST, fileContent }: { AST: any; fileContent: string
const results = query(AST, selector);
// router.{get,post,delete,put,use}
return results.flatMap((node: any) => {
// TODO: improve query to avoid this check
// res.set("X-Content-Type-Options", req.get("test")));
if (node.callee.property.name !== method) {
return [];
}
// single argument should be ignored
// req.get("host"); it is not routing
if (node.arguments.length === 1) {
return [];
}
const pathValue =
node.arguments[0] !== undefined &&
node.arguments[0].type === "StringLiteral" &&
node.arguments[0].value;
if (!pathValue) {
return []; // skip: it will only includes middleware
}
// single argument should be ignored
// req.get("host"); it is not routing
if (node.arguments.length === 1) {
return [];
}
const middlewareArguments =
method === "use"
? // @ts-ignore
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/res.set-get-but-it-is-not-router/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express";

const app = Router();

app.get("/get", (req, res) => {
res.set("key", req.get("host"));
});
export default app;
19 changes: 18 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe("app snapshot", function () {
| | use | /useEvents | Anonymous Function | app.ts#L5-L7 |`
);
});
it("should ignore req.get() for getting paramter", async () => {
it("should ignore req.get() for getting parameter", async () => {
const rootDir = path.join(__dirname, "fixtures/req.get-but-it-is-not-router");
const mdResults = await analyzeDependencies({
filePaths: await globby(["**/*.ts"], { cwd: rootDir }),
Expand All @@ -271,6 +271,23 @@ describe("app snapshot", function () {
| File | Method | Routing | Middlewares | FilePath |
| ------ | ------ | ------- | ----------- | ------------ |
| app.ts | | | | |
| | get | /get | | app.ts#L5-L7 |`
);
});
it("should ignore res.set(key, req.get()) for getting parameter", async () => {
const rootDir = path.join(__dirname, "fixtures/res.set-get-but-it-is-not-router");
const mdResults = await analyzeDependencies({
filePaths: await globby(["**/*.ts"], { cwd: rootDir }),
cwd: rootDir,
rootBaseUrl: "",
outputFormat: "markdown"
});
assert.strictEqual(
mdResults,
`\
| File | Method | Routing | Middlewares | FilePath |
| ------ | ------ | ------- | ----------- | ------------ |
| app.ts | | | | |
| | get | /get | | app.ts#L5-L7 |`
);
});
Expand Down

0 comments on commit 807732b

Please sign in to comment.