dsh-git-push 2.20.18 → 3.0.0
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/README.md +3 -2
- package/cli-tool.mjs +2 -4
- package/cli.mjs +36 -2
- package/lib/app/apply.js +9 -9
- package/lib/app/command-registry.js +44 -20
- package/lib/app/handlers/meta.js +7 -142
- package/lib/app/handlers/npm.js +18 -1
- package/lib/app/handlers/preprocess.js +107 -0
- package/lib/app/handlers/repo-actions.js +2 -2
- package/lib/app/http-handlers.js +25 -12
- package/lib/app/index.js +2 -1
- package/lib/app/inject-text.js +3 -2
- package/lib/app/preprocess-client.js +136 -0
- package/lib/app/repo-ownership.js +1 -1
- package/lib/app/schema.js +3 -3
- package/lib/app/severity.js +14 -0
- package/lib/app/slash-commands.js +5 -164
- package/lib/app/tool-call.js +75 -140
- package/lib/app/tools.js +13 -6
- package/lib/cli/commands-preprocess.mjs +74 -0
- package/lib/cli/commands-tool.mjs +3 -2
- package/lib/cli/commands-vcs.mjs +4 -4
- package/lib/cli/shared.mjs +42 -13
- package/lib/client-parts/git/controller.js +2 -2
- package/lib/client.js +2 -2
- package/lib/commit-push/pre-commit-engine.js +57 -10
- package/lib/commit-push.js +125 -36
- package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
- package/lib/git/ignore.js +1 -1
- package/lib/git/repos.js +2 -2
- package/lib/git/sensitive.js +1 -1
- package/lib/npm/publish.js +20 -5
- package/lib/plugin/auto-push.js +2 -2
- package/lib/preprocess/providers.js +191 -0
- package/lib/self/index.js +1 -1
- package/lib/skip-dirs.js +14 -55
- package/package.json +29 -37
- package/scripts/build-client.mjs +2 -0
- package/scripts/check.mjs +4 -2
- package/scripts/local-registry.mjs +2 -1
- package/skills/dev/dsh-git-push.md +5 -0
- package/cli-audit.mjs +0 -141
- package/lib/app/audit-api.js +0 -173
- package/lib/app/host-doc-checks.js +0 -25
- package/lib/app/scope-check.js +0 -69
- package/lib/ast/brace.js +0 -120
- package/lib/ast/callgraph.js +0 -135
- package/lib/ast/code-lines.js +0 -175
- package/lib/ast/consts.js +0 -12
- package/lib/ast/control-flow.js +0 -470
- package/lib/ast/credential.js +0 -315
- package/lib/ast/dataflow.js +0 -391
- package/lib/ast/dup-code.js +0 -165
- package/lib/ast/fs-hotspots.js +0 -154
- package/lib/ast/index.js +0 -38
- package/lib/ast/io-risk-const.js +0 -95
- package/lib/ast/io-risk-fn.js +0 -208
- package/lib/ast/io-risk-loop.js +0 -308
- package/lib/ast/io-risk.js +0 -314
- package/lib/ast/lang.js +0 -156
- package/lib/ast/line-count.js +0 -52
- package/lib/ast/magic-number.js +0 -272
- package/lib/ast/naming.js +0 -292
- package/lib/ast/perf.js +0 -488
- package/lib/ast/scope-collisions.js +0 -228
- package/lib/ast/scope.js +0 -241
- package/lib/ast/shell.js +0 -131
- package/lib/ast/size.js +0 -448
- package/lib/ast/symbol-index.js +0 -399
- package/lib/ast/tokenizer.js +0 -421
- package/lib/ast/treesitter.js +0 -413
- package/lib/audit/analysis-coverage.js +0 -98
- package/lib/audit/audit-file.js +0 -205
- package/lib/audit/auditignore-rules.js +0 -148
- package/lib/audit/checks.js +0 -17
- package/lib/audit/client-entry.js +0 -93
- package/lib/audit/collector.js +0 -610
- package/lib/audit/ext-runner.js +0 -80
- package/lib/audit/file-context.js +0 -391
- package/lib/audit/finding.js +0 -101
- package/lib/audit/gitignore-match.js +0 -113
- package/lib/audit/glob.js +0 -63
- package/lib/audit/history-report.js +0 -100
- package/lib/audit/history.js +0 -126
- package/lib/audit/ignore-blind.js +0 -102
- package/lib/audit/index.js +0 -33
- package/lib/audit/orchestrate.js +0 -672
- package/lib/audit/public.js +0 -24
- package/lib/audit/repo-level.js +0 -30
- package/lib/audit/report-yaml.js +0 -90
- package/lib/audit/scope-policy.js +0 -110
- package/lib/audit/slot.js +0 -66
- package/lib/audit-rules/audit-rules-comment.yml +0 -199
- package/lib/audit-rules/audit-rules-cpp.yml +0 -114
- package/lib/audit-rules/audit-rules-csharp.yml +0 -82
- package/lib/audit-rules/audit-rules-docs.yml +0 -32
- package/lib/audit-rules/audit-rules-dsh.yml +0 -177
- package/lib/audit-rules/audit-rules-filehealth.yml +0 -42
- package/lib/audit-rules/audit-rules-folder.yml +0 -99
- package/lib/audit-rules/audit-rules-frontend.yml +0 -335
- package/lib/audit-rules/audit-rules-go.yml +0 -103
- package/lib/audit-rules/audit-rules-i18n.yml +0 -45
- package/lib/audit-rules/audit-rules-java.yml +0 -105
- package/lib/audit-rules/audit-rules-nodejs.yml +0 -662
- package/lib/audit-rules/audit-rules-npm.yml +0 -187
- package/lib/audit-rules/audit-rules-paths.yml +0 -98
- package/lib/audit-rules/audit-rules-performance.yml +0 -245
- package/lib/audit-rules/audit-rules-php.yml +0 -101
- package/lib/audit-rules/audit-rules-private.yml +0 -24
- package/lib/audit-rules/audit-rules-python.yml +0 -106
- package/lib/audit-rules/audit-rules-robustness.yml +0 -31
- package/lib/audit-rules/audit-rules-rust.yml +0 -109
- package/lib/audit-rules/audit-rules-structure.yml +0 -158
- package/lib/audit-rules/audit-rules-swift.yml +0 -99
- package/lib/audit-rules/audit-rules-template.yml +0 -108
- package/lib/audit-ui-sdk/contract.js +0 -117
- package/lib/audit-ui-sdk/index.js +0 -17
- package/lib/checks/button-bind.js +0 -347
- package/lib/checks/common.js +0 -170
- package/lib/checks/credential-file.js +0 -43
- package/lib/checks/dataflow.js +0 -40
- package/lib/checks/dispatch.js +0 -117
- package/lib/checks/dup-code.js +0 -69
- package/lib/checks/dup-const.js +0 -232
- package/lib/checks/file-health.js +0 -150
- package/lib/checks/filter.js +0 -137
- package/lib/checks/folder.js +0 -173
- package/lib/checks/fs-scanner.js +0 -57
- package/lib/checks/hardcoded-path.js +0 -98
- package/lib/checks/index.js +0 -49
- package/lib/checks/io.js +0 -66
- package/lib/checks/magic-number.js +0 -42
- package/lib/checks/npm-json.js +0 -273
- package/lib/checks/private.js +0 -64
- package/lib/checks/regex.js +0 -298
- package/lib/checks/scope-collision.js +0 -68
- package/lib/checks/semantic.js +0 -134
- package/lib/checks/structural.js +0 -281
- package/lib/cli/commands-audit.mjs +0 -142
- package/lib/cli/commands-doc.mjs +0 -127
- package/lib/client-parts/audit/audit.js +0 -301
- package/lib/exempt/tool.js +0 -198
- package/lib/git/module-splitter.js +0 -47
- package/lib/link-check/index.js +0 -187
- package/lib/lint-adapter/detect.js +0 -161
- package/lib/lint-adapter/index.js +0 -50
- package/lib/lint-adapter/runner.js +0 -133
- package/lib/rule/compilers/credential.js +0 -62
- package/lib/rule/compilers/dataflow.js +0 -38
- package/lib/rule/compilers/file-health.js +0 -75
- package/lib/rule/compilers/frontend.js +0 -86
- package/lib/rule/compilers/func.js +0 -35
- package/lib/rule/compilers/helpers.js +0 -71
- package/lib/rule/compilers/index.js +0 -29
- package/lib/rule/compilers/link-semantic.js +0 -45
- package/lib/rule/compilers/list-kinds.js +0 -12
- package/lib/rule/compilers/numeric-kinds.js +0 -73
- package/lib/rule/compilers/regex.js +0 -78
- package/lib/rule/compilers/structure.js +0 -107
- package/lib/rule/compilers.js +0 -17
- package/lib/rule/homoglyph.js +0 -45
- package/lib/rule/loader.js +0 -274
- package/lib/rule/registry.js +0 -122
- package/lib/rule/scope.js +0 -67
- package/lib/score/docs-score.js +0 -166
- package/lib/score/index.js +0 -234
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +0 -34
- package/scripts/audit-ext/dsh-inject-declarations.mjs +0 -209
- package/scripts/audit-ext/variable-min-length.mjs +0 -102
- package/scripts/audit-gate.mjs +0 -235
- package/scripts/audit-runner.mjs +0 -35
- package/scripts/audit-runtime-check.mjs +0 -129
- package/scripts/browser-page-probe.mjs +0 -271
- package/scripts/check-js-scope.mjs +0 -87
- package/scripts/clean-date-comments.py +0 -102
- package/scripts/cpu-sample.mjs +0 -188
- package/scripts/doc-drift.mjs +0 -200
- package/scripts/hot-path-probe-cjs.cjs +0 -71
- package/scripts/hot-path-probe-fs-shim.mjs +0 -28
- package/scripts/hot-path-probe-fsp-shim.mjs +0 -31
- package/scripts/hot-path-probe.mjs +0 -153
- package/scripts/module-splitter.py +0 -800
- package/scripts/picker-repro.mjs +0 -115
- package/scripts/platform-scan.mjs +0 -168
- package/scripts/probe-recheck.mjs +0 -47
- package/scripts/rename-locator.mjs +0 -97
- package/scripts/rule-switch.mjs +0 -97
- package/scripts/rules-solo-audit.mjs +0 -173
- package/scripts/scan-file-io.mjs +0 -717
- package/scripts/scan-fs-hotspots.mjs +0 -107
- package/scripts/symbol-index.mjs +0 -85
- package/scripts/verify-prestep.mjs +0 -100
- package/skills/dev/audit-rule-authoring.md +0 -58
- package/skills/guide/guide-audit.md +0 -270
- /package/lib/{audit → app}/ownership.js +0 -0
- /package/lib/app/{schema-audit.js → schema-preprocess.js} +0 -0
- /package/lib/{audit-defaults.js → git/defaults.js} +0 -0
package/lib/ast/symbol-index.js
DELETED
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 跨文件符号索引(稳定 ID + import 解析 + 跨文件调用落地)
|
|
3
|
-
*
|
|
4
|
-
* 【为什么需要】现有 lib/ast 是**单文件**分析:callgraph.js 头部明确「跨文件调用…不强行判断」。
|
|
5
|
-
* 要支撑「谁调用了谁」「哪些导出没人用」这类跨文件事实,先得有一份工程级符号索引。
|
|
6
|
-
*
|
|
7
|
-
* 【稳定 ID】`<清洗后的相对路径>#<清洗后的符号名>`,**不含行号**——行号会随编辑漂移,
|
|
8
|
-
* 把行号放进 ID 会让缓存与引用全废。ID 只由「路径 + 符号名」决定,改行、加注释都不变。
|
|
9
|
-
*
|
|
10
|
-
* 【调用落地(宁缺勿错)】每个调用点按三级判定落到定义:
|
|
11
|
-
* ① local —— 本文件有同名声明
|
|
12
|
-
* ② import —— 该名字来自 import/require,沿说明符解析到工程内文件,取该文件的导出
|
|
13
|
-
* ③ unique —— 全工程**有且仅有一个**文件导出该名字
|
|
14
|
-
* 三级都不成立或出现多个候选 → **不连边**,记进 unresolved(供「分析覆盖率」统计)。
|
|
15
|
-
*
|
|
16
|
-
* 【纯函数化】不碰文件系统:调用方传 `files`(相对路径列表)与 `readFile(rel)`,
|
|
17
|
-
* 便于单测用内存工程、也便于上层换成带缓存的读取(配合按文件失效的增量索引)。
|
|
18
|
-
*/
|
|
19
|
-
import { tokenize } from './tokenizer.js';
|
|
20
|
-
import { collectNamedFnRanges } from './callgraph.js';
|
|
21
|
-
|
|
22
|
-
/** 解析相对说明符时的候选后缀(按顺序尝试)。 */
|
|
23
|
-
const RESOLVE_SUFFIXES = ['', '.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '/index.js', '/index.mjs', '/index.ts', '/index.tsx'];
|
|
24
|
-
/** 声明类关键字(后接标识符即为声明)。 */
|
|
25
|
-
const DECL_KEYWORDS = ['function', 'class', 'const', 'let', 'var'];
|
|
26
|
-
/** 调用点排除的关键字(形如 `if (`、`return (` 不是调用)。 */
|
|
27
|
-
const CALL_EXCLUDE = new Set(['if', 'for', 'while', 'switch', 'catch', 'return', 'typeof', 'await', 'new', 'function', 'import']);
|
|
28
|
-
/**
|
|
29
|
-
* TS 项目的 ESM 写法:源码是 `x.ts`,import 却写 `./x.js`(Node 解析规范)。
|
|
30
|
-
* 因此当 `.js/.mjs/.cjs` 后缀解析不到时,把后缀换成对应 TS 扩展再试一遍。
|
|
31
|
-
*/
|
|
32
|
-
const TS_FALLBACK = { '.js': ['.ts', '.tsx'], '.mjs': ['.mts', '.ts'], '.cjs': ['.cts', '.ts'], '.jsx': ['.tsx'] };
|
|
33
|
-
/** 往回找 `export` 关键字的最多 token 数(`export function f` / `export const f` 都在 4 个 token 内)。 */
|
|
34
|
-
const EXPORT_LOOKBACK = 4;
|
|
35
|
-
/** 往回找 CJS `require` 赋值左侧名字的最多 token 数(`const { a, b } = require(...)`)。 */
|
|
36
|
-
const CJS_LOOKBACK = 12;
|
|
37
|
-
|
|
38
|
-
/** 把任意字符串清洗成可用作 ID 片段的标识符(保留 `$`,JS 合法)。 */
|
|
39
|
-
export function sanitizeId(input) {
|
|
40
|
-
return String(input || '').replace(/[^A-Za-z0-9_$]/g, '_');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 生成稳定符号 ID(不含行号)。
|
|
45
|
-
* @param {string} relPath 相对路径(如 `lib/a.js`)
|
|
46
|
-
* @param {string} name 符号名
|
|
47
|
-
* @returns {string} 如 `lib_a_js#alpha`
|
|
48
|
-
*/
|
|
49
|
-
export function symbolId(relPath, name) {
|
|
50
|
-
const cleanPath = sanitizeId(String(relPath || '').replace(/\\/g, '/').replace(/\.[^./]+$/, ''));
|
|
51
|
-
return `${cleanPath}#${sanitizeId(name)}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** 去掉字符串字面量的引号。 */
|
|
55
|
-
function unquote(raw) {
|
|
56
|
-
const s = String(raw || '');
|
|
57
|
-
return s.length >= 2 && (s[0] === '"' || s[0] === "'" || s[0] === '`') ? s.slice(1, -1) : s;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** 收集声明:`function/class/const|let|var NAME`,并标记是否被 export。 */
|
|
61
|
-
function collectDeclarations(tokens) {
|
|
62
|
-
const decls = [];
|
|
63
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
64
|
-
const t = tokens[i];
|
|
65
|
-
if (t.type !== 'ident' || !DECL_KEYWORDS.includes(t.value)) continue;
|
|
66
|
-
const nameTok = tokens[i + 1];
|
|
67
|
-
if (!nameTok || nameTok.type !== 'ident') continue;
|
|
68
|
-
// 往回看是否为 `export`(`export function f` / `export const f`)
|
|
69
|
-
let exported = false;
|
|
70
|
-
for (let j = i - 1; j >= 0 && j > i - EXPORT_LOOKBACK; j--) {
|
|
71
|
-
if (tokens[j].type === 'comment' || tokens[j].type === 'ws') continue;
|
|
72
|
-
exported = tokens[j].type === 'ident' && tokens[j].value === 'export';
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
decls.push({ name: nameTok.value, kind: t.value === 'function' ? 'function' : t.value, line: nameTok.line, exported });
|
|
76
|
-
}
|
|
77
|
-
return decls;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 收集 ESM 导入绑定:`import X from 's'` / `import {a, b as c} from 's'` / `import * as ns from 's'`。
|
|
82
|
-
* 每条绑定记 `{local, imported}` —— **别名必须保留原名**,否则解析时拿本地名去目标文件找导出必然落空。
|
|
83
|
-
*/
|
|
84
|
-
function collectEsmImports(tokens) {
|
|
85
|
-
const imports = [];
|
|
86
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
87
|
-
const t = tokens[i];
|
|
88
|
-
if (t.type !== 'ident' || t.value !== 'import') continue;
|
|
89
|
-
const bindings = [];
|
|
90
|
-
let source = '';
|
|
91
|
-
for (let j = i + 1; j < tokens.length; j++) {
|
|
92
|
-
const tk = tokens[j];
|
|
93
|
-
if (tk.type === 'str') { source = unquote(tk.value); break; }
|
|
94
|
-
if (tk.type === 'punct' && tk.value === ';') break;
|
|
95
|
-
if (tk.type === 'ident' && (tk.value === 'from' || tk.value === 'type')) continue;
|
|
96
|
-
if (tk.type === 'ident' && tk.value === 'as') {
|
|
97
|
-
const ns = tokens[j + 1]; // `import * as ns`:整模块命名空间,按 '*' 记账
|
|
98
|
-
if (ns && ns.type === 'ident') { bindings.push({ local: ns.value, imported: '*' }); j++; }
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (tk.type !== 'ident') continue;
|
|
102
|
-
const next = tokens[j + 1];
|
|
103
|
-
if (next && next.type === 'ident' && next.value === 'as') {
|
|
104
|
-
const alias = tokens[j + 2]; // `{ imported as local }`
|
|
105
|
-
if (alias && alias.type === 'ident') { bindings.push({ local: alias.value, imported: tk.value }); j += 2; }
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const isDefault = next && next.type === 'ident' && next.value === 'from';
|
|
109
|
-
bindings.push({ local: tk.value, imported: isDefault ? 'default' : tk.value });
|
|
110
|
-
}
|
|
111
|
-
if (source) imports.push({ source, bindings, line: t.line });
|
|
112
|
-
}
|
|
113
|
-
return imports;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* 收集 CJS 导入:`const x = require('s')` / `const { a, b: c } = require('s')`。
|
|
118
|
-
* 解构改名(`b: c`)无法从 token 反推,这里按同名近似记账(宁可少解析,不猜错目标)。
|
|
119
|
-
*/
|
|
120
|
-
function collectCjsImports(tokens) {
|
|
121
|
-
const imports = [];
|
|
122
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
123
|
-
const t = tokens[i];
|
|
124
|
-
if (t.type !== 'ident' || t.value !== 'require') continue;
|
|
125
|
-
const open = tokens[i + 1];
|
|
126
|
-
const arg = tokens[i + 2];
|
|
127
|
-
if (!open || open.value !== '(' || !arg || arg.type !== 'str') continue;
|
|
128
|
-
const bindings = [];
|
|
129
|
-
for (let j = i - 1; j >= 0 && j > i - CJS_LOOKBACK; j--) {
|
|
130
|
-
const tk = tokens[j];
|
|
131
|
-
if (tk.type === 'ident' && !['const', 'let', 'var'].includes(tk.value)) bindings.push({ local: tk.value, imported: tk.value });
|
|
132
|
-
if (tk.type === 'punct' && tk.value === '=') break;
|
|
133
|
-
}
|
|
134
|
-
imports.push({ source: unquote(arg.value), bindings, line: t.line });
|
|
135
|
-
}
|
|
136
|
-
return imports;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 收集调用点:标识符紧跟 `(`,且不在函数声明处、不是关键字、**不是成员调用**。
|
|
141
|
-
*
|
|
142
|
-
* 成员调用(`obj.method(`、`a?.b(`)与符号索引能解析的「跨文件函数调用」不是一类东西:
|
|
143
|
-
* 把它算进来会让「未解析」爆炸(实测 2 万次调用里绝大多数是 `console.log`/`arr.map` 这类),
|
|
144
|
-
* 覆盖率随之虚低、指标失去意义。这里单独计数(skippedMemberCalls)以便如实说明被排除的量。
|
|
145
|
-
* @returns {{calls:Array, skippedMemberCalls:number}}
|
|
146
|
-
*/
|
|
147
|
-
function collectCallSites(tokens, fnRanges) {
|
|
148
|
-
const calls = [];
|
|
149
|
-
let skippedMemberCalls = 0;
|
|
150
|
-
const declNameIdx = new Set();
|
|
151
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
152
|
-
const t = tokens[i];
|
|
153
|
-
if (t.type === 'ident' && DECL_KEYWORDS.includes(t.value) && tokens[i + 1] && tokens[i + 1].type === 'ident') declNameIdx.add(i + 1);
|
|
154
|
-
}
|
|
155
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
156
|
-
const t = tokens[i];
|
|
157
|
-
if (t.type !== 'ident' || CALL_EXCLUDE.has(t.value) || declNameIdx.has(i)) continue;
|
|
158
|
-
const nx = tokens[i + 1];
|
|
159
|
-
if (!nx || nx.type !== 'punct' || nx.value !== '(') continue;
|
|
160
|
-
if (isMemberAccess(tokens, i)) { skippedMemberCalls++; continue; }
|
|
161
|
-
calls.push({ name: t.value, line: t.line, caller: findEnclosingFn(i, fnRanges) });
|
|
162
|
-
}
|
|
163
|
-
return { calls, skippedMemberCalls };
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** 该标识符是否是成员访问(前面是 `.` 或 `?.`,跳过空白/注释)。 */
|
|
167
|
-
function isMemberAccess(tokens, idx) {
|
|
168
|
-
for (let j = idx - 1; j >= 0; j--) {
|
|
169
|
-
const tk = tokens[j];
|
|
170
|
-
if (tk.type === 'ws' || tk.type === 'comment') continue;
|
|
171
|
-
if (tk.type === 'punct' && (tk.value === '.' || tk.value === '?.')) return true;
|
|
172
|
-
return false;
|
|
173
|
-
}
|
|
174
|
-
return false;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/** 找到包含该 token 下标的具名函数(作为调用者)。 */
|
|
178
|
-
function findEnclosingFn(idx, fnRanges) {
|
|
179
|
-
for (const [name, [start, end]] of fnRanges) {
|
|
180
|
-
if (idx > start && idx < end) return name;
|
|
181
|
-
}
|
|
182
|
-
return '';
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* 提取单文件的符号事实(纯函数)。
|
|
187
|
-
* @param {string} relPath 相对路径
|
|
188
|
-
* @param {string} text 文件全文
|
|
189
|
-
* @returns {{path:string, declarations:Array, imports:Array, calls:Array}}
|
|
190
|
-
*/
|
|
191
|
-
export function extractFileSymbols(relPath, text) {
|
|
192
|
-
const tokens = tokenize(String(text || ''));
|
|
193
|
-
const fnRanges = collectNamedFnRanges(tokens);
|
|
194
|
-
const { calls, skippedMemberCalls } = collectCallSites(tokens, fnRanges);
|
|
195
|
-
return {
|
|
196
|
-
path: relPath,
|
|
197
|
-
declarations: collectDeclarations(tokens),
|
|
198
|
-
imports: [...collectEsmImports(tokens), ...collectCjsImports(tokens)],
|
|
199
|
-
calls,
|
|
200
|
-
skippedMemberCalls,
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* 把 import 说明符解析到工程内文件。
|
|
206
|
-
* @param {string} relPath 引用方相对路径
|
|
207
|
-
* @param {string} source 说明符(如 `./a.js`、`../lib/b`、`react`)
|
|
208
|
-
* @param {Set<string>} fileSet 工程内全部相对路径
|
|
209
|
-
* @returns {string|null} 命中的工程内文件;裸包名或解析不到返回 null
|
|
210
|
-
*/
|
|
211
|
-
export function resolveSpecifier(relPath, source, fileSet) {
|
|
212
|
-
const spec = String(source || '');
|
|
213
|
-
if (!spec.startsWith('.')) return null; // 裸包名:外部依赖,不解析
|
|
214
|
-
const baseDir = String(relPath).replace(/\\/g, '/').split('/').slice(0, -1).join('/');
|
|
215
|
-
const joined = `${baseDir}/${spec}`.replace(/\/\.\//g, '/');
|
|
216
|
-
const parts = [];
|
|
217
|
-
for (const seg of joined.split('/')) {
|
|
218
|
-
if (seg === '' || seg === '.') continue;
|
|
219
|
-
if (seg === '..') parts.pop();
|
|
220
|
-
else parts.push(seg);
|
|
221
|
-
}
|
|
222
|
-
const base = parts.join('/');
|
|
223
|
-
for (const suffix of RESOLVE_SUFFIXES) {
|
|
224
|
-
const cand = base + suffix;
|
|
225
|
-
if (fileSet.has(cand)) return cand;
|
|
226
|
-
}
|
|
227
|
-
// TS 回退:`./x.js` 实际是 `x.ts` / `x.tsx`(Node 解析规范下的 TS 写法)
|
|
228
|
-
const extMatch = base.match(/(\.(?:js|mjs|cjs|jsx))$/);
|
|
229
|
-
if (extMatch) {
|
|
230
|
-
const stem = base.slice(0, -extMatch[1].length);
|
|
231
|
-
for (const alt of TS_FALLBACK[extMatch[1]] || []) {
|
|
232
|
-
if (fileSet.has(stem + alt)) return stem + alt;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return null;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* 构建工程级符号索引。
|
|
240
|
-
* @param {string[]} files 工程内文件相对路径列表
|
|
241
|
-
* @param {{readFile:(rel:string)=>string}} opts 读取文件内容(调用方提供,便于单测与缓存)
|
|
242
|
-
* @returns {object} 索引对象(见下方字段)
|
|
243
|
-
*/
|
|
244
|
-
export function buildSymbolIndex(files = [], opts = {}) {
|
|
245
|
-
const readFile = opts.readFile;
|
|
246
|
-
if (typeof readFile !== 'function') throw new TypeError('buildSymbolIndex 需要 readFile(rel)');
|
|
247
|
-
const fileSet = new Set(files);
|
|
248
|
-
const perFile = new Map();
|
|
249
|
-
for (const rel of files) perFile.set(rel, extractFileSymbols(rel, readFile(rel)));
|
|
250
|
-
const exportsByFile = collectExports(perFile);
|
|
251
|
-
const symbols = flattenSymbols(exportsByFile, perFile);
|
|
252
|
-
const { edges, unresolved, importRefs } = resolveCalls(perFile, exportsByFile, fileSet);
|
|
253
|
-
return { files: [...files], symbols, exportsByFile, perFile, edges, unresolved, importRefs, stats: indexStats(perFile, symbols, edges, unresolved) };
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/** 每个文件的导出表:name → {id, kind, line}。 */
|
|
257
|
-
function collectExports(perFile) {
|
|
258
|
-
const out = new Map();
|
|
259
|
-
for (const [rel, facts] of perFile.entries()) {
|
|
260
|
-
const map = new Map();
|
|
261
|
-
for (const d of facts.declarations) {
|
|
262
|
-
if (d.exported) map.set(d.name, { id: symbolId(rel, d.name), kind: d.kind, line: d.line });
|
|
263
|
-
}
|
|
264
|
-
out.set(rel, map);
|
|
265
|
-
}
|
|
266
|
-
return out;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/** 全工程符号表:id → {id, file, name, kind, line}。 */
|
|
270
|
-
function flattenSymbols(exportsByFile, perFile) {
|
|
271
|
-
const symbols = new Map();
|
|
272
|
-
for (const [rel, map] of exportsByFile.entries()) {
|
|
273
|
-
for (const [name, info] of map.entries()) symbols.set(info.id, { id: info.id, file: rel, name, kind: info.kind, line: info.line });
|
|
274
|
-
}
|
|
275
|
-
for (const [rel, facts] of perFile.entries()) {
|
|
276
|
-
for (const d of facts.declarations) {
|
|
277
|
-
const id = symbolId(rel, d.name);
|
|
278
|
-
if (!symbols.has(id)) symbols.set(id, { id, file: rel, name: d.name, kind: d.kind, line: d.line });
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return symbols;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/** 建「名字 → 导出它的文件」反查表(用于 unique 判定)。 */
|
|
285
|
-
function buildNameIndex(exportsByFile) {
|
|
286
|
-
const byName = new Map();
|
|
287
|
-
for (const [rel, map] of exportsByFile.entries()) {
|
|
288
|
-
for (const [name, info] of map.entries()) {
|
|
289
|
-
if (!byName.has(name)) byName.set(name, []);
|
|
290
|
-
byName.get(name).push({ file: rel, id: info.id });
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return byName;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/** 逐调用点做三级判定,产出跨文件边、未解析清单与被 import 引用到的导出集合。 */
|
|
297
|
-
function resolveCalls(perFile, exportsByFile, fileSet) {
|
|
298
|
-
const byName = buildNameIndex(exportsByFile);
|
|
299
|
-
const edges = [];
|
|
300
|
-
const unresolved = [];
|
|
301
|
-
const importRefs = new Set();
|
|
302
|
-
for (const [rel, facts] of perFile.entries()) {
|
|
303
|
-
const localNames = new Set(facts.declarations.map((d) => d.name));
|
|
304
|
-
const importMap = buildImportMap(rel, facts.imports, fileSet, exportsByFile);
|
|
305
|
-
for (const id of importMap.values()) importRefs.add(id);
|
|
306
|
-
for (const call of facts.calls) {
|
|
307
|
-
const hit = resolveOne(rel, call, localNames, importMap, byName);
|
|
308
|
-
if (hit) edges.push(hit);
|
|
309
|
-
else unresolved.push({ file: rel, name: call.name, line: call.line, reason: byName.has(call.name) ? 'ambiguous' : 'unknown' });
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
return { edges, unresolved, importRefs };
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/** 该文件的「本地名 → 目标导出 ID」映射(含 import 解析结果;用 binding.imported 去目标文件找导出)。 */
|
|
316
|
-
function buildImportMap(rel, imports, fileSet, exportsByFile) {
|
|
317
|
-
const map = new Map();
|
|
318
|
-
for (const imp of imports) {
|
|
319
|
-
const target = resolveSpecifier(rel, imp.source, fileSet);
|
|
320
|
-
if (!target) continue;
|
|
321
|
-
const exportsOfTarget = exportsByFile.get(target) || new Map();
|
|
322
|
-
for (const b of imp.bindings || []) {
|
|
323
|
-
const exported = exportsOfTarget.get(b.imported);
|
|
324
|
-
if (exported) map.set(b.local, exported.id);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return map;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/** 单个调用点的三级判定。 */
|
|
331
|
-
function resolveOne(rel, call, localNames, importMap, byName) {
|
|
332
|
-
if (localNames.has(call.name)) return { from: rel, to: symbolId(rel, call.name), via: 'local', line: call.line, caller: call.caller };
|
|
333
|
-
if (importMap.has(call.name)) return { from: rel, to: importMap.get(call.name), via: 'import', line: call.line, caller: call.caller };
|
|
334
|
-
const candidates = byName.get(call.name) || [];
|
|
335
|
-
if (candidates.length === 1) return { from: rel, to: candidates[0].id, via: 'unique', line: call.line, caller: call.caller };
|
|
336
|
-
return null;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/** 汇总统计(含「分析覆盖率」,供置信度指标使用)。 */
|
|
340
|
-
function indexStats(perFile, symbols, edges, unresolved) {
|
|
341
|
-
const byVia = {};
|
|
342
|
-
for (const e of edges) byVia[e.via] = (byVia[e.via] || 0) + 1;
|
|
343
|
-
const facts = [...perFile.values()];
|
|
344
|
-
const calls = facts.reduce((n, f) => n + f.calls.length, 0);
|
|
345
|
-
const skippedMemberCalls = facts.reduce((n, f) => n + (f.skippedMemberCalls || 0), 0);
|
|
346
|
-
const resolved = edges.length;
|
|
347
|
-
return {
|
|
348
|
-
files: perFile.size,
|
|
349
|
-
declarations: facts.reduce((n, f) => n + f.declarations.length, 0),
|
|
350
|
-
exports: facts.reduce((n, f) => n + f.declarations.filter((d) => d.exported).length, 0),
|
|
351
|
-
symbols: symbols.size,
|
|
352
|
-
calls,
|
|
353
|
-
skippedMemberCalls,
|
|
354
|
-
edges: resolved,
|
|
355
|
-
edgesByVia: byVia,
|
|
356
|
-
unresolved: unresolved.length,
|
|
357
|
-
coverage: calls ? Math.round((resolved / calls) * 1000) / 10 : 100,
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/** 查询某个符号的全部调用者(去重,带调用行号)。 */
|
|
362
|
-
export function callersOf(index, id) {
|
|
363
|
-
const target = index.symbols.get(id);
|
|
364
|
-
const out = [];
|
|
365
|
-
for (const e of index.edges) {
|
|
366
|
-
if (e.to !== id) continue;
|
|
367
|
-
out.push({ file: e.from, line: e.line, via: e.via, symbol: target ? target.name : id });
|
|
368
|
-
}
|
|
369
|
-
return out;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* 未被任何调用/导入引用的导出**候选**(动态访问、字符串引用等无法静态判定,故为候选而非结论)。
|
|
374
|
-
* @param {object} index buildSymbolIndex 结果
|
|
375
|
-
* @returns {Array<{id:string,file:string,name:string,line:number}>}
|
|
376
|
-
*/
|
|
377
|
-
export function unusedExportCandidates(index) {
|
|
378
|
-
// 被调用边(import/unique)或被 import 引用(即便没调用)都算「有人用」——
|
|
379
|
-
// 注意:动态访问(obj[name]、字符串引用、被外部工程 import)无法静态判定,故输出是**候选**。
|
|
380
|
-
const referenced = new Set(index.importRefs || []);
|
|
381
|
-
for (const e of index.edges) {
|
|
382
|
-
if (e.via === 'import' || e.via === 'unique') referenced.add(e.to);
|
|
383
|
-
}
|
|
384
|
-
const out = [];
|
|
385
|
-
for (const [file, map] of index.exportsByFile.entries()) {
|
|
386
|
-
for (const [name, info] of map.entries()) {
|
|
387
|
-
if (!referenced.has(info.id)) out.push({ id: info.id, file, name, line: info.line });
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
return out;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/** 一行式摘要(工具/CLI 输出用)。 */
|
|
394
|
-
export function indexSummary(index) {
|
|
395
|
-
const s = index.stats;
|
|
396
|
-
return `符号索引:文件 ${s.files}|声明 ${s.declarations}|导出 ${s.exports}|调用 ${s.calls}|连边 ${s.edges}`
|
|
397
|
-
+ `(local ${s.edgesByVia.local || 0} / import ${s.edgesByVia.import || 0} / unique ${s.edgesByVia.unique || 0})`
|
|
398
|
-
+ `|未解析 ${s.unresolved}|覆盖率 ${s.coverage}%`;
|
|
399
|
-
}
|