Skip to content

Commit

Permalink
feat: support function
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jul 21, 2023
1 parent 5e6cac8 commit 6e3dc76
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 1,658 deletions.
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ const findRouting = async ({ AST, fileContent }: { AST: any; fileContent: string
node.arguments?.slice(1) ?? []
: // @ts-ignore
node.arguments?.slice(1, node.arguments.length - 1) ?? [];
const middlewares = middlewareArguments.map((arg: { start: number; end: number }) => {
const middlewares = middlewareArguments.map((arg: { type: string; start: number; end: number }) => {
// app.use(() => {});
if (arg.type === "ArrowFunctionExpression") {
return "Anonymous Function";
}
// app.use(function () {});
if (arg.type === "FunctionExpression") {
// @ts-ignore
return arg?.id?.name ?? "Anonymous Function";
}
return fileContent.slice(arg.start, arg.end);
});
return [
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/app.use-function/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from "express";
const app = Router();
app.post("/getEvents", async (req, res, next) => {});

app.use("/useEvents", async (req, res, next) => {
// use anonymous function
});

export = app;
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,22 @@ describe("app snapshot", function () {
}
]);
});
it("test app.use function", async () => {
const rootDir = path.join(__dirname, "fixtures/app.use-function");
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 | | | | |
| | post | /getEvents | | app.ts#L3-L4 |
| | use | /useEvents | Anonymous Function | app.ts#L6-L8 |`
);
});
});
Loading

0 comments on commit 6e3dc76

Please sign in to comment.