dsh-git-push 2.20.19 → 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.
Files changed (196) hide show
  1. package/README.md +3 -2
  2. package/cli-tool.mjs +2 -4
  3. package/cli.mjs +7 -1
  4. package/lib/app/apply.js +4 -4
  5. package/lib/app/command-registry.js +22 -17
  6. package/lib/app/handlers/meta.js +2 -2
  7. package/lib/app/handlers/preprocess.js +107 -0
  8. package/lib/app/handlers/repo-actions.js +2 -2
  9. package/lib/app/http-handlers.js +13 -42
  10. package/lib/app/index.js +2 -1
  11. package/lib/app/inject-text.js +3 -3
  12. package/lib/app/preprocess-client.js +136 -0
  13. package/lib/app/repo-ownership.js +1 -1
  14. package/lib/app/schema.js +3 -3
  15. package/lib/app/severity.js +14 -0
  16. package/lib/app/slash-commands.js +5 -164
  17. package/lib/app/tool-call.js +73 -144
  18. package/lib/cli/commands-preprocess.mjs +74 -0
  19. package/lib/cli/commands-tool.mjs +3 -2
  20. package/lib/cli/commands-vcs.mjs +4 -4
  21. package/lib/cli/shared.mjs +35 -9
  22. package/lib/commit-push/pre-commit-engine.js +57 -10
  23. package/lib/commit-push.js +71 -27
  24. package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
  25. package/lib/git/ignore.js +1 -1
  26. package/lib/git/repos.js +2 -2
  27. package/lib/git/sensitive.js +1 -1
  28. package/lib/npm/publish.js +20 -5
  29. package/lib/plugin/auto-push.js +2 -2
  30. package/lib/preprocess/providers.js +191 -0
  31. package/lib/self/index.js +1 -1
  32. package/lib/skip-dirs.js +14 -55
  33. package/package.json +29 -37
  34. package/scripts/build-client.mjs +2 -0
  35. package/scripts/check.mjs +4 -2
  36. package/scripts/local-registry.mjs +2 -1
  37. package/cli-audit.mjs +0 -141
  38. package/lib/app/audit-api.js +0 -173
  39. package/lib/app/handlers/audit.js +0 -150
  40. package/lib/app/host-doc-checks.js +0 -25
  41. package/lib/app/scope-check.js +0 -69
  42. package/lib/ast/brace.js +0 -120
  43. package/lib/ast/callgraph.js +0 -135
  44. package/lib/ast/code-lines.js +0 -175
  45. package/lib/ast/consts.js +0 -12
  46. package/lib/ast/control-flow.js +0 -470
  47. package/lib/ast/credential.js +0 -315
  48. package/lib/ast/dataflow.js +0 -391
  49. package/lib/ast/dup-code.js +0 -165
  50. package/lib/ast/fs-hotspots.js +0 -154
  51. package/lib/ast/index.js +0 -38
  52. package/lib/ast/io-risk-const.js +0 -95
  53. package/lib/ast/io-risk-fn.js +0 -208
  54. package/lib/ast/io-risk-loop.js +0 -308
  55. package/lib/ast/io-risk.js +0 -314
  56. package/lib/ast/lang.js +0 -156
  57. package/lib/ast/line-count.js +0 -52
  58. package/lib/ast/magic-number.js +0 -272
  59. package/lib/ast/naming.js +0 -292
  60. package/lib/ast/perf.js +0 -488
  61. package/lib/ast/scope-collisions.js +0 -228
  62. package/lib/ast/scope.js +0 -241
  63. package/lib/ast/shell.js +0 -131
  64. package/lib/ast/size.js +0 -448
  65. package/lib/ast/symbol-index.js +0 -399
  66. package/lib/ast/tokenizer.js +0 -421
  67. package/lib/ast/treesitter.js +0 -413
  68. package/lib/audit/analysis-coverage.js +0 -98
  69. package/lib/audit/audit-file.js +0 -205
  70. package/lib/audit/auditignore-rules.js +0 -148
  71. package/lib/audit/checks.js +0 -17
  72. package/lib/audit/client-entry.js +0 -93
  73. package/lib/audit/collector.js +0 -610
  74. package/lib/audit/ext-runner.js +0 -80
  75. package/lib/audit/file-context.js +0 -391
  76. package/lib/audit/finding.js +0 -101
  77. package/lib/audit/gitignore-match.js +0 -113
  78. package/lib/audit/glob.js +0 -63
  79. package/lib/audit/history-report.js +0 -100
  80. package/lib/audit/history.js +0 -126
  81. package/lib/audit/ignore-blind.js +0 -102
  82. package/lib/audit/index.js +0 -33
  83. package/lib/audit/orchestrate.js +0 -672
  84. package/lib/audit/public.js +0 -24
  85. package/lib/audit/repo-level.js +0 -30
  86. package/lib/audit/report-yaml.js +0 -90
  87. package/lib/audit/scope-policy.js +0 -110
  88. package/lib/audit/slot.js +0 -66
  89. package/lib/audit-rules/audit-rules-comment.yml +0 -199
  90. package/lib/audit-rules/audit-rules-cpp.yml +0 -114
  91. package/lib/audit-rules/audit-rules-csharp.yml +0 -82
  92. package/lib/audit-rules/audit-rules-docs.yml +0 -32
  93. package/lib/audit-rules/audit-rules-dsh.yml +0 -177
  94. package/lib/audit-rules/audit-rules-filehealth.yml +0 -42
  95. package/lib/audit-rules/audit-rules-folder.yml +0 -99
  96. package/lib/audit-rules/audit-rules-frontend.yml +0 -335
  97. package/lib/audit-rules/audit-rules-go.yml +0 -103
  98. package/lib/audit-rules/audit-rules-i18n.yml +0 -45
  99. package/lib/audit-rules/audit-rules-java.yml +0 -105
  100. package/lib/audit-rules/audit-rules-nodejs.yml +0 -662
  101. package/lib/audit-rules/audit-rules-npm.yml +0 -187
  102. package/lib/audit-rules/audit-rules-paths.yml +0 -98
  103. package/lib/audit-rules/audit-rules-performance.yml +0 -245
  104. package/lib/audit-rules/audit-rules-php.yml +0 -101
  105. package/lib/audit-rules/audit-rules-private.yml +0 -24
  106. package/lib/audit-rules/audit-rules-python.yml +0 -106
  107. package/lib/audit-rules/audit-rules-robustness.yml +0 -31
  108. package/lib/audit-rules/audit-rules-rust.yml +0 -109
  109. package/lib/audit-rules/audit-rules-structure.yml +0 -158
  110. package/lib/audit-rules/audit-rules-swift.yml +0 -99
  111. package/lib/audit-rules/audit-rules-template.yml +0 -108
  112. package/lib/audit-ui-sdk/contract.js +0 -117
  113. package/lib/audit-ui-sdk/index.js +0 -17
  114. package/lib/checks/button-bind.js +0 -347
  115. package/lib/checks/common.js +0 -170
  116. package/lib/checks/credential-file.js +0 -43
  117. package/lib/checks/dataflow.js +0 -40
  118. package/lib/checks/dispatch.js +0 -117
  119. package/lib/checks/dup-code.js +0 -69
  120. package/lib/checks/dup-const.js +0 -232
  121. package/lib/checks/file-health.js +0 -150
  122. package/lib/checks/filter.js +0 -137
  123. package/lib/checks/folder.js +0 -173
  124. package/lib/checks/fs-scanner.js +0 -57
  125. package/lib/checks/hardcoded-path.js +0 -98
  126. package/lib/checks/index.js +0 -49
  127. package/lib/checks/io.js +0 -66
  128. package/lib/checks/magic-number.js +0 -42
  129. package/lib/checks/npm-json.js +0 -273
  130. package/lib/checks/private.js +0 -64
  131. package/lib/checks/regex.js +0 -298
  132. package/lib/checks/scope-collision.js +0 -68
  133. package/lib/checks/semantic.js +0 -134
  134. package/lib/checks/structural.js +0 -281
  135. package/lib/cli/commands-audit.mjs +0 -142
  136. package/lib/cli/commands-doc.mjs +0 -127
  137. package/lib/client-parts/audit/audit.js +0 -301
  138. package/lib/exempt/tool.js +0 -198
  139. package/lib/git/module-splitter.js +0 -47
  140. package/lib/link-check/index.js +0 -187
  141. package/lib/lint-adapter/detect.js +0 -161
  142. package/lib/lint-adapter/index.js +0 -50
  143. package/lib/lint-adapter/runner.js +0 -133
  144. package/lib/preprocess/audit.js +0 -98
  145. package/lib/preprocess/index.js +0 -78
  146. package/lib/rule/compilers/credential.js +0 -62
  147. package/lib/rule/compilers/dataflow.js +0 -38
  148. package/lib/rule/compilers/file-health.js +0 -75
  149. package/lib/rule/compilers/frontend.js +0 -86
  150. package/lib/rule/compilers/func.js +0 -35
  151. package/lib/rule/compilers/helpers.js +0 -71
  152. package/lib/rule/compilers/index.js +0 -29
  153. package/lib/rule/compilers/link-semantic.js +0 -45
  154. package/lib/rule/compilers/list-kinds.js +0 -12
  155. package/lib/rule/compilers/numeric-kinds.js +0 -73
  156. package/lib/rule/compilers/regex.js +0 -78
  157. package/lib/rule/compilers/structure.js +0 -107
  158. package/lib/rule/compilers.js +0 -17
  159. package/lib/rule/homoglyph.js +0 -45
  160. package/lib/rule/loader.js +0 -274
  161. package/lib/rule/registry.js +0 -122
  162. package/lib/rule/scope.js +0 -67
  163. package/lib/score/docs-score.js +0 -166
  164. package/lib/score/index.js +0 -234
  165. package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
  166. package/scripts/audit-ext/_example-readme-present.mjs +0 -34
  167. package/scripts/audit-ext/dsh-inject-declarations.mjs +0 -209
  168. package/scripts/audit-ext/variable-min-length.mjs +0 -102
  169. package/scripts/audit-gate.mjs +0 -235
  170. package/scripts/audit-runner.mjs +0 -35
  171. package/scripts/audit-runtime-check.mjs +0 -129
  172. package/scripts/browser-page-probe.mjs +0 -271
  173. package/scripts/check-js-scope.mjs +0 -87
  174. package/scripts/clean-date-comments.py +0 -102
  175. package/scripts/cpu-sample.mjs +0 -188
  176. package/scripts/doc-drift.mjs +0 -200
  177. package/scripts/hot-path-probe-cjs.cjs +0 -71
  178. package/scripts/hot-path-probe-fs-shim.mjs +0 -28
  179. package/scripts/hot-path-probe-fsp-shim.mjs +0 -31
  180. package/scripts/hot-path-probe.mjs +0 -153
  181. package/scripts/module-splitter.py +0 -800
  182. package/scripts/picker-repro.mjs +0 -115
  183. package/scripts/platform-scan.mjs +0 -168
  184. package/scripts/probe-recheck.mjs +0 -47
  185. package/scripts/rename-locator.mjs +0 -97
  186. package/scripts/rule-switch.mjs +0 -97
  187. package/scripts/rules-solo-audit.mjs +0 -173
  188. package/scripts/scan-file-io.mjs +0 -717
  189. package/scripts/scan-fs-hotspots.mjs +0 -107
  190. package/scripts/symbol-index.mjs +0 -85
  191. package/scripts/verify-prestep.mjs +0 -100
  192. package/skills/dev/audit-rule-authoring.md +0 -58
  193. package/skills/guide/guide-audit.md +0 -270
  194. /package/lib/{audit → app}/ownership.js +0 -0
  195. /package/lib/app/{schema-audit.js → schema-preprocess.js} +0 -0
  196. /package/lib/{audit-defaults.js → git/defaults.js} +0 -0
@@ -1,43 +0,0 @@
1
- /**
2
- * 检查层 · 凭据文件检查
3
- *
4
- * 职责:识别并检查仓库中的凭据类文件(.env、密钥文件等)。
5
- */
6
-
7
- import { readdirSync, statSync, existsSync, readFileSync } from 'node:fs';
8
- import { join } from 'node:path';
9
- import { makeFinding } from '../audit/index.js';
10
- import { capSeverity } from './common.js';
11
-
12
- /** 检查文件路径规则(credential-file:私钥/凭据文件路径命中)。 */
13
- export function checkCredentialFiles({ file, relPath, rules }) {
14
- const findings = [];
15
- const target = String(relPath || file || '');
16
- const base = target.split(/[/\\]/).pop() || '';
17
- for (const rule of rules || []) {
18
- // bugfix:compilers/credential.js 对 credfile-* 规则编译输出的是
19
- // pathPattern 字段(取自 yml path_pattern),但这里原来只读 patterns/pattern
20
- // → .env / 密钥目录等凭据文件路径规则永远不触发(审计变动文件拦截形同虚设)。
21
- // 现在两种形态都认:pathPattern 优先,patterns/pattern 兼容旧规则。
22
- const pats = rule.pathPattern ? [rule.pathPattern] : (rule.patterns || (rule.pattern ? [rule.pattern] : []));
23
- for (const p of pats) {
24
- let re;
25
- try { re = p instanceof RegExp ? p : new RegExp(String(p), 'i'); } catch { continue; }
26
- if (re.test(target) || re.test(base)) {
27
- // severity 契约对齐 checkPathRegexRules——yml error 级凭据文件
28
- // (编译后 level=blocker)必须升 blocker 拦截提交;warning 级保持 warning。
29
- // 原来 capSeverity('error','blocker') 恒返回 'error',runAudit 只认 blocker → 永不拦截。
30
- findings.push(makeFinding({
31
- file, line: 1, rule: rule.id, kind: 'credential-file',
32
- severity: rule.level === 'blocker' ? 'blocker' : capSeverity(rule.severity, 'warning'),
33
- message: rule.message || `凭据/私钥类文件:${base}`,
34
- dimensions: rule.dimensions || ['安全性'],
35
- exemptHint: 'dsh-skip-sensitive(文件头=整文件 / 行尾=本行)',
36
- scoreImpact: 2,
37
- }));
38
- break;
39
- }
40
- }
41
- }
42
- return findings;
43
- }
@@ -1,40 +0,0 @@
1
- /**
2
- * 检查层 · 数据流规则(三层审计 L2)
3
- *
4
- * 职责:薄包装 lib/ast/dataflow.js 的 checkClearAccessAst(token 级同函数
5
- * 数据流判定),把命中转成统一 finding。
6
- *
7
- * 三层对应:
8
- * L1 正则初筛:filterRulesByFileText 用规则声明的 file_patterns 先筛候选文件,
9
- * 未命中 → grouped['dataflow'] 为空数组 → 本检查器空规则短路直接 return。
10
- * L2 本检查器:只对候选文件跑 AST 数据流分析(同函数清空后访问)。
11
- * L3 运行时:scripts/audit-runtime-check.mjs 兜跨文件/闭包/异步盲区。
12
- *
13
- * 安全:空 rules 短路(防 rule.severity 崩溃——与 structural.js 各检查器同款守卫)。
14
- */
15
-
16
- import { makeFinding } from '../audit/index.js';
17
- import { checkClearAccessAst } from '../ast/index.js';
18
- import { HINT_QUALITY } from './common.js';
19
-
20
- /**
21
- * 数据流检查(kind=dataflow):同函数「清空后访问」。
22
- * @param {object} ctx { file, text, rules }
23
- * @returns {Array} findings
24
- */
25
- export function checkDataflow({ file, text, rules }) {
26
- const findings = [];
27
- const rule = (rules || [])[0];
28
- if (!rule) return findings; // 规则被 L1 file_patterns 初筛剔除 / exts 过滤为空时短路
29
- for (const hit of checkClearAccessAst(text)) {
30
- findings.push(makeFinding({
31
- file, line: hit.line, rule: rule.id || 'dataflow/clear-then-access', kind: 'dataflow',
32
- severity: rule.severity || 'warning',
33
- message: `「${hit.obj}」先被清空(${hit.clearOp},行 ${hit.clearLine}),同函数内随后访问 ${hit.accessOp}——可能读到已清空对象(undefined/空);若中间有重新填充或跨分支时序,请用运行时检测(L3)确认`,
34
- dimensions: rule.dimensions || ['健壮性'],
35
- exemptHint: HINT_QUALITY,
36
- scoreImpact: 1,
37
- }));
38
- }
39
- return findings;
40
- }
@@ -1,117 +0,0 @@
1
- /**
2
- * 检查层 · 调度(runChecks)
3
- *
4
- * ⚠️ 本文件只写「引用」——按 kind 依次调用各检查器并汇总结果,不含任何检查逻辑。
5
- * 检查逻辑在 lib/checks/* 各模块;AST 判定在 lib/ast/*。
6
- * 往这里加检查时:只加一行「按 kind 调用」,不要在调度里实现判定。
7
- *
8
- * 调度前统一做三件事(顺序不能反):
9
- * ① exts 过滤:规则声明目标扩展名时按当前文件裁剪(如 patch 规则只查 yml/yaml)
10
- * ② 路径过滤:exclude_paths 声明的目录整规则跳过(如只查浏览器侧的规则排除 server/)
11
- * ③ 文件内容初筛:file_patterns 声明的规则先做 L1 正则预筛——未命中 = 非候选文件,
12
- * 该规则剔除,不进 L2 AST 精查(三层审计 L1,)
13
- * 三者都在此生效,是「规则作用域」的落地点——漏了就会出现 yml 声明被静默忽略。
14
- *
15
- * quick 档跳过需要解析的检查(AST 类),只跑正则/黑名单,用于高频轻量审计。
16
- */
17
- import { checkRegexRules, checkPathRegexRules, checkBlacklist } from './regex.js';
18
- import { checkFuncLines, checkEmptyCatch, checkMinLength, checkComplexity, checkDepth, checkMaxLines, checkRepeated, checkCommentDensity } from './structural.js';
19
- import { checkSemantic, checkPatchInsert } from './semantic.js';
20
- import { checkCredentialFiles } from './credential-file.js';
21
- import { checkNpmJson } from './npm-json.js';
22
- import { checkFolderRules } from './folder.js';
23
- import { checkPrivateFiles } from './private.js';
24
- import { checkButtonBindings } from './button-bind.js';
25
- import { checkMagicNumberSmart } from './magic-number.js';
26
- import { checkDataflow } from './dataflow.js';
27
- import { checkScopeCollision } from './scope-collision.js';
28
- import { checkFileHealth } from './file-health.js';
29
- import { filterRulesByExt, filterRulesByPath, filterRulesByFileText } from './filter.js';
30
- import { resolveDshClientEntries, resolveDshClientFragments } from '../audit/client-entry.js';
31
-
32
- /** 客户端入口集合缓存:repoPath → Set(相对路径)(同一次审计内只算一次)。 */
33
- const clientEntryCache = new Map();
34
- /** 客户端分片目录前缀缓存:repoPath → Set(相对路径前缀)(同一次审计内只算一次)。 */
35
- const clientFragmentCache = new Map();
36
-
37
- /**
38
- * 推导当前文件的**文件级作用域**(供规则 yml 的 scope_rules 命中)。
39
- * 两档(都由仓库 package.json 推导,见 lib/audit/client-entry.js):
40
- * · `dsh-client-fragment` —— 客户端**分片源码**目录(`<入口目录>/<入口名>-parts/`,或
41
- * package.json 的 `dsh.client.fragments` 显式声明)。分片是**片段**:单文件语法不成立、
42
- * 行数天然可能超阈值 ⇒ 按片段定级(仍报、不扣分),而不是靠「不可拆分」把整类问题挡掉。
43
- * · `dsh-client-entry` —— 客户端**构建产物**入口本身(运行态单文件的那一份)。
44
- * 放在调度层是因为这里能拿到 opts.repoPath(仓库上下文),而检查器是纯函数。
45
- * @param {{repoPath?:string, relPath?:string}} args
46
- * @returns {string} 作用域名;无命中返回 ''(空串 = 无作用域)
47
- */
48
- function resolveFileScope({ repoPath, relPath }) {
49
- if (!repoPath) return '';
50
- const rel = String(relPath || '').replace(/^\.\//, '');
51
- if (!rel) return '';
52
- if (!clientFragmentCache.has(repoPath)) clientFragmentCache.set(repoPath, resolveDshClientFragments(repoPath));
53
- for (const prefix of clientFragmentCache.get(repoPath)) if (rel.startsWith(prefix)) return 'dsh-client-fragment';
54
- if (!clientEntryCache.has(repoPath)) clientEntryCache.set(repoPath, resolveDshClientEntries(repoPath));
55
- return clientEntryCache.get(repoPath).has(rel) ? 'dsh-client-entry' : '';
56
- }
57
-
58
- export function runChecks({ file, relPath, text, grouped }, opts = {}) {
59
- const quick = opts.level === 'quick';
60
- // exts 过滤:规则声明目标扩展名时按当前文件裁剪(如 patch 规则只查 yml/yaml)
61
- grouped = filterRulesByExt(grouped, relPath || file || '');
62
- // 路径级过滤——exclude_paths 声明排除的目录整规则跳过(如 server/ lib/ test/)
63
- grouped = filterRulesByPath(grouped, relPath || file || '');
64
- // 三层审计 L1——file_patterns 文件内容级初筛(未命中 = 非候选,剔除进 L2)
65
- grouped = filterRulesByFileText(grouped, text);
66
- let findings = [];
67
- if (grouped['[FUNC]']) findings = findings.concat(checkRegexRules({ file, text, rules: grouped['[FUNC]'], repoPath: opts?.repoPath, treeConfirm: opts?.treeConfirm }));
68
- if (grouped['credential-ref']) findings = findings.concat(checkRegexRules({ file, text, rules: grouped['credential-ref'], repoPath: opts?.repoPath, treeConfirm: opts?.treeConfirm }));
69
- if (grouped['regex']) findings = findings.concat(checkRegexRules({ file, text, rules: grouped['regex'], repoPath: opts?.repoPath, treeConfirm: opts?.treeConfirm }));
70
- if (grouped['path-regex']) findings = findings.concat(checkPathRegexRules({ file, relPath, rules: grouped['path-regex'] }));
71
- if (!quick && grouped['func-lines']) findings = findings.concat(checkFuncLines({ file, text, rules: grouped['func-lines'] }));
72
- // 此前 9 个 kind 编译后无人消费(死桶)
73
- if (!quick && grouped['credential-file']) findings = findings.concat(checkCredentialFiles({ file, relPath, rules: grouped['credential-file'] }));
74
- if (!quick && grouped['min-length']) findings = findings.concat(checkMinLength({ file, text, rules: grouped['min-length'] }));
75
- if (!quick && grouped['max-complexity']) findings = findings.concat(checkComplexity({ file, text, rules: grouped['max-complexity'] }));
76
- if (!quick && grouped['max-depth']) findings = findings.concat(checkDepth({ file, text, rules: grouped['max-depth'] }));
77
- if (!quick && grouped['max-lines']) findings = findings.concat(checkMaxLines({ file, text, rules: grouped['max-lines'], fileScope: resolveFileScope({ repoPath: opts?.repoPath, relPath: relPath || file }) }));
78
- // 注释冗余密度(用户规则反馈①——减少行注释、保留块注释)。
79
- // 测试文件豁免:测试 setup 标头注释/用例说明是常规写法(fixture 语义的一部分),与
80
- // repeated-string 对 test 的豁免口径一致(audit-file.js maintainabilityScoped)。
81
- if (!quick && grouped['comment-density'] && !/(^|[/\\])(test|tests|__tests__|spec)[/\\]|\.(test|spec)\.(js|mjs|cjs|ts|py|tsx)$/i.test(String(relPath || file))) {
82
- findings = findings.concat(checkCommentDensity({ file, text, rules: grouped['comment-density'] }));
83
- }
84
- // 1.0.13:文件健康度矩阵(行数/大小/行长三维评分)
85
- if (!quick && grouped['file-health']) findings = findings.concat(checkFileHealth({ file, relPath, text, rules: grouped['file-health'] }));
86
- // repeated-string 与 min-occurrences 是两个 kind 但检测逻辑相同(同文本双报的根因)——
87
- // 合并规则列表一次运行,checkRepeated 内按文本去重(同一文本只报一条,不再「no-duplicate-code +
88
- // no-repeated-hardcoded-literals」同文本报两条)。
89
- if (!quick && (grouped['repeated-string'] || grouped['min-occurrences'])) {
90
- findings = findings.concat(checkRepeated({
91
- file, text,
92
- rules: [...(grouped['repeated-string'] || []), ...(grouped['min-occurrences'] || [])],
93
- }));
94
- }
95
- if (!quick && grouped['semantic']) findings = findings.concat(checkSemantic({ file, relPath, rules: grouped['semantic'], repoLevelRules: opts?.repoLevelRules }));
96
- if (grouped['blacklist']) findings = findings.concat(checkBlacklist({ file, text, rules: grouped['blacklist'] }));
97
- if (grouped['npm-json']) findings = findings.concat(checkNpmJson({ file, text, rules: grouped['npm-json'], repoHasJsYamlImport: opts?.repoHasJsYamlImport, repoPath: opts?.repoPath }));
98
- // patch insert 语义检查(yml 文件 insert/覆盖 id 冲突才报)
99
- if (grouped['patch-insert']) findings = findings.concat(checkPatchInsert({ file, text, rules: grouped['patch-insert'] }));
100
- // 1.0.5:按钮事件绑定交叉比对(油猴脚本/扩展——HTML 在 JS 字符串,事件在 JS 绑定)
101
- if (grouped['button-bind']) findings = findings.concat(checkButtonBindings({ file, text, rules: grouped['button-bind'], fileScope: resolveFileScope({ repoPath: opts?.repoPath, relPath: relPath || file }) }));
102
- // 1.0.7:硬编码魔数检测(版本号豁免版——版本号/日期/HTTP 状态码/常见合法常量/状态枚举自动豁免)
103
- if (grouped['magic-number-smart']) findings = findings.concat(checkMagicNumberSmart({ file, text, rules: grouped['magic-number-smart'] }));
104
- // 三层审计 L2——数据流检查(kind=dataflow,如 clear-then-access 同函数清空后访问)
105
- // L1 已在 filterRulesByFileText 初筛:file_patterns 未命中的文件这里 grouped 已空,空规则短路
106
- if (!quick && grouped['dataflow']) findings = findings.concat(checkDataflow({ file, text, rules: grouped['dataflow'] }));
107
- // 拼接产物作用域撞名(多段 JS 拼进同一作用域):判定在 lib/ast/scope-collisions.js,
108
- // 契约由被审项目自己的 package.json 决定(lib/audit/scope-policy.js)——
109
- // 规则 yml 跨仓库共享,故 yml 只声明「查不查」,不写任何项目的前缀约定。
110
- if (!quick && grouped['scope-collision']) findings = findings.concat(checkScopeCollision({ file, text, rules: grouped['scope-collision'] }));
111
- findings = findings.concat(checkEmptyCatch({ file, text }));
112
- // I/O 风险分级 —— 经「正则初筛 → AST 精筛」产出(robustness/io-risk
113
- // 规则 + astConfirmKind: io-risk),不在此独立调用检查器:两条路径会为同一处 I/O
114
- // 各报一次(实测 6 处重叠),且既有架构里 astConfirmKind 类规则一律只走正则路径、
115
- // AST 只做过滤(与 shell-cd-dynamic / write-into-gitignored 等一致)。
116
- return findings;
117
- }
@@ -1,69 +0,0 @@
1
- /**
2
- * 检查层 · 重复代码检测(抽公共函数候选)
3
- *
4
- * 为什么是「跨文件」检查:runChecks 逐文件调用只能发现文件内重复,
5
- * 而抽公共函数的收益场景主要是「同一逻辑散落在不同模块」——因此本检查
6
- * 在 auditFiles 层(拿到全部文件文本后)统一执行一次,不进 runChecks。
7
- *
8
- * 判定标准(量化判定标准):
9
- * · 同一归一化结构 ≥3 处(2 次观察、3 次必须抽)
10
- * · 函数体 ≥5 行(少于 5 行不值得抽)
11
- * · 排除:参数 >5、布尔 flag 参数、test/ 夹具、纯语法糖
12
- * severity 封顶 warning(只提示不拦截)。
13
- */
14
-
15
- import { collectDupCodeCandidates, findDuplicateBodies } from '../ast/dup-code.js';
16
- import { makeFinding } from '../audit/index.js';
17
- import { HINT_QUALITY } from './common.js';
18
-
19
- /** 重复代码豁免提示(文件头=整文件;测试夹具/故意复用的场景声明豁免)。 */
20
- export const HINT_DUP_CODE = 'dsh-skip-quality(文件头=整文件)';
21
-
22
- /**
23
- * 跨文件重复代码检测:对全部代码文件跑一遍,找出「该抽公共函数」的候选。
24
- * @param {Array<{path:string, text:string}>} fileTexts 全部代码文件(path + 全文)
25
- * @param {object} [rules] 规则定义(取 severity/dimensions;缺省按 warning)
26
- * @returns {Array} findings(severity 恒 warning 封顶)
27
- */
28
- export function checkDuplicateCode(fileTexts = [], rules = null) {
29
- const findings = [];
30
- if (!Array.isArray(fileTexts) || fileTexts.length < 3) return findings; // 文件太少无重复意义
31
-
32
- const rule = Array.isArray(rules) && rules.length ? rules[0] : null;
33
- const severity = 'warning'; // 恒 warning(最高只警告不拦截)
34
- const dimensions = rule?.dimensions || ['可维护性'];
35
- const ruleOpts = {
36
- minLines: rule?.extra?.minLines || rule?.minLines,
37
- minOccurrences: rule?.extra?.minOccurrences || rule?.minOccurrences,
38
- };
39
-
40
- // 逐文件提取候选(只处理代码文件,跳过 test/ 夹具)
41
- const perFile = [];
42
- for (const { path, text } of fileTexts) {
43
- if (!text) continue;
44
- if (/(^|[\\/])(test|tests|__tests__|fixtures?|mocks?)([\\/]|$)/.test(path)) continue; // 测试夹具不报
45
- if (!/\.(js|mjs|cjs|ts|tsx|jsx)$/.test(path)) continue; // 只查代码文件
46
- try {
47
- perFile.push(collectDupCodeCandidates(text, path));
48
- } catch { /* 单文件解析异常不影响整体 */ }
49
- }
50
-
51
- const dups = findDuplicateBodies(perFile, ruleOpts);
52
- for (const dup of dups) {
53
- // 只报第一处(其余实例在 message 里列出,避免同结构 N 条 finding)
54
- const [first, ...rest] = dup.instances;
55
- const others = rest.map((i) => `${i.name}@${i.file}:${i.line}`).join(',');
56
- findings.push(makeFinding({
57
- file: first.file,
58
- line: first.line,
59
- rule: rule?.id || 'maintainability/no-duplicate-code',
60
- kind: 'duplicate-code',
61
- severity, // warning 封顶
62
- message: `函数「${first.name}」(${dup.lines} 行)与 ${dup.instances.length - 1} 处重复(${others})——同一结构 ≥3 处,应抽公共函数`,
63
- dimensions,
64
- exemptHint: HINT_DUP_CODE,
65
- scoreImpact: 1,
66
- }));
67
- }
68
- return findings;
69
- }
@@ -1,232 +0,0 @@
1
- /**
2
- * 检查层 · 同名常量跨文件重复定义(DRY)
3
- *
4
- * 为什么是「跨文件」检查:runChecks 逐文件调用只能发现文件内重复,
5
- * 而「同一阈值/长度/窗口常量散落多个模块」正是改一处漏多处的维护坑——
6
- * 因此与 dup-code 一样在 auditFiles 层(拿到全部文件文本后)统一执行一次。
7
- *
8
- * 判定(用户建议「加规则」——MAX_MSG_PREVIEW=80 三处重复实测驱动):
9
- * · 提取每个代码文件的 `const NAME = <num|str>` 定义(token 级,剥离字符串/注释)
10
- * · 跨文件按 (name, value) 聚合:**同名校同值 ≥2 个文件** → 报「应统一单一来源」
11
- * · 排除:test/fixtures(测试夹具各自常量合理)、0/1/-1/2/3 等基础值(循环/布尔语境常见)
12
- * · severity 恒 warning(只提示不拦截)
13
- */
14
- import { tokenize } from '../ast/tokenizer.js';
15
- import { detectLang } from '../ast/lang.js';
16
- import { makeFinding } from '../audit/index.js';
17
- import { HINT_QUALITY } from './common.js';
18
-
19
- /** 基础值跳过(循环计数/布尔/简单算术——不是「应配置化」的阈值常量)。 */
20
- const SKIP_VALUES = new Set(['0', '1', '-1', '2', '3']);
21
- // 「脚本自解析自己的根」惯用名:这些名字出现在多个脚本里是设计预期(各脚本独立解析自己的根),
22
- // 不算重复定义。注意:本检查器只取 `=` 后的**第一个 token** 作为 value(如 `resolve(...)` 记成
23
- // `resolve`),所以下面的值判定只能按首 token 匹配,不能写 `resolve\(` 这类带括号的写法。
24
- const ROOT_IDIOM_NAMES = new Set(['HERE', 'ROOT', 'SCRIPT_DIR', 'SELF', 'DIR', 'BASE', 'BASE_DIR',
25
- // ESM 下每个脚本各写一份 `const __dirname = dirname(fileURLToPath(import.meta.url))`
26
- // 是标准写法(Node 的 CJS 变量在 ESM 里没有),同样是「各脚本自解析自己的根」,
27
- // 合并会破坏脚本独立运行 ⇒ 归入惯用法豁免(此前名字表漏了它,实测被报 5 处)。
28
- '__dirname']);
29
- const ROOT_IDIOM_VALUE = /^(dirname|fileURLToPath|__dirname|resolve|join|process|argv)$/;
30
- // 对象/数组字面量常量无法用本检查器判定:它只取 `=` 后第一个 token(对象记成 `{`、数组 `[`),
31
- // 同名同 `{` 不代表内容相同(如各扩展脚本各自导出的 `auditExt = { ... }`,行为完全不同)。
32
- // `new` 同理:`WRITE_METHODS = new Set([...])` 在 lib/ast(数组方法)与 lib/http(HTTP 方法)
33
- // 是两个完全不同的域,仅名字撞车就被报(实测确认两处内容毫无交集);
34
- // `SKIP_DIRS = new Set([...])` 在 lib/score(含 vendor/test/dist/coverage)与 lib/skip-dirs
35
- // (仅 node_modules/.git)内容也不同。⇒ 首 token 判不了内容的,一律不报。
36
- // 注:`new` 同名同概念但**清单已漂移**的情况(如 SYNC_FS_FNS)本规则也报不了——
37
- // 那需要结构化比对,属另一类工具职责;发现后按 DRY 手工合并。
38
- const UNJUDGEABLE_VALUES = new Set(['{', '[', 'new']);
39
- // 这两个是「每脚本一份」的命令行/环境对象,无条件豁免(与是不是路径无关)
40
- const PER_SCRIPT_NAMES = new Set(['args', 'env', 'argv', 'flags', 'opts']);
41
-
42
- /**
43
- * 提取 `const NAME = 值` / `const NAME: T = 值`(Rust 类型标注)/ Kotlin `const val NAME = 值`
44
- * 中的常量定义。仅模块级(atTopLevel 校验由调用方传闭包);返回 true=已消费该 const token。
45
- */
46
- function extractFromConst(tokens, i, atTopLevel, push) {
47
- if (!atTopLevel(tokens[i].line)) return true; // 函数内 const 是局部变量,跳过
48
- let k = i + 1;
49
- const end = Math.min(tokens.length, i + 7);
50
- while (k < end) {
51
- const nm = tokens[k];
52
- if (!nm || nm.type !== 'ident') { k++; continue; }
53
- const eq = tokens[k + 1];
54
- if (eq?.type === 'punct' && eq.value === '=') { push(nm.value, tokens[k + 2], nm.line); return true; }
55
- if (eq?.type === 'punct' && eq.value === ':') {
56
- // Rust `NAME: T = 值`——NAME 后 `:` 再向后找 `=`
57
- for (let m = k + 2; m < Math.min(tokens.length, k + 5); m++) {
58
- if (tokens[m]?.type === 'punct' && tokens[m].value === '=') { push(nm.value, tokens[m + 1], nm.line); return true; }
59
- }
60
- return true;
61
- }
62
- // 非 `NAME =` 的 ident(Kotlin const 后的 val)→ 继续找下一个
63
- k++;
64
- }
65
- return true;
66
- }
67
-
68
- /**
69
- * 提取 Java `static final TYPE NAME = 值` 类常量字段(final 前须有 static——方法内局部 final 不算常量)。
70
- * 返回 true=已消费该 final token。
71
- */
72
- function extractFromJavaFinal(tokens, i, push) {
73
- const prev = tokens[i - 1];
74
- if (!(prev?.type === 'ident' && prev.value === 'static')) return true; // 仅 static final 类常量
75
- for (let k = i + 1; k < Math.min(tokens.length, i + 5); k++) {
76
- const nm = tokens[k], eq = tokens[k + 1], val = tokens[k + 2];
77
- if (nm?.type === 'ident' && eq?.type === 'punct' && eq.value === '=') { push(nm.value, val, nm.line); return true; }
78
- }
79
- return true;
80
- }
81
-
82
- /**
83
- * 提取文件内「命名常量定义」(token 级,剥离字符串/注释),按语言分派:
84
- * js/ts/tsx/jsx:`const NAME = <num|str>`
85
- * kotlin:`const 声明 + NAME = <值>` / 顶层 `val NAME = <值>`
86
- * java:`[static] final [类型] NAME = <值>;`(final 后 3 token 内的 NAME =)
87
- * go/rust:`const NAME = <值>`(const 后)
88
- * python:**行首无缩进** `NAME = <num|str>`(模块级常量惯例)
89
- * 统一返回 {name, value, line};value 归一(num 原样、str 去引号)。
90
- * @param {string} text 文件全文
91
- * @returns {Array<{name:string, value:string, line:number}>}
92
- */
93
- /**
94
- * 造一个「常量入表」闭包:值归一 + 各类豁免判定。
95
- *
96
- * 为什么单独成函数:原 findConstDefs 把「token 循环 + 五种语言形态分支 + 值过滤 + 去重」
97
- * 混在一个函数里,圈复杂度 32(阈值 10,自审 max-cyclomatic-complexity 高风险)。
98
- * 过滤规则(值豁免 / 惯用法豁免)抽出来后,循环只负责「哪种形态交给谁判」。
99
- */
100
- function createConstPusher(out) {
101
- return (name, val, line) => {
102
- if (!name || !val) return;
103
- const raw = val.type === 'num' ? val.value : String(val.value).replace(/^(['"])([\s\S]*)\1$/, '$2');
104
- if (SKIP_VALUES.has(raw)) return;
105
- // 对象/数组字面量:本检查器只取首 token(`{` / `[`),判不了内容是否真相同 ⇒ 不报(见常量注释)
106
- if (UNJUDGEABLE_VALUES.has(raw)) return;
107
- // 「脚本自解析自己的根」是**独立运行**的设计前提(脚本随插件发布、在安装副本目录里同样要能跑,
108
- // 见 scripts/ 各脚本的零依赖约定)——每个脚本各写一份 HERE / ROOT / SCRIPT_DIR / args / env
109
- // 不是「重复定义」,合并成公共常量反而让脚本依赖 lib 而失去独立性。
110
- // 此前这类惯用法被当 DRY 问题报出来:自审 22 条里大半是它(误报,实测)。
111
- // 判定要同时满足「通用名 + 路径/argv 惯用值」两个条件,避免误伤 PLUGIN_ROOT 这类真重复。
112
- if (PER_SCRIPT_NAMES.has(name)) return; // 每脚本一份的命令行/环境对象,不是共享常量
113
- if (ROOT_IDIOM_NAMES.has(name) && ROOT_IDIOM_VALUE.test(raw)) return;
114
- out.push({ name, value: raw, line });
115
- };
116
- }
117
-
118
- /**
119
- * 按语言形态判定「当前 token 是否是模块级命名常量声明」,是则入表并返回 true(已消费)。
120
- *
121
- * 为什么单独成函数:五种形态(JS const / Kotlin const / Kotlin val / Java static final /
122
- * Python 全大写)此前全挤在 findConstDefs 的循环里,是该函数圈复杂度居高(32)的主因。
123
- */
124
- function extractConstAtToken(tokens, i, { lang, atTopLevel, push }) {
125
- const t = tokens[i];
126
- if (t.type !== 'ident') return false;
127
- // const NAME = 值 / Kotlin const 声明 + NAME = 值 / Rust const NAME: T = 值(类型标注)
128
- // ——仅**模块级**(行首无缩进)才算命名常量;函数内 const 是局部变量(r/args/out 等
129
- // 常见名跨文件同名纯属巧合,不是 DRY 信号——实测误爆 338 条修正)
130
- if (t.value === 'const' && lang !== 'java') return extractFromConst(tokens, i, atTopLevel, push);
131
- // Kotlin:val NAME = val(顶层,无 const 前缀)
132
- if (t.value === 'val' && lang === 'kotlin' && atTopLevel(t.line)) {
133
- const nm = tokens[i + 1], eq = tokens[i + 2], val = tokens[i + 3];
134
- if (nm?.type === 'ident' && eq?.type === 'punct' && eq.value === '=') push(nm.value, val, nm.line);
135
- return true;
136
- }
137
- // Java:`static final TYPE NAME = val`(类常量字段;final 前须有 static——方法内局部 final 不算常量)
138
- if (t.value === 'final' && lang === 'java') return extractFromJavaFinal(tokens, i, push);
139
- // Python:行首无缩进且**全大写** `NAME = val`(Python 模块级常量惯例 PEP8;
140
- // 不依赖 detectLang——单行常量样本无 def/class 时 detectLang 落 js,直接用形态判定)
141
- if (atTopLevel(t.line) && /^[A-Z][A-Z0-9_]*$/.test(t.value)) {
142
- const eq = tokens[i + 1], val = tokens[i + 2];
143
- if (eq?.type === 'punct' && eq.value === '=' && val && (val.type === 'num' || val.type === 'str')) {
144
- push(t.value, val, t.line);
145
- }
146
- }
147
- return false;
148
- }
149
-
150
- export function findConstDefs(text = '') {
151
- const tokens = tokenize(String(text));
152
- const lang = detectLang(String(text));
153
- const out = [];
154
- // 行首是否无缩进(模块级顶层)——函数内 const/let/var/val 是局部变量,不是「命名常量」。
155
- // ⚠️ 性能(2026-10-09 实测,这曾是全插件最大的热点):原实现**每次调用**都 `String(text).split('\n')`,
156
- // 而本函数会被**每个 token** 调用 ⇒ O(tokens × 文件长度) 的二次复杂度:
157
- // 外部仓全量审计 23.9s 里有 21.5s(CPU profile 89.8%)耗在这一行 ✗。
158
- // 改为只 split 一次(lines[line-1] 为 O(1) 取行,`/^\S/` 锚定在行首亦为 O(1))⇒ 复杂度降回线性。
159
- const lines = String(text).split('\n');
160
- const atTopLevel = (line) => /^\S/.test(lines[line - 1] || '');
161
- const push = createConstPusher(out);
162
- const ctx = { lang, atTopLevel, push };
163
- for (let i = 0; i < tokens.length; i++) {
164
- if (extractConstAtToken(tokens, i, ctx)) continue;
165
- }
166
- // 多分支可能对同一常量重复提取(JS const 分支 + 全大写分支 / Java final 分支 + 全大写分支
167
- // 都可能命中同一 `NAME = 值`)——按行名去重,保证同一条只留一份
168
- const seen = new Set();
169
- return out.filter((d) => {
170
- const k = `${d.line}:${d.name}:${d.value}`;
171
- if (seen.has(k)) return false;
172
- seen.add(k);
173
- return true;
174
- });
175
- }
176
-
177
- /**
178
- * 跨文件同名常量重复定义检测。
179
- * @param {Array<{path:string, text:string}>} fileTexts 全部代码文件
180
- * @param {Array} [rules] 规则定义(取 id/dimensions)
181
- * @returns {Array} findings(severity 恒 warning)
182
- */
183
- export function checkDuplicateConst(fileTexts = [], rules = null) {
184
- const findings = [];
185
- if (!Array.isArray(fileTexts) || fileTexts.length < 2) return findings;
186
- const rule = Array.isArray(rules) && rules.length ? rules[0] : null;
187
-
188
- // 逐文件提取(跳过 test/fixtures——测试夹具常量各自定义是正常形态)
189
- const perFile = [];
190
- for (const { path, text } of fileTexts) {
191
- if (!text) continue;
192
- if (/(^|[\\/])(test|tests|__tests__|fixtures?|mocks?)([\\/]|$)/.test(path)) continue;
193
- if (!/\.(js|mjs|cjs|ts|tsx|jsx)$/.test(path)) continue;
194
- try {
195
- perFile.push({ path, defs: findConstDefs(text) });
196
- } catch { /* 单文件解析异常不影响整体 */ }
197
- }
198
-
199
- // 聚合:key = name|value → {name, value, files: [{path, line}]}
200
- const groups = new Map();
201
- for (const { path, defs } of perFile) {
202
- for (const d of defs) {
203
- const key = `${d.name}|${d.value}`;
204
- if (!groups.has(key)) groups.set(key, { name: d.name, value: d.value, files: [] });
205
- groups.get(key).files.push({ path, line: d.line });
206
- }
207
- }
208
-
209
- for (const g of groups.values()) {
210
- if (g.files.length < 2) continue;
211
- // 独立脚本与 lib 的同名常量不算重复:scripts/ 下每个脚本都要能**单独运行**
212
- // (随插件发布、在安装副本目录里同样可执行),各自解析自己的根 / 维护自己的注释标记
213
- // 是设计前提;强行合并会让脚本依赖 lib 而失去独立性。
214
- // 自审剩余 3 条全属此类:SKIP_DIRS(脚本与 collector 各自 getSkipSet())、
215
- // PLUGIN_ROOT(脚本自己 join(__dirname,'..'))、MARK_START/MARK_END(两个脚本各自维护)。
216
- if (g.files.some((f) => /(^|[\\/])scripts[\\/]/.test(f.path))) continue;
217
- const [first, ...rest] = g.files;
218
- const others = rest.map((f) => `${f.path}:${f.line}`).join(',');
219
- findings.push(makeFinding({
220
- file: first.path,
221
- line: first.line,
222
- rule: rule?.id || 'maintainability/duplicate-constant-def',
223
- kind: 'duplicate-const',
224
- severity: 'warning',
225
- message: `常量「${g.name} = ${g.value}」在 ${g.files.length} 个文件重复定义(${first.path}:${first.line}、${others})——阈值/长度/窗口应统一到公共常量模块或配置文件单处定义(DRY,改一处即可全局生效)`,
226
- dimensions: rule?.dimensions || ['可维护性'],
227
- exemptHint: HINT_QUALITY,
228
- scoreImpact: 1,
229
- }));
230
- }
231
- return findings;
232
- }