From 9e074f3ab8c2b796b61c2e733dc27594a8eb1975 Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Mon, 26 Oct 2026 14:15:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=B7=AF=E7=94=B1=E6=A3=80=E6=9F=A5):=20?= =?UTF-8?q?=E6=94=B9=E6=88=90=E5=8D=95=E5=BC=95=E5=8F=B7=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=20check:routes=20=E4=B8=80=E6=9D=A1=E8=B7=AF=E7=94=B1=E9=83=BD?= =?UTF-8?q?=E6=B2=A1=E6=9F=A5=EF=BC=8C=E5=8D=B4=E7=85=A7=E6=A0=B7=E6=8A=A5?= =?UTF-8?q?=E3=80=8C=E6=B2=A1=E6=9C=89=E9=81=AE=E8=94=BD=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 正则只认双引号,全仓换成单引号后匹配数变成 0。现在单双引号都认,查到 197 条。 Co-Authored-By: Claude Opus 5.5 --- apps/api/src/scripts/check-route-shadowing.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/api/src/scripts/check-route-shadowing.ts b/apps/api/src/scripts/check-route-shadowing.ts index 21f621d..407959b 100644 --- a/apps/api/src/scripts/check-route-shadowing.ts +++ b/apps/api/src/scripts/check-route-shadowing.ts @@ -16,8 +16,8 @@ * * 加路由时顺手跑一下,比事后靠人眼在 200 多条路由里看出顺序问题可靠。 * - * 局限:靠正则读源码,只认 `xxxRoutes.get("字面量", …)` 这种写法, - * 以及 `xxxRoutes.route("字面量", 子路由)` 的嵌套挂载(按挂载位置展开)。 + * 局限:靠正则读源码,只认 `xxxRoutes.get('字面量', …)` 这种写法(单双引号都认), + * 以及 `xxxRoutes.route('字面量', 子路由)` 的嵌套挂载(按挂载位置展开)。 * 动态拼出来的路径看不见 —— 但本仓库没有那种写法,加的时候请保持。 */ @@ -75,7 +75,10 @@ function collect(): Route[] { const text = readFileSync(file, 'utf8') // 直接注册的路由和嵌套挂载(`router.route("/", child)`)放在一起按出现位置排序: // 子路由挂在哪个位置,它的路由就在哪个位置参与匹配 - const pattern = new RegExp(`${router}\\.(get|post|put|delete|patch)\\(\\s*"([^"]+)"|${router}\\.route\\(\\s*"([^"]*)"\\s*,\\s*(\\w+)\\s*\\)`, 'g') + const pattern = new RegExp( + `${router}\\.(get|post|put|delete|patch)\\(\\s*['"]([^'"]+)['"]|${router}\\.route\\(\\s*['"]([^'"]*)['"]\\s*,\\s*(\\w+)\\s*\\)`, + 'g', + ) return [...text.matchAll(pattern)].flatMap((m) => { if (m[4]) return routesOf(m[4], prefix + m[3]!) return [ @@ -91,10 +94,10 @@ function collect(): Route[] { // 挂载顺序就是匹配顺序,所以必须按 index.ts 里出现的先后来摊平 const index = readFileSync(join(SRC, 'index.ts'), 'utf8') const adminIndex = readFileSync(join(SRC, 'routes/admin/index.ts'), 'utf8') - const adminMounts = [...adminIndex.matchAll(/\.route\(\s*"([^"]*)"\s*,\s*(\w+)\s*\)/g)] + const adminMounts = [...adminIndex.matchAll(/\.route\(\s*['"]([^'"]*)['"]\s*,\s*(\w+)\s*\)/g)] const all: Route[] = [] - for (const m of index.matchAll(/app\.route\(\s*"([^"]+)"\s*,\s*(\w+)\s*\)/g)) { + for (const m of index.matchAll(/app\.route\(\s*['"]([^'"]+)['"]\s*,\s*(\w+)\s*\)/g)) { const [, prefix, router] = m if (router === 'adminRoutes') { for (const a of adminMounts) all.push(...routesOf(a[2]!, prefix! + a[1]!))