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/size.js
DELETED
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 规模检查
|
|
3
|
-
*
|
|
4
|
-
* 职责:函数长度、文件长度、重复字符串。
|
|
5
|
-
* 函数长度按括号平衡区间统计函数体内语句密度,单行海量语句也能识别。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { tokenize } from './tokenizer.js';
|
|
9
|
-
import { matchBrace, matchParenForward } from './brace.js';
|
|
10
|
-
import { javaKtFuncRanges } from './lang.js';
|
|
11
|
-
import { lineStats } from './line-count.js';
|
|
12
|
-
|
|
13
|
-
/** 函数体行数默认阈值:≥ WARN 报 warning,≥ BLOCK 报 blocker。 */
|
|
14
|
-
const FUNC_LINES_WARN = 50;
|
|
15
|
-
const FUNC_LINES_BLOCK = 100;
|
|
16
|
-
/** 从 `function` 关键字向后搜索函数体 `{` 的 token 窗口(覆盖 name(params ) { 形态)。 */
|
|
17
|
-
const FUNC_BODY_LOOKAHEAD_TOKENS = 20;
|
|
18
|
-
/** 行注释密度默认阈值(0~1——行注释占有效代码行比例,超过提示「注释复述代码」)。 */
|
|
19
|
-
const COMMENT_DENSITY_WARN_DEFAULT = 0.4;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 列出全文所有函数的**精确行范围**(AST 级,tokenizer 括号配对)。
|
|
23
|
-
*
|
|
24
|
-
* 与 checkFuncLinesAst 共用同一套边界算法,区别是:本函数返回**全部**函数(不只超限的),
|
|
25
|
-
* 供上层在此基础上做「语句密度」等派生判定,避免上层再自建第二套边界识别
|
|
26
|
-
* (历史教训:上层曾用正则找起点 + 自己数花括号,未剥离字面量,把 8 行函数算成 242 行 blocker)。
|
|
27
|
-
* @param {string} text 文件全文
|
|
28
|
-
* @returns {Array<{startLine:number, endLine:number, len:number}>} 按出现顺序
|
|
29
|
-
*/
|
|
30
|
-
/** 从 function 标记提取函数名:`function NAME(` 直接取;`const f = function(` 向前找声明名;否则 (匿名)。 */
|
|
31
|
-
function funcNameAt(tokens, i) {
|
|
32
|
-
const nx = tokens[i + 1];
|
|
33
|
-
if (nx && nx.type === 'ident') return nx.value;
|
|
34
|
-
if (nx && nx.type === 'punct' && nx.value === '(') {
|
|
35
|
-
// 向前找 const/let/var NAME =
|
|
36
|
-
for (let k = i - 1; k >= Math.max(0, i - 6); k--) {
|
|
37
|
-
const tk = tokens[k];
|
|
38
|
-
if (tk && tk.type === 'ident' && ['const', 'let', 'var'].includes(tk.value)) {
|
|
39
|
-
const nm = tokens[k + 1];
|
|
40
|
-
if (nm && nm.type === 'ident') return nm.value;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return '(匿名)';
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Python 函数范围收集(行级缩进法)。
|
|
49
|
-
*
|
|
50
|
-
* Python 无 `function` 关键字与大括号——`def name(...):` + 缩进定义函数体。
|
|
51
|
-
* funcRangesAst 的大括号配对法对 Python 完全失效,这里按行扫描:
|
|
52
|
-
* · `def name(...):` 行 = 函数起点
|
|
53
|
-
* · 函数体 = 后续缩进 > def 行缩进的行,直到缩进回到 def 缩进或更浅(或文件尾)
|
|
54
|
-
* · 行内对象/列表推导不产生缩进块,天然正确
|
|
55
|
-
* 返回 [{startLine, endLine, len, name}],与 funcRangesAst 输出结构一致,
|
|
56
|
-
* 由 funcRangesAst 合并(JS + Python 双语言函数范围)。
|
|
57
|
-
* @param {string} text 文件全文
|
|
58
|
-
* @returns {Array<{startLine:number, endLine:number, len:number, name:string}>}
|
|
59
|
-
*/
|
|
60
|
-
export function pythonFuncRanges(text = '') {
|
|
61
|
-
const out = [];
|
|
62
|
-
const lines = String(text).split('\n');
|
|
63
|
-
const DEF_RE = /^(\s*)def\s+([A-Za-z_]\w*)\s*\(/;
|
|
64
|
-
for (let i = 0; i < lines.length; i++) {
|
|
65
|
-
const m = DEF_RE.exec(lines[i]);
|
|
66
|
-
if (!m) continue;
|
|
67
|
-
const defIndent = m[1].length;
|
|
68
|
-
const name = m[2];
|
|
69
|
-
// 找函数体结束:后续首个「非空且缩进 <= defIndent」的行是函数外的行;函数体 = 其间行
|
|
70
|
-
let end = i;
|
|
71
|
-
for (let j = i + 1; j < lines.length; j++) {
|
|
72
|
-
const l = lines[j];
|
|
73
|
-
if (!l.trim()) continue; // 空行属于函数体(不结束)
|
|
74
|
-
const indent = (l.match(/^\s*/) || [''])[0].length;
|
|
75
|
-
// 注释行:`#` 顶格或在任何缩进——不算「函数外」,跳过(注释不打断函数体)
|
|
76
|
-
if (/^\s*#/.test(l)) continue;
|
|
77
|
-
if (indent <= defIndent) break;
|
|
78
|
-
end = j;
|
|
79
|
-
}
|
|
80
|
-
out.push({ startLine: i + 1, endLine: end + 1, len: end - i + 1, name });
|
|
81
|
-
i = end; // 跳过函数体,继续找下一个 def
|
|
82
|
-
}
|
|
83
|
-
return out;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 最大函数长度(行数,跨语言:JS/Python/Java/Kotlin 统一由 funcRangesAst 提供范围)。
|
|
88
|
-
* 供 file-health / max-lines 的「聚合型 vs 臃肿型」判定:最大函数 > 合规阈值 = 臃肿型
|
|
89
|
-
* (文件大是因为藏着大函数,该罚);全部函数合规(含无函数的数据文件)= 聚合型
|
|
90
|
-
* (文件大只是函数多,可接受不按规模扣分)。
|
|
91
|
-
* 注意:纯 JS 文件的 Java/Python 探测零命中,无额外开销(tokenize 有 LRU 缓存)。
|
|
92
|
-
* @param {string} text 文件全文
|
|
93
|
-
* @returns {number} 最大函数行数(无函数返回 0)
|
|
94
|
-
*/
|
|
95
|
-
export function maxFunctionLength(text = '') {
|
|
96
|
-
let max = 0;
|
|
97
|
-
for (const r of funcRangesAst(String(text))) {
|
|
98
|
-
if (r.len > max) max = r.len;
|
|
99
|
-
}
|
|
100
|
-
return max;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 不是「方法名」的标识符:控制关键字与语言关键字。
|
|
105
|
-
* 方法简写判定是「ident ( … ) {」形态,必须把这些排除,否则 `if (x) {` / `for (…) {`
|
|
106
|
-
* 会被当成函数体,把整段代码算进「最大函数长度」。
|
|
107
|
-
*/
|
|
108
|
-
const NOT_A_METHOD_NAME = new Set([
|
|
109
|
-
'if', 'for', 'while', 'switch', 'catch', 'with', 'else', 'do', 'try', 'finally',
|
|
110
|
-
'return', 'typeof', 'await', 'yield', 'new', 'delete', 'void', 'instanceof', 'in', 'of',
|
|
111
|
-
'case', 'default', 'function', 'class', 'import', 'export', 'super', 'this',
|
|
112
|
-
]);
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 箭头函数(块体)与方法简写的函数体范围。
|
|
116
|
-
*
|
|
117
|
-
* 为什么需要(2026-10-24 外部项目实测反馈):原先只认 `function` 关键字,于是
|
|
118
|
-
* `() => {…}`、`async () => {…}`、对象/类的方法简写 `m() {…}` **全部不计数** ⇒
|
|
119
|
-
* maxFunctionLength 返回 0。后果有两个,都是真缺陷:
|
|
120
|
-
* ① max-file-length / file-health 的「聚合型」判定要求「有函数且都合规」,
|
|
121
|
-
* maxFuncLen=0 会落进「臃肿型」,于是纯聚合形态的测试文件被报
|
|
122
|
-
* 「最大函数 0 行(臃肿型:文件大因藏着超大函数)」——文案自相矛盾且 scoreImpact=2;
|
|
123
|
-
* ② 真正很长的箭头函数从不被 max-function-length 报出(漏报)。
|
|
124
|
-
*
|
|
125
|
-
* 边界:
|
|
126
|
-
* · 只认**块体**箭头(`=> {`);表达式体(`=> x + 1`)不构成「长函数」;
|
|
127
|
-
* · 方法简写排除控制关键字与前缀为 `.` 的成员调用;
|
|
128
|
-
* · `function` 声明由 funcRangesAst 原分支处理,这里用 prev 判定跳过,避免重复计数。
|
|
129
|
-
*/
|
|
130
|
-
function arrowAndMethodRanges(tokens) {
|
|
131
|
-
const out = [];
|
|
132
|
-
const push = (range, name) => {
|
|
133
|
-
const startLine = tokens[range[0]].line;
|
|
134
|
-
const endLine = tokens[range[1]].line;
|
|
135
|
-
out.push({ startLine, endLine, len: endLine - startLine + 1, name });
|
|
136
|
-
};
|
|
137
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
138
|
-
const t = tokens[i];
|
|
139
|
-
// ① 箭头函数:`=>` 紧跟 `{`
|
|
140
|
-
if (t.type === 'punct' && t.value === '=>') {
|
|
141
|
-
const nx = tokens[i + 1];
|
|
142
|
-
if (nx && nx.type === 'punct' && nx.value === '{') {
|
|
143
|
-
const r = matchBrace(tokens, i + 1);
|
|
144
|
-
if (r) push(r, '(箭头)');
|
|
145
|
-
}
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
// ② 方法简写:ident '(' … ')' '{'
|
|
149
|
-
if (t.type !== 'ident' || NOT_A_METHOD_NAME.has(t.value)) continue;
|
|
150
|
-
const prev = tokens[i - 1];
|
|
151
|
-
if (prev && prev.type === 'punct' && prev.value === '.') continue; // 成员调用 a.b()
|
|
152
|
-
if (prev && prev.type === 'ident' && prev.value === 'function') continue; // function 声明已由原分支处理
|
|
153
|
-
const np = tokens[i + 1];
|
|
154
|
-
if (!np || np.type !== 'punct' || np.value !== '(') continue;
|
|
155
|
-
const closeParen = matchParenForward(tokens, i + 1);
|
|
156
|
-
if (closeParen < 0) continue;
|
|
157
|
-
const nb = tokens[closeParen + 1];
|
|
158
|
-
if (!nb || nb.type !== 'punct' || nb.value !== '{') continue;
|
|
159
|
-
const r = matchBrace(tokens, closeParen + 1);
|
|
160
|
-
if (r) push(r, t.value);
|
|
161
|
-
}
|
|
162
|
-
return out;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function funcRangesAst(text = '') {
|
|
166
|
-
const tokens = tokenize(text);
|
|
167
|
-
const out = [];
|
|
168
|
-
// 合并 Python 函数范围(缩进法)——Python 项目函数长度/复杂度/嵌套
|
|
169
|
-
// 规则依赖它(python/max-function-length 等此前对 py 全失效)。
|
|
170
|
-
for (const r of pythonFuncRanges(text)) out.push(r);
|
|
171
|
-
// Java/Kotlin 方法范围(方法签名 + 括号配对)——语言路由:JS 用 function 关键字、
|
|
172
|
-
// Python 按 def+缩进、Java/Kotlin 无声明关键字,只能靠「方法签名 + 括号配对」判定——
|
|
173
|
-
// Java/Kotlin 无 function 关键字,JS 大括号法零命中 → 由 lang.js 的
|
|
174
|
-
// javaKtFuncRanges 补充(轻量内容探测,纯 JS 项目不受影响)。
|
|
175
|
-
for (const r of javaKtFuncRanges(text)) {
|
|
176
|
-
// 与 JS 分支结果去重(极端混合文件里同名起点的保守处理)
|
|
177
|
-
if (!out.some((o) => o.startLine === r.startLine)) out.push(r);
|
|
178
|
-
}
|
|
179
|
-
// 箭头函数与方法简写:与 function 声明并列进同一份范围表
|
|
180
|
-
// (去重按 startLine:同一函数体中嵌套的箭头会被外层覆盖,取外层起点等价)
|
|
181
|
-
for (const r of arrowAndMethodRanges(tokens)) {
|
|
182
|
-
if (!out.some((o) => o.startLine === r.startLine)) out.push(r);
|
|
183
|
-
}
|
|
184
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
185
|
-
const t = tokens[i];
|
|
186
|
-
if (t.type !== 'ident' || t.value !== 'function') continue;
|
|
187
|
-
let openIdx = -1;
|
|
188
|
-
for (let j = i + 1; j < Math.min(tokens.length, i + FUNC_BODY_LOOKAHEAD_TOKENS); j++) {
|
|
189
|
-
const tj = tokens[j];
|
|
190
|
-
if (tj.type === 'punct' && tj.value === '{') { openIdx = j; break; }
|
|
191
|
-
if (tj.type === 'punct' && tj.value === ';') break;
|
|
192
|
-
}
|
|
193
|
-
if (openIdx === -1) continue;
|
|
194
|
-
const range = matchBrace(tokens, openIdx);
|
|
195
|
-
if (!range) continue;
|
|
196
|
-
const startLine = tokens[openIdx].line;
|
|
197
|
-
const endLine = tokens[range[1]].line;
|
|
198
|
-
out.push({ startLine, endLine, len: endLine - startLine + 1, name: funcNameAt(tokens, i) });
|
|
199
|
-
i = range[1];
|
|
200
|
-
}
|
|
201
|
-
return out;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* 统计指定行范围内的注释行数(多项检查共用:函数长度/文件长度的「非注释部分」口径)。
|
|
206
|
-
* 与 countCommentLines 同思路(comment token 按物理行去重、跨行块注释逐行覆盖),
|
|
207
|
-
* 但限定在 [startLine, endLine] 闭区间内。
|
|
208
|
-
* @param {Array} tokens tokenize 输出
|
|
209
|
-
* @param {number} startLine 起始物理行(含)
|
|
210
|
-
* @param {number} endLine 结束物理行(含)
|
|
211
|
-
* @returns {number} 范围内注释行数
|
|
212
|
-
*/
|
|
213
|
-
export function countCommentLinesInRange(tokens, startLine = 1, endLine = Infinity) {
|
|
214
|
-
const commentLines = new Set();
|
|
215
|
-
for (const t of tokens) {
|
|
216
|
-
if (t.type !== 'comment') continue;
|
|
217
|
-
const start = Number(t.line) || 1;
|
|
218
|
-
if (start < startLine || start > endLine) continue;
|
|
219
|
-
commentLines.add(start);
|
|
220
|
-
const newlines = String(t.value || '').match(/\n/g)?.length || 0;
|
|
221
|
-
for (let i = 1; i <= newlines; i++) {
|
|
222
|
-
if (start + i <= endLine) commentLines.add(start + i);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return commentLines.size;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* 检查单函数超长(AST 级:括号平衡精确统计函数体行数)。
|
|
230
|
-
* 判定按**非注释部分**(codeLen = 总行数 − 范围内注释行),
|
|
231
|
-
* 注释/文档行不占「代码规模」预算;总行数仍随返回供展示。
|
|
232
|
-
* @param {string} text 文件全文
|
|
233
|
-
* @param {{warn?: number, block?: number}} [opts]
|
|
234
|
-
* @returns {Array<{line:number, len:number, codeLen:number, commentLines:number, level:'warning'|'blocker'}>}
|
|
235
|
-
*/
|
|
236
|
-
export function checkFuncLinesAst(text = '', { warn = FUNC_LINES_WARN, block = FUNC_LINES_BLOCK } = {}) {
|
|
237
|
-
const tokens = tokenize(text);
|
|
238
|
-
const out = [];
|
|
239
|
-
// 统一走 funcRangesAst(JS 大括号 + Python 缩进双语言函数范围)——
|
|
240
|
-
// 旧内联扫描只认 `function`+`{`,Python def 函数长度规则(python/max-function-length)
|
|
241
|
-
// 对 py 文件全失效。funcRangesAst 已合并 pythonFuncRanges。
|
|
242
|
-
for (const r of funcRangesAst(text)) {
|
|
243
|
-
const startLine = r.startLine;
|
|
244
|
-
const endLine = r.endLine;
|
|
245
|
-
const len = endLine - startLine + 1;
|
|
246
|
-
const commentLines = countCommentLinesInRange(tokens, startLine, endLine);
|
|
247
|
-
const codeLen = Math.max(1, len - commentLines);
|
|
248
|
-
if (codeLen > warn) {
|
|
249
|
-
out.push({ line: startLine, name: r.name || '(匿名)', len, codeLen, commentLines, level: codeLen > block ? 'blocker' : 'warning' });
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return out;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/** 语句密度门槛(条/行):低于此值说明语句正常铺开分布,不算「单行海量语句」。 */
|
|
256
|
-
const STMT_DENSITY_MIN = 3;
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* 检查「单行/极少行堆叠海量语句」(AST 级,tokenizer 计数)。
|
|
260
|
-
*
|
|
261
|
-
* 为什么独立于行数判定:`function f() { a++; a++; … ×200 }` 挤在**一行**,行数=1 永远
|
|
262
|
-
* 超不过 50 行阈值,只能按**语句密度**(语句数 ÷ 函数体行数)识别。
|
|
263
|
-
*
|
|
264
|
-
* 为什么不与行数判定重复报:两处都基于同一份 AST 函数范围;本函数跳过「已被行数判定报出」
|
|
265
|
-
* 的函数(见 `reportedByLines`),避免同一函数在同一规则下产生两条 finding。
|
|
266
|
-
*
|
|
267
|
-
* 历史(误报):旧实现在 `lib/checks/` 层用正则找起点、自己数花括号、无条件套用
|
|
268
|
-
* 语句总数阈值 → 把 DSH 客户端 bundle 的单入口工厂 `factory: (require) => { … }`(整个
|
|
269
|
-
* 插件体都在这个箭头函数里,横跨上千行、语句数自然过百,密度却不到 1)误报为 blocker。
|
|
270
|
-
* 现加密度门槛,并把全部字符级操作收敛到 AST 层(tokenizer 计数,不经正则)。
|
|
271
|
-
*
|
|
272
|
-
* @param {string} text 文件全文
|
|
273
|
-
* @param {{threshold?: number, blockThreshold?: number, skipLines?: Set<number>}} [opts]
|
|
274
|
-
* skipLines:已被行数判定报出的函数起始行,不再重复报
|
|
275
|
-
* @returns {Array<{line:number, stmtCount:number, bodyLines:number, density:number, level:'warning'|'blocker'}>}
|
|
276
|
-
*/
|
|
277
|
-
export function checkFuncDensityAst(text = '', { threshold = FUNC_LINES_WARN, blockThreshold = FUNC_LINES_BLOCK, skipLines = new Set() } = {}) {
|
|
278
|
-
const tokens = tokenize(String(text));
|
|
279
|
-
const out = [];
|
|
280
|
-
for (const r of funcRangesAst(text)) {
|
|
281
|
-
if (skipLines.has(r.startLine)) continue;
|
|
282
|
-
const bodyLines = Math.max(1, r.len);
|
|
283
|
-
// 语句数 = 该函数行范围内的分号 token 数(tokenizer 已剥离字符串/正则/注释内的分号)
|
|
284
|
-
let stmtCount = 0;
|
|
285
|
-
for (const t of tokens) {
|
|
286
|
-
if (t.type !== 'punct' || t.value !== ';') continue;
|
|
287
|
-
if (t.line >= r.startLine && t.line <= r.endLine) stmtCount += 1;
|
|
288
|
-
}
|
|
289
|
-
const density = stmtCount / bodyLines;
|
|
290
|
-
if (stmtCount > threshold && density > STMT_DENSITY_MIN) {
|
|
291
|
-
out.push({
|
|
292
|
-
line: r.startLine, stmtCount, bodyLines, density,
|
|
293
|
-
level: stmtCount > blockThreshold ? 'blocker' : 'warning',
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
return out;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* 统计注释行数(注释行单独算):基于 tokenizer 的 comment token,
|
|
302
|
-
* 对跨行块注释按换行拆分后按「物理行号去重」计注释行数。
|
|
303
|
-
* @param {string} text 文件全文
|
|
304
|
-
* @returns {number} 注释行数(0 = 无注释/无法解析)
|
|
305
|
-
*/
|
|
306
|
-
export function countCommentLines(text = '') {
|
|
307
|
-
const tokens = tokenize(String(text));
|
|
308
|
-
const commentLines = new Set();
|
|
309
|
-
for (const t of tokens) {
|
|
310
|
-
if (t.type !== 'comment') continue;
|
|
311
|
-
const v = String(t.value || '');
|
|
312
|
-
const start = Number(t.line) || 1;
|
|
313
|
-
commentLines.add(start);
|
|
314
|
-
// 跨行块注释:从起始行起按换行推进,覆盖到的每行都算注释行
|
|
315
|
-
const newlines = v.match(/\n/g)?.length || 0;
|
|
316
|
-
for (let i = 1; i <= newlines; i++) commentLines.add(start + i);
|
|
317
|
-
}
|
|
318
|
-
return commentLines.size;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* 检查文件行数(max-lines kind)。
|
|
323
|
-
* 判定默认按**非注释部分**(codeLines = 总行数 − 注释行数),
|
|
324
|
-
* 注释/文档行不占文件规模预算;总行数/注释行数仍随返回供展示。
|
|
325
|
-
* @param {string} text 文件全文
|
|
326
|
-
* @param {object} [opts] { warn=500, block=1000, excludeComments=true }
|
|
327
|
-
* excludeComments=false 时回退旧口径(按总行数判定)。
|
|
328
|
-
* @returns {{lines:number, codeLines:number, commentLines:number, level:string|null}}
|
|
329
|
-
*/
|
|
330
|
-
export function checkFileLines(text = '', { warn = 500, block = 1000, excludeComments = true } = {}) {
|
|
331
|
-
// 行数口径走**单一来源** lib/ast/line-count.js(代码行 = 总行 − 纯注释行 − 空行)——
|
|
332
|
-
// 此前只减注释行、**漏减空行**,同一文件在 file-health 与 max-file-length 两条规则里行数对不上。
|
|
333
|
-
const stats = lineStats(text);
|
|
334
|
-
const lines = stats.total;
|
|
335
|
-
const commentLines = stats.comment;
|
|
336
|
-
const codeLines = excludeComments ? stats.code : lines;
|
|
337
|
-
if (codeLines <= warn) return { lines, codeLines, commentLines, level: null };
|
|
338
|
-
return { lines, codeLines, commentLines, level: codeLines > block ? 'blocker' : 'warning' };
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* 注释冗余检测(评审规则①)。
|
|
343
|
-
*
|
|
344
|
-
* 语义:**变量即注释**——语义明确的代码里变量名已表达内容,额外的行注释并不加深
|
|
345
|
-
* 理解;应只保留具有隐性约束、复杂设计、外部契约、非显然决策的块注释,同时减少
|
|
346
|
-
* 行注释。注释应解释「为什么」而非复述「做了什么」。
|
|
347
|
-
*
|
|
348
|
-
* 判据(保守,防误伤合理注释):
|
|
349
|
-
* · 统计**行注释**行数(独立 `//` / `#` 行,非行尾注释)÷ 有效代码行数;
|
|
350
|
-
* · 行注释密度 > 阈值(默认 0.4,即行注释占代码行 40% 以上)→ 提示
|
|
351
|
-
* 「行注释过多,可能复述代码」;
|
|
352
|
-
* · **块注释**(斜杠星号文档注释、Python docstring)不计入——那是设计说明,
|
|
353
|
-
* 评审要求保留;
|
|
354
|
-
* · 连续注释块(≥3 行同属一个逻辑说明)不触发——大段注释往往是设计文档;
|
|
355
|
-
* · 只提示(warning),不拦截。
|
|
356
|
-
*
|
|
357
|
-
* @param {string} text 文件全文
|
|
358
|
-
* @param {{warn?:number}} [opts] warn=行注释密度阈值(0~1)
|
|
359
|
-
* @returns {Array<{line:number, commentLines:number, codeLines:number, ratio:number}>}
|
|
360
|
-
*/
|
|
361
|
-
export function checkCommentDensityAst(text = '', { warn = COMMENT_DENSITY_WARN_DEFAULT, minCodeLines = 5 } = {}) {
|
|
362
|
-
const lines = String(text).split('\n');
|
|
363
|
-
let commentLines = 0;
|
|
364
|
-
let codeLines = 0;
|
|
365
|
-
let inBlockComment = false;
|
|
366
|
-
const hits = [];
|
|
367
|
-
for (let i = 0; i < lines.length; i++) {
|
|
368
|
-
const raw = lines[i];
|
|
369
|
-
const line = raw.trim();
|
|
370
|
-
// 跨行块注释(/* ... */ 未闭合)
|
|
371
|
-
if (inBlockComment) {
|
|
372
|
-
if (line.includes('*/')) inBlockComment = false;
|
|
373
|
-
continue; // 块注释不计入行注释密度
|
|
374
|
-
}
|
|
375
|
-
if (line.startsWith('/*')) { if (!line.includes('*/')) inBlockComment = true; continue; }
|
|
376
|
-
// 文档注释 `/** */` 单行、`///` 也算块/文档注释不计(设计说明保留)
|
|
377
|
-
if (/^\/\*\*/.test(line) || /^\/\/\//.test(line)) continue;
|
|
378
|
-
// 行注释(// 或 # 开头;# 排除 shebang)——**行注释无论是否连续都计入**:
|
|
379
|
-
// 修:评审要求是「减少行注释、保留块注释」——连续行注释是
|
|
380
|
-
// 复述代码的高发区(每行注释对应一行简单语句),不是设计说明,count 不豁免。
|
|
381
|
-
if (/^(\/\/|#)/.test(line) && !/^#!/.test(line)) {
|
|
382
|
-
commentLines++;
|
|
383
|
-
} else if (line) {
|
|
384
|
-
// 代码行(非空、非注释)
|
|
385
|
-
codeLines++;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
// 短文件(代码行 < minCodeLines)不判定:文件头说明/简述占比天然高且篇幅小,
|
|
389
|
-
// 「注释复述代码」是长文件信号(行注释随代码量线性堆积时才该提示)。
|
|
390
|
-
if (codeLines < minCodeLines) return [];
|
|
391
|
-
const ratio = commentLines / codeLines;
|
|
392
|
-
if (ratio > warn) {
|
|
393
|
-
hits.push({ line: 1, commentLines, codeLines, ratio });
|
|
394
|
-
}
|
|
395
|
-
return hits;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* 检查重复出现的硬编码字符串/模板串(repeated-string / min-occurrences kind)。
|
|
400
|
-
* 数值字面量(num)不参与统计——版本号/端口/阈值等数字重复属正常,
|
|
401
|
-
* 按「硬编码文本」报会造成大面积噪音(如 README 里的 10/15)。
|
|
402
|
-
* @param {string} text 文件全文
|
|
403
|
-
* @param {object} [opts] { min=3, ignore=['', ' ', '\\n', '-', '/', '0', '1'] }
|
|
404
|
-
* @returns {Array<{line:number, value:string, count:number}>}
|
|
405
|
-
*/
|
|
406
|
-
export function checkRepeatedStringsAst(text = '', { min = 3, ignore = [] } = {}) {
|
|
407
|
-
const tokens = tokenize(text);
|
|
408
|
-
// tokenizer 产出的字面量类型名是 str / tmpl / num(非 string/number);
|
|
409
|
-
// 类型名不匹配会让本检查永不命中,故按实际类型名收集;num 刻意排除(见上)。
|
|
410
|
-
// tmpl(模板字符串)也排除:模板串常用于「生成代码/拼接文本」(如垫片 harness 里
|
|
411
|
-
// 重复出现的 ` };` 缩进),其内部重复是生成内容而非「应配置化的硬编码值」,
|
|
412
|
-
// 统计它会把生成代码的排版误报成重复字面量(2026-10-17 实测误报)。
|
|
413
|
-
const LITERAL_TYPES = new Set(['str']);
|
|
414
|
-
const ignoreSet = new Set(['', ' ', '\n', '-', '/', '0', '1', 'utf8', 'string', 'number', 'boolean', 'object', 'function', ...ignore]);
|
|
415
|
-
const counts = new Map();
|
|
416
|
-
for (const t of tokens) {
|
|
417
|
-
if (!LITERAL_TYPES.has(t.type)) continue;
|
|
418
|
-
// 去掉字面量外层引号('' "" ``),计数与展示都用裸值
|
|
419
|
-
const v = String(t.value).replace(/^(['"`])([\s\S]*)\1$/, '$2');
|
|
420
|
-
if (ignoreSet.has(v) || v.length < 4) continue;
|
|
421
|
-
// ① 纯标识符形状(含下划线):kind 名/分派词(credential-ref、code_audit 等),重复属正常
|
|
422
|
-
if (/^[a-z0-9][a-z0-9_-]{0,19}$/i.test(v)) continue;
|
|
423
|
-
// ①b dotfile 名(.git/.dsh/.env 等):路径/目录域名词汇,重复属正常
|
|
424
|
-
if (/^\.[a-z0-9_-]{1,16}$/i.test(v)) continue;
|
|
425
|
-
// ①c i18n 键/配置键(common.cancel / menu.save_as / zh-CN.foo / api.retry_times /
|
|
426
|
-
// downloader.files_netloc):点分小写链是字典/配置引用键(多处引用同一键是正常
|
|
427
|
-
// 用法,不是「应配置化的硬编码值」——键本身就是配置)。允许 snake_case 段
|
|
428
|
-
// (Python 配置键惯例, 二次修:下划线段 files_netloc 此前漏豁免)。
|
|
429
|
-
// 判据:全小写 + 点分 2~4 段 + 每段 [a-z][a-z0-9_]*(允许下划线)+ 段可含连字符;
|
|
430
|
-
// 排除域名特征——末段是域名后缀(com/net/org/io 等)或含 example/test/localhost
|
|
431
|
-
// 段的是真实业务/URL。`api.scheme`(配置字段名)算配置键豁免。
|
|
432
|
-
if (/^[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*){1,3}$/.test(v)
|
|
433
|
-
&& !/\.(com|net|org|io|dev|cn|co|example|test|localhost)$/.test(v)
|
|
434
|
-
&& !/(^|\.)(example|test|localhost|your|demo)\./.test(v)) continue;
|
|
435
|
-
// ①d 路由/端点路径(/naming /session /api/foo):路径段是小写词(含连字符),
|
|
436
|
-
// 重复是「多处引用同一路由常量」的正常形态(路由表/链接/匹配共用),非硬编码值。
|
|
437
|
-
// 排除含 `..`/空白/大写的真实路径与查询串(?a=b 是数据不是路由名)。
|
|
438
|
-
if (/^\/[a-z0-9][a-z0-9/_-]*$/.test(v) && !v.includes('..') && !/[\s?]/.test(v)) continue;
|
|
439
|
-
// ② 短期望词(2-4 个汉字):可读性/可维护性/安全性 等维度名,重复属正常
|
|
440
|
-
if (/^[\u4e00-\u9fff]{2,4}$/.test(v)) continue;
|
|
441
|
-
// ③ 无值型特征(不含 ./:\ 空格/CJK)的短串:\n、-c、-q、[FUNC] 属控制串/标志,不报
|
|
442
|
-
// 值型特征 = 含 路径分隔/点/冒号/反斜杠/空白/CJK —— 才像"硬编码值"
|
|
443
|
-
if (!/[\s./:\\\u4e00-\u9fff]/.test(v)) continue;
|
|
444
|
-
if (!counts.has(v)) counts.set(v, { value: v, count: 0, line: t.line });
|
|
445
|
-
counts.get(v).count++;
|
|
446
|
-
}
|
|
447
|
-
return [...counts.values()].filter((x) => x.count >= min);
|
|
448
|
-
}
|