dsh-git-push 2.20.19 → 3.1.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 +76 -47
- package/cli-tool.mjs +2 -4
- package/cli.mjs +11 -4
- package/lib/app/apply.js +15 -13
- package/lib/app/command-registry.js +26 -19
- package/lib/app/handlers/http-constants.js +8 -0
- package/lib/app/handlers/meta.js +2 -2
- package/lib/app/handlers/preprocess.js +104 -0
- package/lib/app/handlers/repo-actions.js +2 -3
- package/lib/app/handlers/repos.js +137 -10
- package/lib/app/handlers/settings.js +34 -15
- package/lib/app/http-handlers.js +13 -42
- package/lib/app/index.js +3 -2
- package/lib/app/inject-text.js +6 -21
- package/lib/app/preprocess-client.js +136 -0
- package/lib/app/repo-ownership.js +1 -1
- package/lib/app/schema.js +20 -10
- package/lib/app/settings-bridge.js +60 -8
- package/lib/app/severity.js +14 -0
- package/lib/app/slash-commands.js +5 -164
- package/lib/app/tool-call.js +96 -163
- package/lib/cli/commands-preprocess.mjs +74 -0
- package/lib/cli/commands-tool.mjs +3 -2
- package/lib/cli/commands-vcs.mjs +39 -5
- package/lib/cli/shared.mjs +36 -10
- package/lib/client/index.js +11 -16
- package/lib/client-parts/git/account.js +21 -12
- package/lib/client-parts/git/actions.js +23 -77
- package/lib/client-parts/git/apply.js +8 -2
- package/lib/client-parts/git/browser.js +1 -1
- package/lib/client-parts/git/const.js +33 -1
- package/lib/client-parts/git/controller.js +175 -176
- package/lib/client-parts/git/icons.js +1 -1
- package/lib/client-parts/git/loader.js +1 -1
- package/lib/client-parts/git/npm.js +56 -37
- package/lib/client-parts/git/page.js +53 -1
- package/lib/client-parts/git/repo.js +8 -1
- package/lib/client-parts/git/settings.js +13 -1
- package/lib/client-parts/git/state.js +237 -0
- package/lib/client-parts/git/utils.js +1 -1
- package/lib/client.js +631 -312
- package/lib/commit-push/pre-commit-engine.js +629 -30
- package/lib/commit-push.js +127 -67
- package/lib/context/index.js +7 -5
- package/lib/core/config.js +1 -27
- package/lib/core/exec.js +2 -11
- package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
- package/lib/git/identity-rewrite.js +1 -1
- package/lib/git/ignore.js +1 -1
- package/lib/git/index.js +1 -1
- package/lib/git/push.js +2 -17
- package/lib/git/repo-index.js +8 -3
- package/lib/git/repos.js +2 -2
- package/lib/git/sensitive.js +1 -1
- package/lib/npm/install.js +1 -1
- package/lib/npm/publish.js +20 -5
- package/lib/npm/registry.js +2 -2
- package/lib/plugin/auto-push.js +5 -8
- package/lib/preprocess/providers.js +195 -0
- package/lib/self/index.js +1 -1
- package/lib/skip-dirs.js +14 -55
- package/package.json +31 -42
- package/skills/dev/dsh-git-push.md +9 -9
- package/skills/guide/guide-commit-push.md +11 -13
- package/skills/workflow/README.md +0 -1
- package/skills/workflow/task-completion-report.md +2 -2
- package/.auditignore +0 -30
- package/.gitignore +0 -43
- package/cli-audit.mjs +0 -141
- package/lib/BUGFIX-NOTES-2026-09-14.md +0 -278
- package/lib/app/audit-api.js +0 -173
- package/lib/app/handlers/audit.js +0 -150
- package/lib/app/host-doc-checks.js +0 -25
- package/lib/app/schema-audit.js +0 -42
- 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/preprocess/audit.js +0 -98
- package/lib/preprocess/index.js +0 -78
- 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/lib/user-requirements.json +0 -15
- 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/build-client.mjs +0 -131
- package/scripts/bump-version.mjs +0 -178
- package/scripts/check-js-scope.mjs +0 -87
- package/scripts/check.mjs +0 -203
- package/scripts/clean-date-comments.py +0 -102
- package/scripts/cpu-sample.mjs +0 -188
- package/scripts/doc-drift.mjs +0 -200
- package/scripts/doc-func.mjs +0 -212
- package/scripts/doc-tree.mjs +0 -458
- package/scripts/doc-version.mjs +0 -149
- 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/local-registry.mjs +0 -37
- 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/scan-version.mjs +0 -125
- package/scripts/symbol-index.mjs +0 -85
- package/scripts/verify-prestep.mjs +0 -100
- package/scripts/version-source.mjs +0 -173
- 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/{audit-defaults.js → git/defaults.js} +0 -0
package/lib/ast/control-flow.js
DELETED
|
@@ -1,470 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 控制流检查
|
|
3
|
-
*
|
|
4
|
-
* 职责:同步 fs 调用、空 catch、圈复杂度、嵌套深度(token 级判定)。
|
|
5
|
-
* token 化保证注释里的示例代码不被计入。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { tokenize } from './tokenizer.js';
|
|
9
|
-
import { matchBrace, collectInnerFnRanges, isBlockParen, matchingOpen } from './brace.js';
|
|
10
|
-
import { pythonFuncRanges } from './size.js';
|
|
11
|
-
import { javaKtFuncRanges } from './lang.js';
|
|
12
|
-
|
|
13
|
-
// 向后找异步函数体 '{' 的最大 token 跨度:与 brace.js 共用同一来源(lib/ast/consts.js)
|
|
14
|
-
import { FN_BODY_LOOKAHEAD } from './consts.js';
|
|
15
|
-
|
|
16
|
-
const SYNC_FS_FNS = new Set([
|
|
17
|
-
'readFileSync', 'writeFileSync', 'readdirSync', 'existsSync', 'statSync', 'mkdirSync',
|
|
18
|
-
'rmSync', 'unlinkSync', 'readlinkSync', 'lstatSync', 'renameSync', 'copyFileSync', 'appendFileSync',
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 检查 async 路径中的 fs 同步调用(AST 级,修 named import 假阴性)。
|
|
23
|
-
* @param {string} text 文件全文
|
|
24
|
-
* @returns {Array<{line:number, call:string, via:string}>} via: 'named-import' | 'fs-prefix'
|
|
25
|
-
*/
|
|
26
|
-
/** 第 1 段:收集 `import { readFileSync, X } from 'node:fs'` 的 fs 同步函数名(named import)。 */
|
|
27
|
-
function collectNamedSyncFs(tokens) { // dsh-skip-complexity: named-import 扫描器(import{..}→from 模式匹配,扫描结构必然嵌套)
|
|
28
|
-
const namedSync = new Set();
|
|
29
|
-
for (let i = 0; i < tokens.length - 1; i++) {
|
|
30
|
-
if (tokens[i].type === 'ident' && tokens[i].value === 'import'
|
|
31
|
-
&& tokens[i + 1].type === 'punct' && tokens[i + 1].value === '{') {
|
|
32
|
-
let j = i + 2;
|
|
33
|
-
const names = [];
|
|
34
|
-
while (j < tokens.length && !(tokens[j].type === 'punct' && tokens[j].value === '}')) {
|
|
35
|
-
if (tokens[j].type === 'ident') names.push(tokens[j].value);
|
|
36
|
-
j++;
|
|
37
|
-
}
|
|
38
|
-
// 找 from 'node:fs' / 'fs'
|
|
39
|
-
let k = j + 1;
|
|
40
|
-
let from = '';
|
|
41
|
-
while (k < tokens.length && k < j + 8) {
|
|
42
|
-
if (tokens[k].type === 'ident' && tokens[k].value === 'from') {
|
|
43
|
-
const strTok = tokens[k + 1];
|
|
44
|
-
if (strTok && strTok.type === 'str') {
|
|
45
|
-
from = strTok.value.replace(/['"]/g, '');
|
|
46
|
-
if (/^(node:)?fs$/.test(from)) {
|
|
47
|
-
for (const nm of names) if (SYNC_FS_FNS.has(nm)) namedSync.add(nm);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
k++;
|
|
53
|
-
}
|
|
54
|
-
i = j;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return namedSync;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** 第 2 段:定位所有 async 函数体边界(async function / async X(/ async (),返回 [起行, 止行, 开括号idx, 闭括号idx]。 */
|
|
61
|
-
function collectAsyncRanges(tokens) { // dsh-skip-complexity: async 函数体边界扫描器(token 遍历+配对,结构必然嵌套)
|
|
62
|
-
const asyncRanges = [];
|
|
63
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
64
|
-
const t = tokens[i];
|
|
65
|
-
if (t.type === 'ident' && t.value === 'async') {
|
|
66
|
-
// 向后找函数体 '{'(跳过函数名/参数,限 20 token)
|
|
67
|
-
for (let j = i + 1; j < Math.min(tokens.length, i + FN_BODY_LOOKAHEAD); j++) {
|
|
68
|
-
const tj = tokens[j];
|
|
69
|
-
if (tj.type === 'punct' && tj.value === '{') {
|
|
70
|
-
const range = matchBrace(tokens, j);
|
|
71
|
-
if (range) asyncRanges.push([tokens[j].line, tokens[range[1]].line, j, range[1]]);
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
if (tj.type === 'punct' && (tj.value === ';' || tj.value === '}')) break;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return asyncRanges;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** 第 3 段:找同步 fs 调用(named 直调或 fs.XSync(前缀),落在 async 函数体内即报。 */
|
|
82
|
-
function scanSyncCalls(tokens, namedSync, asyncRanges) { // dsh-skip-complexity: 同步 fs 调用扫描器(定位调用点+区间归属判定,结构必然嵌套)
|
|
83
|
-
const out = [];
|
|
84
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
85
|
-
const t = tokens[i];
|
|
86
|
-
if (t.type !== 'ident') continue;
|
|
87
|
-
let call = '';
|
|
88
|
-
let via = '';
|
|
89
|
-
if (namedSync.has(t.value)) {
|
|
90
|
-
const nx = tokens[i + 1];
|
|
91
|
-
if (nx && nx.type === 'punct' && nx.value === '(') { call = t.value; via = 'named-import'; }
|
|
92
|
-
} else if (t.value === 'fs') {
|
|
93
|
-
const nx = tokens[i + 1];
|
|
94
|
-
if (nx && nx.type === 'punct' && nx.value === '.') {
|
|
95
|
-
const nn = tokens[i + 2];
|
|
96
|
-
if (nn && nn.type === 'ident' && SYNC_FS_FNS.has(nn.value)) {
|
|
97
|
-
const after = tokens[i + 3];
|
|
98
|
-
if (after && after.type === 'punct' && after.value === '(') { call = nn.value; via = 'fs-prefix'; }
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (!call) continue;
|
|
103
|
-
// 判定是否在 async 函数区间内(token 索引区间)
|
|
104
|
-
const inAsync = asyncRanges.some(([, , oi, ci]) => i > oi && i < ci);
|
|
105
|
-
if (inAsync) out.push({ line: t.line, call, via });
|
|
106
|
-
}
|
|
107
|
-
return out;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** 同步 fs 调用落在 async 函数体内 → 报「异步路径同步 I/O」(阻塞事件循环)。 */
|
|
111
|
-
export function checkSyncFs(text = '') {
|
|
112
|
-
const tokens = tokenize(text);
|
|
113
|
-
const namedSync = collectNamedSyncFs(tokens);
|
|
114
|
-
const asyncRanges = collectAsyncRanges(tokens);
|
|
115
|
-
return scanSyncCalls(tokens, namedSync, asyncRanges);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 检查静默吞错 catch(AST 级,括号平衡 → 块内去注释/空白后为空即报)。
|
|
120
|
-
* @param {string} text 文件全文
|
|
121
|
-
* @returns {Array<{line:number}>}
|
|
122
|
-
*/
|
|
123
|
-
export function checkEmptyCatchAst(text = '') { // dsh-skip-complexity: 空 catch 检测器本体(token 遍历 + catch 体空判定多条件链,职责单一)
|
|
124
|
-
const tokens = tokenize(text);
|
|
125
|
-
const out = [];
|
|
126
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
127
|
-
const t = tokens[i];
|
|
128
|
-
if (t.type !== 'ident' || t.value !== 'catch') continue;
|
|
129
|
-
// 跳过 **Promise 方法调用** `.catch(...)`——它不是 try/catch 语句,而是链式调用:
|
|
130
|
-
// 实测误报(dsh-session-conductor 的 12 条里有 6 条属此类):
|
|
131
|
-
// `await res.json().catch(() => ({}))`(取默认值)、`p.catch(() => {})`(故意静默兜底)
|
|
132
|
-
// 两者都不是「静默吞错的 catch 块」,按空 catch 报属误报。
|
|
133
|
-
// 判定:向前跳过空白/注释,若前一 token 是 `.` ⇒ 成员调用。
|
|
134
|
-
let back = i - 1;
|
|
135
|
-
while (back >= 0 && (tokens[back].type === 'ws' || tokens[back].type === 'comment')) back--;
|
|
136
|
-
if (back >= 0 && tokens[back].type === 'punct' && tokens[back].value === '.') continue;
|
|
137
|
-
// 找 catch 后的 '{'(跳过 (param) 与空白,限 10 token)
|
|
138
|
-
let openIdx = -1;
|
|
139
|
-
for (let j = i + 1; j < Math.min(tokens.length, i + 10); j++) {
|
|
140
|
-
const tj = tokens[j];
|
|
141
|
-
if (tj.type === 'punct' && tj.value === '{') { openIdx = j; break; }
|
|
142
|
-
if (tj.type === 'punct' && tj.value === '}') break; // catch (e) 无块 → 不是本类
|
|
143
|
-
}
|
|
144
|
-
if (openIdx === -1) continue;
|
|
145
|
-
const range = matchBrace(tokens, openIdx);
|
|
146
|
-
if (!range) continue;
|
|
147
|
-
const [, closeIdx] = range;
|
|
148
|
-
// 块内判定:有实质语句 → 正常;仅有说明性注释 → 视为「已说明的静默」(不算问题);
|
|
149
|
-
// 真正空块(无语句也无注释)→ 报问题(静默吞错且无任何交代)。
|
|
150
|
-
let hasBody = false;
|
|
151
|
-
let hasNote = false;
|
|
152
|
-
for (let k = openIdx + 1; k < closeIdx; k++) {
|
|
153
|
-
const tk = tokens[k];
|
|
154
|
-
if (tk.type === 'ws') continue;
|
|
155
|
-
if (tk.type === 'comment') {
|
|
156
|
-
// 修:注释「交代了原因」即算已说明——不再只看说明词表。
|
|
157
|
-
// 旧逻辑只认词表(跳过/忽略/…),「/* 无缓存/损坏则空 */」「预热失败不影响
|
|
158
|
-
// 主链路」这类**实质说明了为什么静默**的注释会误报(它们是好的静默)。
|
|
159
|
-
// 判定:去空白后长度 ≥ 4 且不只含单个标识符(`// e` 这种占位不算交代)。
|
|
160
|
-
// 词表语义保留为兜底(含说明词但很短也认)。
|
|
161
|
-
const noteText = String(tk.value || '').replace(/[/*\s]/g, '');
|
|
162
|
-
// CJK 按信息量折算:汉字单字信息量远高于 ASCII 字母,`/* 尽力 */`(2 字)已是完整交代,
|
|
163
|
-
// 但按「≥4 字符」的 ASCII 门槛会被判为「没交代」而误报(dsh-session-conductor 实测)。
|
|
164
|
-
// 判定:去符号后 ≥4 字符(ASCII 语境),或含 ≥2 个 CJK 字符(中文语境)。
|
|
165
|
-
const cjkCount = (noteText.match(/[\u4e00-\u9fa5]/g) || []).length;
|
|
166
|
-
if (/跳过|忽略|已断开|不抛|按空|不阻断|无需|降级|兜底|视为|不回滚|非\s*JSON|raw|空仓|best-effort/i.test(tk.value || '')
|
|
167
|
-
|| (noteText.length >= 4 && !/^[a-zA-Z_$][\w$]*$/.test(noteText))
|
|
168
|
-
|| cjkCount >= 2) hasNote = true;
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
hasBody = true;
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
if (!hasBody && !hasNote) out.push({ line: t.line });
|
|
175
|
-
}
|
|
176
|
-
return out;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export function checkComplexityAst(text = '', { warn = 10, block = 20 } = {}) {
|
|
180
|
-
const tokens = tokenize(text);
|
|
181
|
-
const out = [];
|
|
182
|
-
// 分支关键字(口径校准,用户提供标准):
|
|
183
|
-
// if/else if 每分支、for、while(普通 while 与 do-while 的 while 均计 1——
|
|
184
|
-
// do-while 只计 1:**do 不进分支集合**,do-while 由 while token 计)、case(switch
|
|
185
|
-
// 每个分支)、catch。**带标签 break/continue 额外 +1**(跨循环/switch 的复杂控制流,
|
|
186
|
-
// 由下方 labelled-break 判定处理)。
|
|
187
|
-
const branch = new Set(['if', 'for', 'while', 'case', 'catch']);
|
|
188
|
-
// Python 分支——def + 缩进块,走行级复杂度统计(token 级大括号法对 py 失效)
|
|
189
|
-
if (pythonDetectDef(text)) {
|
|
190
|
-
return checkPythonComplexity(text, { warn, block });
|
|
191
|
-
}
|
|
192
|
-
// Java/Kotlin 分支——方法无 function 关键字,JS token 级只认 function → 零命中;
|
|
193
|
-
// 复用 javaKtFuncRanges 的方法区间,对每个方法体做 token 级分支计数(与 JS 同口径)
|
|
194
|
-
const jr = javaKtFuncRanges(text);
|
|
195
|
-
if (jr.length) return checkJavaKtComplexity(tokens, jr, { warn, block });
|
|
196
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
197
|
-
const t = tokens[i];
|
|
198
|
-
if (t.type !== 'ident' || t.value !== 'function') continue;
|
|
199
|
-
let openIdx = -1;
|
|
200
|
-
for (let j = i + 1; j < Math.min(tokens.length, i + FN_BODY_LOOKAHEAD); j++) {
|
|
201
|
-
if (tokens[j].type === 'punct' && tokens[j].value === '{') { openIdx = j; break; }
|
|
202
|
-
}
|
|
203
|
-
if (openIdx === -1) continue;
|
|
204
|
-
const range = matchBrace(tokens, openIdx);
|
|
205
|
-
if (!range) continue;
|
|
206
|
-
// 修:嵌套函数的复杂度**不得累加到外层**(旧实现整段计入,导致
|
|
207
|
-
// 只要函数里定义了几个带分支的内部函数,外层就被虚报成高复杂度——如
|
|
208
|
-
// makeTitleResolver 自身仅 2 个 catch + 2 个 ??,却被报 29)。
|
|
209
|
-
// 做法:先收集内层函数体的 token 区间,扫外层时跳过这些区间;内层自身
|
|
210
|
-
// 由后续迭代单独评估(不再 i = range[1] 整体跳过,否则内层永远扫不到)。
|
|
211
|
-
const innerRanges = collectInnerFnRanges(tokens, openIdx + 1, range[1]);
|
|
212
|
-
const inInner = (k) => innerRanges.some(([a, b]) => k >= a && k < b);
|
|
213
|
-
let cx = 1;
|
|
214
|
-
for (let k = openIdx + 1; k < range[1]; k++) {
|
|
215
|
-
if (inInner(k)) continue;
|
|
216
|
-
const tk = tokens[k];
|
|
217
|
-
if (tk.type === 'comment') continue;
|
|
218
|
-
if (tk.type === 'ident' && branch.has(tk.value)) cx++;
|
|
219
|
-
// 分支点:逻辑与/或、空值合并、三元问号。**排除可选链 `?.`**——它是空值保护,
|
|
220
|
-
// 不产生独立执行路径(修:多字符 token 化后 `?.` 是独立 token,
|
|
221
|
-
// 不再被误认成三元 `?`)。
|
|
222
|
-
if (tk.type === 'punct' && ['&&', '||', '??', '?'].includes(tk.value)) cx++;
|
|
223
|
-
// 带标签 break/continue +1:break label; / continue label;——
|
|
224
|
-
// 跨循环/switch 的复杂控制流(跳出多层/跳到外层迭代),增加独立执行路径。
|
|
225
|
-
// 普通 break/continue(switch case 收尾、循环尾)不计——它们是常规流程。
|
|
226
|
-
if (tk.type === 'ident' && (tk.value === 'break' || tk.value === 'continue')) {
|
|
227
|
-
const nx = tokens[k + 1];
|
|
228
|
-
if (nx && nx.type === 'ident') cx++;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
if (cx > warn) {
|
|
232
|
-
const nx = tokens[i + 1];
|
|
233
|
-
out.push({ line: tokens[openIdx].line, name: nx?.type === 'ident' ? nx.value : '(匿名)', complexity: cx, level: cx > block ? 'blocker' : 'warning' });
|
|
234
|
-
}
|
|
235
|
-
// 不跳过内部:继续迭代把内层函数也评估掉(i 只前进到本函数声明之后)
|
|
236
|
-
i = openIdx;
|
|
237
|
-
}
|
|
238
|
-
return out;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* 检查嵌套深度(max-depth kind,AST 级)。
|
|
243
|
-
* 统计函数体内「控制流块」最大嵌套层数(if/for/while/try/switch 等)。
|
|
244
|
-
*
|
|
245
|
-
* 修 bug:旧实现把所有 `{` 都计入深度,**对象字面量/解构/JSON 结构**
|
|
246
|
-
* 被当成嵌套层级——`return { version: 2, skills: {}, sessions: {} }` 会被报
|
|
247
|
-
* depth=4,`try { ... } catch { return {...} }`(真实深度 2)也被报 depth=4,
|
|
248
|
-
* 造成大面积误报(一个仓库 19 条)。
|
|
249
|
-
* 现只统计「块语句」大括号:仅当 `{` 跟着控制流关键字或函数体时才算一层;
|
|
250
|
-
* 对象字面量(`{` 前是 `=`/`(`/`return`/`,`/`:`/`[` 等值位置)不计入。
|
|
251
|
-
* 块级关键字(计入):if / else / for / while / do / try / catch / finally / switch / function / =>。
|
|
252
|
-
* @param {string} text 文件全文
|
|
253
|
-
* @param {object} [opts] { warn=4, block=6 }
|
|
254
|
-
* @returns {Array<{line:number, depth:number, level:string}>}
|
|
255
|
-
*/
|
|
256
|
-
export function checkNestingDepthAst(text = '', { warn = 4, block = 6 } = {}) { // dsh-skip-complexity: 嵌套深度统计器本体(统计 if/for 嵌套必然自身嵌套)
|
|
257
|
-
// Python 分支——缩进块(无大括号),走行级嵌套统计
|
|
258
|
-
if (pythonDetectDef(text)) return checkPythonNesting(text, { warn, block });
|
|
259
|
-
const tokens = tokenize(text);
|
|
260
|
-
const out = [];
|
|
261
|
-
// 进入块的前置关键字(`if (...) {`、`else {`、`try {`、`=> {` 等)
|
|
262
|
-
const BLOCK_KW = new Set(['if', 'else', 'for', 'while', 'do', 'try', 'catch', 'finally', 'switch', 'function']);
|
|
263
|
-
// 统计每个 `{` 是否属于块语句(与 token 下标对齐)
|
|
264
|
-
const isBlockBrace = new Map();
|
|
265
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
266
|
-
const t = tokens[i];
|
|
267
|
-
if (!(t.type === 'punct' && t.value === '{')) continue;
|
|
268
|
-
// 向前找最近的有意义 token:若是 `)` → 可能 if/for/while/function 的参数尾(块)
|
|
269
|
-
// 或箭头函数参数尾(块);若直接是 BLOCK_KW/fn 关键字 → 块;
|
|
270
|
-
// 若是 `=>` → 箭头函数体(块);若是 `else`/`do`/`try`/`finally` → 块;
|
|
271
|
-
// 其余(`=` `(` `,` `:` `[` `return` `=>` 之外的值位置)→ 对象字面量,不计。
|
|
272
|
-
let prev = null;
|
|
273
|
-
for (let j = i - 1; j >= 0; j--) {
|
|
274
|
-
const tj = tokens[j];
|
|
275
|
-
if (tj.type === 'ws' || tj.type === 'comment') continue;
|
|
276
|
-
prev = tj;
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
let isBlock = false;
|
|
280
|
-
if (prev) {
|
|
281
|
-
if (prev.type === 'ident' && BLOCK_KW.has(prev.value)) isBlock = true; // if (...) / else ...? 见下
|
|
282
|
-
else if (prev.type === 'punct' && prev.value === '=>') isBlock = true; // () => {
|
|
283
|
-
else if (prev.type === 'punct' && prev.value === ')') {
|
|
284
|
-
// `)` 前是 BLOCK_KW 或函数名 → 块语句/函数体;否则可能是调用参数里传对象
|
|
285
|
-
let before = null;
|
|
286
|
-
for (let j = tokens.indexOf(prev) - 1; j >= 0; j--) {
|
|
287
|
-
const tj = tokens[j];
|
|
288
|
-
if (tj.type === 'ws' || tj.type === 'comment') continue;
|
|
289
|
-
before = tj;
|
|
290
|
-
break;
|
|
291
|
-
}
|
|
292
|
-
isBlock = isBlockParen(tokens, prev) || (before && before.type === 'ident' && before.value === 'function');
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
isBlockBrace.set(i, isBlock);
|
|
296
|
-
}
|
|
297
|
-
let depth = 0;
|
|
298
|
-
let maxDepth = 0;
|
|
299
|
-
let maxLine = 1;
|
|
300
|
-
// 归属判定:用 function 栈(起始行 + 其函数体深度),而不是「深度归零前最后一个 function 关键字」——
|
|
301
|
-
// 后者在模块级代码(不在任何函数里)时会把嵌套归到**已结束的旧函数**,整条规则误报。
|
|
302
|
-
const fnStack = []; // [{ startLine, bodyDepth }]
|
|
303
|
-
let pendingFnLine = 0; // 已见 function 关键字、等其函数体 { 入栈
|
|
304
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
305
|
-
const t = tokens[i];
|
|
306
|
-
if (t.type === 'ident' && t.value === 'function') pendingFnLine = t.line;
|
|
307
|
-
if (t.type === 'punct' && t.value === '{') {
|
|
308
|
-
if (isBlockBrace.get(i)) {
|
|
309
|
-
depth++;
|
|
310
|
-
if (pendingFnLine) { fnStack.push({ startLine: pendingFnLine, bodyDepth: depth }); pendingFnLine = 0; }
|
|
311
|
-
if (depth > maxDepth) { maxDepth = depth; maxLine = t.line; }
|
|
312
|
-
}
|
|
313
|
-
} else if (t.type === 'punct' && t.value === '}') {
|
|
314
|
-
// 只有块大括号才减(对象字面量的 `}` 不影响)
|
|
315
|
-
if (isBlockBrace.get(matchingOpen(tokens, i))) {
|
|
316
|
-
// 先判归属(栈顶函数此时可能正在结束,仍应归到它),再出栈
|
|
317
|
-
const endingFn = fnStack.length > 0 && fnStack[fnStack.length - 1].bodyDepth === depth;
|
|
318
|
-
depth--;
|
|
319
|
-
if (depth === 0 && maxDepth > warn) {
|
|
320
|
-
// 归属:有活跃函数(含正在结束的那个)→ 它的起始行;模块级代码 → 嵌套最深行(maxLine)
|
|
321
|
-
const owner = fnStack.length ? fnStack[fnStack.length - 1].startLine : maxLine;
|
|
322
|
-
out.push({ line: owner, depth: maxDepth, level: maxDepth > block ? 'blocker' : 'warning' });
|
|
323
|
-
}
|
|
324
|
-
if (endingFn) fnStack.pop();
|
|
325
|
-
if (depth <= 0) { depth = 0; maxDepth = 0; }
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
return out;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* 检测文本是否含 Python 函数定义(def ...:)——决定复杂度走行级统计。
|
|
334
|
-
* 纯 JS 项目(无 def)不受影响(零开销正则预检)。
|
|
335
|
-
*/
|
|
336
|
-
function pythonDetectDef(text) {
|
|
337
|
-
return /^\s*def\s+[A-Za-z_]\w*\s*\(/m.test(String(text));
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Python 圈复杂度(行级,)。
|
|
342
|
-
*
|
|
343
|
-
* Python 无大括号/function 关键字,token 级分支计数失效。按行统计:
|
|
344
|
-
* · 函数范围复用 pythonFuncRanges(def 行 + 缩进法确定函数体)
|
|
345
|
-
* · 分支点 = 函数体内**行首**(缩进后)的控制流关键字:if/elif/else/for/while/
|
|
346
|
-
* except/try/with/assert——每个 +1(与 JS 的 branch 集合对齐口径)
|
|
347
|
-
* · 逻辑运算符 &&/|| 在 Python 是 and/or(行内出现 +1);三元是 `x if c else y`
|
|
348
|
-
* (Python 无 `?`,不另计)
|
|
349
|
-
* · 嵌套函数复杂度不累加到外层(与 JS 版同语义:按 pythonFuncRanges 逐函数独立评估)
|
|
350
|
-
* 返回结构与 checkComplexityAst 一致({line, name, complexity, level})。
|
|
351
|
-
*/
|
|
352
|
-
function checkPythonComplexity(text, { warn, block }) {
|
|
353
|
-
const out = [];
|
|
354
|
-
const lines = String(text).split('\n');
|
|
355
|
-
// 行级分支(口径:if/elif 每分支、for/while、try/except(catch 语义)、
|
|
356
|
-
// with/assert 为 Python 语义保留;**else 不计**——else 是 if 的配对分支非新分支,
|
|
357
|
-
// elif 才是新分支,与 JS 版「if/else if 每分支」对齐)
|
|
358
|
-
const BRANCH_LINE = /^\s*(if|elif|for|while|try|except|with|assert)\b/;
|
|
359
|
-
const BOOL_OP = /\b(and|or)\b/;
|
|
360
|
-
for (const r of pythonFuncRanges(text)) {
|
|
361
|
-
let cx = 1;
|
|
362
|
-
for (let i = r.startLine - 1; i < r.endLine; i++) {
|
|
363
|
-
const line = lines[i];
|
|
364
|
-
if (!line) continue;
|
|
365
|
-
if (/^\s*#/.test(line)) continue; // 注释不计
|
|
366
|
-
if (BRANCH_LINE.test(line)) cx++;
|
|
367
|
-
// 行内 and/or(非注释非字符串粗判——行级精度足够,注释行已跳过)
|
|
368
|
-
const m = BOOL_OP.exec(line.replace(/^\s*#.*$/, ''));
|
|
369
|
-
if (m) cx++;
|
|
370
|
-
}
|
|
371
|
-
if (cx > warn) {
|
|
372
|
-
out.push({ line: r.startLine, name: r.name, complexity: cx, level: cx > block ? 'blocker' : 'warning' });
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
return out;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Python 嵌套深度(行级缩进法,)。
|
|
380
|
-
*
|
|
381
|
-
* Python 用缩进表达块结构(无大括号)。逐行统计**块起始行**(行首控制流关键字:
|
|
382
|
-
* if/elif/else/for/while/try/except/with),每个 +1 缩进层级;函数体(def)是
|
|
383
|
-
* 顶层容器不计入嵌套(与 JS 版「函数体不计深度」口径一致——JS 统计的是函数内
|
|
384
|
-
* 控制流块的嵌套,def 本身不是「控制流块」)。
|
|
385
|
-
* 返回 {line, depth, level} 与 JS 版一致;超过 warn 报 warning、超过 block 报 blocker。
|
|
386
|
-
*/
|
|
387
|
-
function checkPythonNesting(text, { warn, block }) {
|
|
388
|
-
const out = [];
|
|
389
|
-
const lines = String(text).split('\n');
|
|
390
|
-
const BLOCK_LINE = /^\s*(if|elif|else|for|while|try|except|with)\b/;
|
|
391
|
-
for (const r of pythonFuncRanges(text)) {
|
|
392
|
-
let depth = 0;
|
|
393
|
-
let maxDepth = 0;
|
|
394
|
-
let maxLine = r.startLine;
|
|
395
|
-
const bodyIndent = indentOf(lines[r.startLine - 1] || '') + 4; // def 缩进 + 一级
|
|
396
|
-
for (let i = r.startLine; i < r.endLine; i++) {
|
|
397
|
-
const line = lines[i];
|
|
398
|
-
if (!line || !line.trim()) continue;
|
|
399
|
-
if (/^\s*#/.test(line)) continue; // 注释不影响嵌套
|
|
400
|
-
const ind = indentOf(line);
|
|
401
|
-
// 缩进回到函数体外 → 函数结束(pythonFuncRanges 已保证不越界,防御性判断)
|
|
402
|
-
if (ind < bodyIndent) break;
|
|
403
|
-
if (BLOCK_LINE.test(line)) {
|
|
404
|
-
depth++;
|
|
405
|
-
if (depth > maxDepth) { maxDepth = depth; maxLine = i + 1; }
|
|
406
|
-
} else if (ind < depth * 4 + bodyIndent) {
|
|
407
|
-
// 缩进回退 → 块结束(近似:每级 4 空格;tab/混合缩进误差可接受)
|
|
408
|
-
depth = Math.max(0, Math.floor((ind - bodyIndent) / 4));
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (maxDepth > warn) {
|
|
412
|
-
out.push({ line: maxLine, depth: maxDepth, level: maxDepth > block ? 'blocker' : 'warning' });
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
return out;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/** 行首缩进空格数。 */
|
|
419
|
-
function indentOf(line) {
|
|
420
|
-
return (String(line).match(/^\s*/) || [''])[0].length;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* Java/Kotlin 圈复杂度(token 级,按方法区间)。
|
|
425
|
-
*
|
|
426
|
-
* Java/Kotlin 方法与 JS 一样用花括号分块、分支关键字一致(if/for/while/case/catch/&&/||/?),
|
|
427
|
-
* 唯一差异是无 `function` 关键字——方法范围由 javaKtFuncRanges 提供,逐方法在 token 流里
|
|
428
|
-
* 按 [openIdx, closeIdx] 区间计数分支点(与 JS 版 checkComplexityAst 同口径)。
|
|
429
|
-
* 内层 lambda/匿名类的大括号由外层方法区间包含(方法体确实包括它们),复杂度的
|
|
430
|
-
* 「分支点」只统计 branch 关键字与逻辑运算符,不把 lambda { 本身计分支——与 JS 一致。
|
|
431
|
-
* @param {Array} tokens token 流
|
|
432
|
-
* @param {Array<{startLine:number,endLine:number,name:string}>} ranges 方法范围
|
|
433
|
-
* @returns {Array<{line:number,name:string,complexity:number,level:string}>}
|
|
434
|
-
*/
|
|
435
|
-
function checkJavaKtComplexity(tokens, ranges, { warn, block }) {
|
|
436
|
-
const out = [];
|
|
437
|
-
// 与 JS 版同口径:if/else if 每分支、for、while(do-while 由 while 计 1,
|
|
438
|
-
// do 不进集合)、case、catch、when=Kotlin switch。带标签 break/continue 额外 +1。
|
|
439
|
-
const branch = new Set(['if', 'for', 'while', 'case', 'catch', 'when']); // when=Kotlin switch
|
|
440
|
-
// 方法体 token 区间([i,j])——按行号定位 tokens
|
|
441
|
-
for (const r of ranges) {
|
|
442
|
-
// 找方法体起点({ 行=r.startLine 的 token 下标)与结束(r.endLine 的最后一个 })
|
|
443
|
-
const startIdx = tokens.findIndex((t) => t.line === r.startLine);
|
|
444
|
-
if (startIdx === -1) continue;
|
|
445
|
-
// 从 startLine 向后找属于该方法的最后一个 } 之前的所有分支 token
|
|
446
|
-
let endIdx = -1;
|
|
447
|
-
for (let k = tokens.length - 1; k >= 0; k--) {
|
|
448
|
-
const tk = tokens[k];
|
|
449
|
-
if (tk.line === r.endLine && tk.type === 'punct' && tk.value === '}') { endIdx = k; break; }
|
|
450
|
-
}
|
|
451
|
-
if (endIdx === -1) continue;
|
|
452
|
-
let cx = 1;
|
|
453
|
-
for (let k = startIdx; k <= endIdx; k++) {
|
|
454
|
-
const tk = tokens[k];
|
|
455
|
-
if (tk.type === 'comment') continue;
|
|
456
|
-
if (tk.type === 'ident' && branch.has(tk.value)) cx++;
|
|
457
|
-
// 逻辑与/或/三元问号(Java 无 ??,Kotlin 有——同 JS 的 ? 分支语义;排除 ?. 可选链)
|
|
458
|
-
if (tk.type === 'punct' && ['&&', '||', '??', '?'].includes(tk.value)) cx++;
|
|
459
|
-
// 带标签 break/continue +1(break label; / continue label;——跨循环/switch 复杂控制流)
|
|
460
|
-
if (tk.type === 'ident' && (tk.value === 'break' || tk.value === 'continue')) {
|
|
461
|
-
const nx = tokens[k + 1];
|
|
462
|
-
if (nx && nx.type === 'ident') cx++;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
if (cx > warn) {
|
|
466
|
-
out.push({ line: r.startLine, name: r.name || '(匿名)', complexity: cx, level: cx > block ? 'blocker' : 'warning' });
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return out;
|
|
470
|
-
}
|