@linkdesk/plugin-sdk 0.1.47 → 0.1.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linkdesk/plugin-sdk",
3
- "version": "0.1.47",
3
+ "version": "0.1.48",
4
4
  "description": "@linkdesk/plugin-sdk——LinkDesk 插件作者 SDK(对标 @types/vscode):window.linkdesk.* 类型 + 一键 build(.linkdesk-plugin) + dev 宿主 + plugin.json 验证 + eslint 门禁(E6#54d)。作者 npm i 后零壳源码依赖。",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,12 +18,18 @@
18
18
  "types": "./dist/vitest-setup.d.ts",
19
19
  "default": "./dist/vitest-setup.js"
20
20
  },
21
+ "./test-audit": {
22
+ "types": "./test-audit.d.ts",
23
+ "default": "./test-audit.mjs"
24
+ },
21
25
  "./package.json": "./package.json"
22
26
  },
23
27
  "files": [
24
28
  "dist",
25
29
  "schemas",
26
- "dev-host"
30
+ "dev-host",
31
+ "test-audit.mjs",
32
+ "test-audit.d.ts"
27
33
  ],
28
34
  "sideEffects": false,
29
35
  "bin": {
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @linkdesk/plugin-sdk/test-audit 的类型声明(实现 = 同目录 test-audit.mjs,免构建直发)。
3
+ * 口径与判据说明见 .mjs 头注——那里是单一真源,这里不重述。
4
+ */
5
+
6
+ export interface AuditedUnit {
7
+ /** 仓相对路径(POSIX 斜杠,不含 .ts 后缀) */
8
+ unit: string;
9
+ /** 判据来源标注(basename-miss / reference-miss) */
10
+ evidence: string[];
11
+ }
12
+
13
+ export interface RepoAudit {
14
+ /** logic = 有纯逻辑单元;exempt = 纯声明式/零逻辑(豁免理由见 exempt);third-party = 第三方仓(消费方只读报出) */
15
+ kind: "logic" | "exempt" | "third-party";
16
+ exempt: string | null;
17
+ prodLines: number;
18
+ testLines: number;
19
+ testFiles: number;
20
+ /** 宽口径单元数 */
21
+ wideUnits: number;
22
+ /** 纯逻辑单元数(严口径) */
23
+ logicUnits: number;
24
+ zeroTest: AuditedUnit[];
25
+ coveredBasenameOnly: string[];
26
+ coveredReferenceOnly: string[];
27
+ coveredBoth: string[];
28
+ }
29
+
30
+ export declare const AUDIT_CALIBER: Record<string, string>;
31
+
32
+ export declare function analyzeRepo(repoDir: string, opts?: { official?: boolean }): RepoAudit;
package/test-audit.mjs ADDED
@@ -0,0 +1,171 @@
1
+ /**
2
+ * @linkdesk/plugin-sdk/test-audit——插件仓「测试覆盖」结构审计核心(单一真源)。
3
+ *
4
+ * ── 谁在用同一份 ──
5
+ * · 官方覆盖尺(壳仓 `scripts/audit-plugin-tests.mjs`,19 仓只读盘点、只报不拦);
6
+ * · 插件仓 CI 第六段(脚手架模板 `scripts/ci-verify.mjs` ⑥,E6#155 步二起**判红**——2026-09-26 用户拍板)。
7
+ * ⛔ 别在任何一处复制这套判据——判据漂移 = 两把尺子打架,假红会让真红失效。
8
+ *
9
+ * ── 口径(与插件测试覆盖层 00-整理档案 §〇b 同源;立案 2026-09-25,本核心 2026-09-26 落地)──
10
+ * · 生产行 = src 下全部 .ts / .tsx / .css(任意深度)去测试文件,逐文件按行切分求和(含末行换行)。
11
+ * · **纯逻辑单元** = src 下任意深度的 .ts 去 .d.ts / 测试文件 / barrel(`types.ts`·`index.ts`·`constants.ts`)
12
+ * 再减 `use*` hook / 内容含 `window.linkdesk` / 含 React 导入(.tsx 本就不在口径内)。
13
+ * · 覆盖命中(两条任一即算):① 同名测试文件(基名 = 单元名 ∨ 所在目录名);
14
+ * ② 任一测试文件的模块说明符引用到它(`from`/`import()`/`require()`/`vi.mock*`,含目录桶 index)。
15
+ * ⚠️ 已知盲区:只跟一跳——`A ← B ← 测试` 的传递覆盖看不见(首跑唯一官方命中即此类「一跳传递假红」)。
16
+ * ⇒ **零测报出 ≠ 判死**:消费方判红信息必须带「先核覆盖再补测」的提示,别逼作者写无意义测试。
17
+ *
18
+ * ── 豁免 ──
19
+ * 纯声明式仓(无 `src/`)与零逻辑空壳(entry 头注自述「零逻辑」)⇒ `kind: "exempt"` + 有据豁免理由。
20
+ * ⛔ 不做硬编码白名单——豁免必须长在结构事实或仓内自述上。
21
+ */
22
+
23
+ import fs from "node:fs";
24
+ import path from "node:path";
25
+
26
+ /** barrel 三件套(无逻辑、只搬运/声明)。 */
27
+ const BARRELS = new Set(["types.ts", "index.ts", "constants.ts"]);
28
+ const SKIP_DIRS = new Set(["node_modules", ".git", "dist", "release", "resources"]);
29
+
30
+ /** 审计口径的机器可读版——报告与判红信息直接引用,别再手抄一份。 */
31
+ export const AUDIT_CALIBER = {
32
+ prodLines: "src/**/*.{ts,tsx,css} 去 *.test.ts(x),逐文件 split(/\\r?\\n/).length 求和(含末行换行)",
33
+ testLines: "src/**/*.test.ts(x)",
34
+ wideUnits: "src/**/*.ts 去 .d.ts / *.test.ts / barrel(types|index|constants)",
35
+ logicUnits: "宽口径再减 use* hook / 含 window.linkdesk / 含 React 导入",
36
+ hit: "① 同名测试文件(基名 = 单元名 ∨ 目录名)∨ ② 任一测试文件的模块说明符引用(含目录桶)",
37
+ zeroTestNote: "零测 = 待裁决,不是判死——同名判据看不见跨文件覆盖,请逐条核",
38
+ };
39
+
40
+ const isTestFile = (p) => /\.(test|spec)\.tsx?$/.test(p);
41
+ /** 计行 = 含末行换行(逐文件 split 后求和)。 */
42
+ const countLines = (p) => fs.readFileSync(p, "utf8").split(/\r?\n/).length;
43
+ const read = (p) => fs.readFileSync(p, "utf8");
44
+
45
+ function walk(dir, filter, out = []) {
46
+ let ents;
47
+ try {
48
+ ents = fs.readdirSync(dir, { withFileTypes: true });
49
+ } catch {
50
+ return out;
51
+ }
52
+ for (const e of ents) {
53
+ if (SKIP_DIRS.has(e.name)) continue;
54
+ const p = path.join(dir, e.name);
55
+ if (e.isDirectory()) walk(p, filter, out);
56
+ else if (filter(p)) out.push(p);
57
+ }
58
+ return out;
59
+ }
60
+
61
+ /** 严口径:`.ts` 单元再减 `use*` hook / 含 `window.linkdesk` / 含 React 导入。 */
62
+ function isPureLogic(file) {
63
+ if (/^use/.test(path.basename(file))) return false;
64
+ const text = read(file);
65
+ if (text.includes("window.linkdesk")) return false;
66
+ if (/\bfrom\s+["']react["']/.test(text)) return false;
67
+ if (/\brequire\(\s*["']react["']\s*\)/.test(text)) return false;
68
+ if (/\bimport\s+React\b/.test(text)) return false;
69
+ return true;
70
+ }
71
+
72
+ /** 测试文件里出现的模块说明符(from / import() / require() / vi.mock 的字符串)。 */
73
+ function specifiersOf(text) {
74
+ const out = [];
75
+ const re = /(?:from|import|require|vi\.mock|vi\.doMock|vi\.importActual|vi\.importMock)\s*\(?\s*["']([^"']+)["']/g;
76
+ let m;
77
+ while ((m = re.exec(text))) out.push(m[1]);
78
+ return out;
79
+ }
80
+
81
+ /** 一个单元「被引用」的全部可能写法:路径 ∨ 基名 ∨ 目录桶(含 index)。 */
82
+ function unitSpecifiers(unitPath, srcDir) {
83
+ const rel = path.relative(srcDir, unitPath).split(path.sep).join("/").replace(/\.ts$/, "");
84
+ const base = path.posix.basename(rel);
85
+ const dirRel = path.posix.dirname(rel);
86
+ const specs = new Set([rel, base, `${rel}.ts`, `${base}.ts`, `${base}.js`]);
87
+ const dirAbs = path.dirname(unitPath);
88
+ if (fs.existsSync(path.join(dirAbs, "index.ts")) || fs.existsSync(path.join(dirAbs, "index.tsx"))) {
89
+ specs.add(dirRel);
90
+ specs.add(`${dirRel}/index`);
91
+ }
92
+ return { rel, specs: [...specs] };
93
+ }
94
+
95
+ /** 声明式 / 零逻辑仓的**有据豁免**:先看结构事实,再看 entry 头注自述(⛔ 不硬编码白名单)。 */
96
+ function exemptReason(repoDir, srcExists, wideCount) {
97
+ if (!srcExists) return "不适用(纯声明式:无 `src/`,无可测单元;门禁 = `ci-verify.mjs` 的结构与声明判据)";
98
+ if (wideCount === 0) {
99
+ for (const entry of ["src/index.tsx", "src/index.ts"]) {
100
+ const p = path.join(repoDir, entry);
101
+ if (!fs.existsSync(p)) continue;
102
+ const quote = read(p)
103
+ .split(/\r?\n/)
104
+ .slice(0, 40)
105
+ .find((l) => l.includes("零逻辑"));
106
+ if (quote) return `不适用(零逻辑空壳 entry——${entry} 头注自述「${quote.replace(/^[\s*/]+/, "").trim()}」)`;
107
+ }
108
+ return "不适用(零逻辑空壳 entry:`src/` 下无非 barrel `.ts` 单元)";
109
+ }
110
+ return null;
111
+ }
112
+
113
+ /**
114
+ * 审计一只插件仓(只读,不写任何文件)。
115
+ * @param {string} repoDir 插件仓根(含 plugin.json 与 src/)
116
+ * @param {{ official?: boolean }} opts official=false ⇒ 第三方仓形态(kind: "third-party",
117
+ * 消费方应只读报出、不代改);默认 true。仓内结构事实判出的豁免见 `exempt`。
118
+ * @returns {{ kind: "logic"|"exempt"|"third-party", exempt: string|null, prodLines: number,
119
+ * testLines: number, testFiles: number, wideUnits: number, logicUnits: number,
120
+ * zeroTest: Array<{unit: string, evidence: string[]}>, coveredBasenameOnly: string[],
121
+ * coveredReferenceOnly: string[], coveredBoth: string[] }}
122
+ */
123
+ export function analyzeRepo(repoDir, opts = {}) {
124
+ const official = opts.official !== false;
125
+ const srcDir = path.join(repoDir, "src");
126
+ const srcExists = fs.existsSync(srcDir);
127
+ const srcFiles = srcExists ? walk(srcDir, (p) => /\.(ts|tsx|css)$/.test(p)) : [];
128
+ const testFiles = srcFiles.filter(isTestFile);
129
+ const prodFiles = srcFiles.filter((p) => !isTestFile(p));
130
+ const candidates = srcFiles.filter(
131
+ (p) => p.endsWith(".ts") && !p.endsWith(".d.ts") && !isTestFile(p) && !BARRELS.has(path.basename(p)),
132
+ );
133
+ const units = candidates.filter(isPureLogic);
134
+
135
+ const prodLines = prodFiles.reduce((a, p) => a + countLines(p), 0);
136
+ const testLines = testFiles.reduce((a, p) => a + countLines(p), 0);
137
+ const testBaseNames = new Set(testFiles.map((p) => path.basename(p).replace(/\.(test|spec)\.tsx?$/, "")));
138
+ const testSpecs = testFiles.map((p) => specifiersOf(read(p)));
139
+
140
+ const judged = units.map((p) => {
141
+ const base = path.basename(p, ".ts");
142
+ const parent = path.basename(path.dirname(p));
143
+ const hitBasename = testBaseNames.has(base) || testBaseNames.has(parent);
144
+ const { rel, specs } = unitSpecifiers(p, srcDir);
145
+ const hitReference = testSpecs.some((list) => list.some((s) => specs.some((c) => s === c || s.endsWith(`/${c}`))));
146
+ return { unit: rel, hitBasename, hitReference, covered: hitBasename || hitReference };
147
+ });
148
+ const zeroTest = judged.filter((j) => !j.covered);
149
+
150
+ let kind = "logic";
151
+ let exempt = null;
152
+ if (!official) kind = "third-party";
153
+ else if (!srcExists || candidates.length === 0) {
154
+ kind = "exempt";
155
+ exempt = exemptReason(repoDir, srcExists, candidates.length);
156
+ }
157
+
158
+ return {
159
+ kind,
160
+ exempt,
161
+ prodLines,
162
+ testLines,
163
+ testFiles: testFiles.length,
164
+ wideUnits: candidates.length,
165
+ logicUnits: units.length,
166
+ zeroTest: zeroTest.map((j) => ({ unit: j.unit, evidence: ["basename-miss", "reference-miss"] })),
167
+ coveredBasenameOnly: judged.filter((j) => j.hitBasename && !j.hitReference).map((j) => j.unit),
168
+ coveredReferenceOnly: judged.filter((j) => !j.hitBasename && j.hitReference).map((j) => j.unit),
169
+ coveredBoth: judged.filter((j) => j.hitBasename && j.hitReference).map((j) => j.unit),
170
+ };
171
+ }