10.3 基于 Babel AST 的依赖图架构守卫

上一节 10.2 我们靠 package.json 的 "exports" 挡住了 import "nano-solid/dist/reactive.js" 这样的裸包名深层导入。但单靠 exports 还有两个盲区:

  1. 仓内相对路径绕过:在 examples/todo-app/App.tsx 里写 import { createSignal } from "../../src/reactive.ts",Node 和 TypeScript 都会放行,因为它是合法的文件系统相对路径;
  2. 框架内部分层腐化与循环依赖:如果某天有人不小心在浏览器运行时 src/dom.ts 里引入了 src/compiler/ 或 @babel/parser,或者在模块间悄悄形成了 A -> B -> C -> A 的循环依赖,等到线上包体积暴涨或运行时出现 ReferenceError: Cannot access before initialization 时往往已经积重难返。

好消息是,第 9 章我们已经把整套 Babel 8 AST 工具链(@babel/parser、@babel/traverse、@babel/types)装进了仓库。这一节我们顺手把它升级成真正的静态依赖图架构守卫(tools/dependency-graph.ts),用自动化测试把模块边界永久钉死。

1. 为什么架构测试必须用 AST,而不能用正则表达式?

很多项目图省事,会用类似 /from\s+["']([^"']+)["']/g 的正则去扫导入路径。这种写法在真实代码库里很快就会翻车:

我们把这套工具放在独立的 tools/ 目录(并把 "tools/**/*.ts" 加入根 tsconfig.json 检查),而不放进 src/,原因同样明确:tsconfig.build.json 只编译 src/**/*.ts,仓内工程守卫工具绝不应该混进对外发布的 dist/ 包产物里。

2. 用 Babel 8 AST 提取全部静态依赖边

在 extractModuleEdges(source, fromFile) 中,我们开启 plugins: ["jsx", "typescript"] 解析 AST,只监听四类标准语法节点:

AST 节点类型覆盖的源码写法kindtypeOnly 判定规则
ImportDeclarationimport ... from "x"、import "x"、import type ..."import"node.importKind === "type",或非空 specifiers 全部都是 importKind === "type" 的 ImportSpecifier
ExportNamedDeclaration(且 node.source 非空)export { a } from "x"、export type { T } from "x""export-named"node.exportKind === "type",或非空 specifiers 全部都是 exportKind === "type" 的 ExportSpecifier
ExportAllDeclarationexport * from "x"、export type * from "x""export-all"node.exportKind === "type"
ImportExpression(source 为 StringLiteral,Babel 8 对齐 ESTree)import("./lazy.tsx")"dynamic-import"固定为 false(动态加载在运行时求值)

对于 ./ 或 ../ 开头的相对导入,调用 resolveRelativeModule(fromFile, specifier) 统一归一化为相对仓库根的正斜杠路径(例如在 examples/todo-app/App.tsx 里写 ../../src/reactive.ts,会立刻还原为 src/reactive.ts),后续规则和环检测都只需面对干净的规范化路径。

3. 三色 DFS 环检测:为什么不能只用一个 visited 集合?

检测有向图里的循环依赖时,初学者常犯的错误是只维护一个 visited = new Set<string>(),再次碰到已访问节点就报“发现循环依赖”。这会把最正常的 菱形无环图(Diamond DAG) 误报成环:

flowchart LR
  subgraph DAG["正常菱形 DAG(无环)"]
    A["a.ts"] --> B["b.ts"]
    A --> C["c.ts"]
    B --> D["d.ts"]
    C --> D
  end
  subgraph Cycle["真实有向环(必须报错)"]
    X["a.ts"] --> Y["b.ts"]
    Y --> Z["c.ts"]
    Z --> X
  end

在左侧菱形图里,DFS 先走 a -> b -> d 把 d 标记为已访问并回溯;随后走 a -> c -> d 时再次遇到 d,但 d 早就退出当前递归路径了,这里根本没有环!

标准解法是三色深度优先搜索(3-Color DFS):

最后,为了避免同一条环从不同起点出发被重复报告(例如 c -> a -> b -> c 和 a -> b -> c -> a 其实是同一个环),我们把环节点旋转到从字典序最小节点出发,再按字符串键去重排序,保证输出 100% 稳定。

4. 为真实仓库立规矩,并为第 11 章留好插槽

目前我们的 nano-solid 生产代码已经天然形成一张干净的分层无环图:

flowchart TD
  App["examples/todo-app/*(仅允许导入公开包名 nano-solid)"] --> Index["src/index.ts(公共 API 出口)"]
  Index --> Dom["src/dom.ts"]
  Index --> CF["src/control-flow.ts"]
  Index --> Jsx["src/jsx-runtime.ts"]
  Index --> Reactive["src/reactive.ts(零依赖响应式内核)"]
  Jsx --> Dom
  Dom --> Reactive
  CF --> Reactive
  CR["src/compiler-runtime.ts"] --> Dom
  CR --> Jsx
  CR --> Reactive
  Vite["src/vite.ts"] --> Plugin["src/compiler/vite-plugin.ts"]
  Plugin --> Transform["src/compiler/transform.ts(@babel/*)"]

我们在 tools/architecture.test.ts 中写下了四条声明式规则,外加全仓零循环依赖检查:

到了第 11 章,当我们把 Todo 应用拆分成领域类型、持久化接口、Store、展示组件和适配器层时,只需往这份规则数组里追加几条声明式规则,整套 AST 依赖图守卫立刻就能继续护航应用架构!

解答 展开 收起

在 tools/dependency-graph.ts 中依次补齐四个核心函数:

  1. extractModuleEdges:用 @babel/parser 解析 TSX/TS 为 AST,遍历 ImportDeclaration、带 source 的 ExportNamedDeclaration、ExportAllDeclaration 与 ImportExpression(Babel 8 对齐 ESTree,动态 import("./...") 是独立的 ImportExpression 节点),准确判断语句级和修饰符级的 typeOnly,并调用 resolveRelativeModule 归一化仓内相对路径。
  2. buildDependencyGraph:依次读取每个文件并调用 extractModuleEdges 填充 Map<string, DependencyEdge[]>。
  3. findCycles:按字典序遍历节点执行三色 DFS(0 未访问、1 递归栈中、2 已完成),只有遇到灰色节点才从栈中截出闭环,并旋转到字典序最小节点出发去重排序:
function dfs(node: string): void {
  color.set(node, 1);
  stackIndex.set(node, stack.length);
  stack.push(node);

  for (const edge of graph.get(node) ?? []) {
    if (!includeTypeOnly && edge.typeOnly) continue;
    const target = edge.resolvedTo;
    if (!target || !graph.has(target)) continue;

    const state = color.get(target) ?? 0;
    if (state === 1) {
      const startIdx = stackIndex.get(target);
      if (startIdx !== undefined) {
        const canonical = canonicalizeCycle(stack.slice(startIdx));
        uniqueCycles.set(canonical.join(" -> "), canonical);
      }
    } else if (state === 0) {
      dfs(target);
    }
  }

  stack.pop();
  stackIndex.delete(node);
  color.set(node, 2);
}
  1. checkArchitectureRules:按来源文件字典序遍历匹配规则和每条依赖边,汇总非空违例消息并按 from、line、rule 稳定排序。

删掉 oxlint-disable-next-line 注释,去掉 tools/architecture.test.ts 中 5 条测试的 .skip,运行 pnpm format && pnpm check && pnpm test:run,全部 154 条测试都会通过。