#!/usr/bin/env node /** * dead-modules 自证(OPEN-16)。判据对桶再导出的处理必须双向可证: * 正向:只被桶再导出、没有任何模块按名 import ⇒ 判死 * 反向:页面按名经桶 import / 直接 import ⇒ 判活 * 保守:export *、namespace import ⇒ 判活(宁可漏判,不可误删) * 做法:临时目录建 5 个默认根(脚本无参数运行需要它们存在),子进程跑真脚本, * 解析 `[dead]` 桶后的清单做断言。夹具不手写第二份路径判据——断言的是行为。 */ import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { execFileSync } from 'node:child_process'; import { fileURLToPath } from 'node:url'; const script = path.join(path.dirname(fileURLToPath(import.meta.url)), 'dead-modules.mjs'); const roots = ['src', 'scripts', 'e2e', 'config', 'prisma']; function makeFixture(files) { const dir = mkdtempSync(path.join(tmpdir(), 'dead-modules-selftest-')); for (const root of roots) mkdirSync(path.join(dir, root), { recursive: true }); for (const [rel, content] of Object.entries(files)) { const p = path.join(dir, rel); mkdirSync(path.dirname(p), { recursive: true }); writeFileSync(p, content); } return dir; } function run(dir) { try { return { code: 0, out: execFileSync(process.execPath, [script], { cwd: dir, encoding: 'utf8' }) }; } catch (e) { return { code: e.status ?? 1, out: `${e.stdout || ''}${e.stderr || ''}` }; } } function deadEntries(out) { const lines = out.split('\n'); const start = lines.findIndex((l) => l.startsWith('[dead]')); if (start < 0) return []; const list = []; for (let i = start + 1; i < lines.length; i++) { const line = lines[i]; if (!line.startsWith(' ')) break; list.push(line.trim().split(/\s+←/)[0].trim()); } return list; } const barrel = "export { Alive } from './alive';\nexport { Dead } from './dead';\n"; const components = { 'src/components/alive.tsx': 'export const Alive = () => null;\n', 'src/components/dead.tsx': 'export const Dead = () => null;\n', 'src/components/index.ts': barrel, }; const page = (body) => ({ 'src/app/page.tsx': `${body}\nexport default function Page() { return null; }\n` }); const cases = [ { name: '仅桶再导出 ⇒ 判死', files: { ...components, ...page("import { Alive } from '../components';") }, dead: ['src/components/dead.tsx'], alive: ['src/components/alive.tsx', 'src/components/index.ts'], }, { name: '页面按名经桶导入 ⇒ 判活', files: { ...components, ...page("import { Alive, Dead } from '../components';") }, dead: [], alive: ['src/components/dead.tsx'], }, { name: '直接导入 ⇒ 判活', files: { ...components, ...page("import { Dead } from '../components/dead';") }, dead: [], alive: ['src/components/dead.tsx'], }, { name: 'export * ⇒ 保守判活', files: { 'src/components/star.tsx': 'export const Star = () => null;\n', 'src/components/index.ts': "export * from './star';\n", ...page("import { Star } from '../components';"), }, dead: [], alive: ['src/components/star.tsx'], }, { name: '命名空间导入 ⇒ 整桶保守判活', files: { ...components, ...page("import * as UI from '../components';") }, dead: [], alive: ['src/components/dead.tsx'], }, { name: '门面 → 子桶链 ⇒ 按名解析到落点(注释里的伪导入不算)', files: { // 注释里的 import 必须被剥离:不剥离会让 Dead 被当成活码(constants.ts 的真实事故形态) 'src/lib/constants.ts': "// 示例:import { Dead } from './dead';\nexport { Alive } from './constants/index';\n", 'src/lib/constants/index.ts': "export { Alive } from './alive';\nexport { Dead } from './dead';\n", 'src/lib/constants/alive.ts': 'export const Alive = 1;\n', 'src/lib/constants/dead.ts': 'export const Dead = 2;\n', 'src/app/page.tsx': "import { Alive } from '@/lib/constants';\nexport default function Page() { return null; }\n", }, dead: ['src/lib/constants/dead.ts'], alive: ['src/lib/constants/alive.ts', 'src/lib/constants.ts', 'src/lib/constants/index.ts'], }, ]; let failed = 0; for (const c of cases) { const dir = makeFixture(c.files); try { const { code, out } = run(dir); const dead = deadEntries(out); const problems = []; if (code !== 0) problems.push(`退出码 ${code}(期望 0)`); for (const f of c.dead) if (!dead.includes(f)) problems.push(`应判死未判死:${f}`); for (const f of c.alive) if (dead.includes(f)) problems.push(`误判死:${f}`); if (problems.length) { failed++; console.log(`❌ ${c.name}`); for (const p of problems) console.log(` - ${p}`); console.log(` 实测 dead: ${dead.join(', ') || '(空)'}`); } else { console.log(`✅ ${c.name}`); } } finally { rmSync(dir, { recursive: true, force: true }); } } if (failed) { console.error(`\nSELFTEST FAILED:${failed}/${cases.length} 个情景未通过`); process.exit(1); } console.log(`\nSELFTEST PASSED:${cases.length}/${cases.length}`);