dsh-git-push 0.0.0-stage → 2.8.4
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/.auditignore +30 -0
- package/.gitignore +39 -0
- package/LICENSE +21 -0
- package/README.md +1482 -2
- package/cli.mjs +360 -0
- package/cordis.patch.yml +14 -0
- package/lib/ARCHITECTURE.md +34 -0
- package/lib/BUGFIX-NOTES-2026-09-14.md +278 -0
- package/lib/app/apply.js +275 -0
- package/lib/app/audit-api.js +158 -0
- package/lib/app/command-registry.js +262 -0
- package/lib/app/constants.js +16 -0
- package/lib/app/handlers/account.js +166 -0
- package/lib/app/handlers/clone.js +168 -0
- package/lib/app/handlers/http-constants.js +9 -0
- package/lib/app/handlers/identity-rewrite.js +94 -0
- package/lib/app/handlers/meta.js +175 -0
- package/lib/app/handlers/npm.js +169 -0
- package/lib/app/handlers/repo-actions.js +143 -0
- package/lib/app/handlers/repos.js +231 -0
- package/lib/app/handlers/settings.js +62 -0
- package/lib/app/host-doc-checks.js +25 -0
- package/lib/app/http-handlers.js +131 -0
- package/lib/app/index.js +30 -0
- package/lib/app/inject-text.js +191 -0
- package/lib/app/scan-root.js +23 -0
- package/lib/app/schema.js +150 -0
- package/lib/app/settings-bridge.js +167 -0
- package/lib/app/slash-commands.js +395 -0
- package/lib/app/slot-stats.js +35 -0
- package/lib/app/tool-call.js +662 -0
- package/lib/app/tools.js +40 -0
- package/lib/ast/brace.js +105 -0
- package/lib/ast/callgraph.js +135 -0
- package/lib/ast/code-lines.js +114 -0
- package/lib/ast/consts.js +12 -0
- package/lib/ast/control-flow.js +461 -0
- package/lib/ast/credential.js +199 -0
- package/lib/ast/dataflow.js +274 -0
- package/lib/ast/dup-code.js +165 -0
- package/lib/ast/index.js +36 -0
- package/lib/ast/io-risk-const.js +95 -0
- package/lib/ast/io-risk-fn.js +208 -0
- package/lib/ast/io-risk-loop.js +308 -0
- package/lib/ast/io-risk.js +314 -0
- package/lib/ast/lang.js +156 -0
- package/lib/ast/line-count.js +52 -0
- package/lib/ast/magic-number.js +272 -0
- package/lib/ast/naming.js +284 -0
- package/lib/ast/perf.js +472 -0
- package/lib/ast/scope.js +241 -0
- package/lib/ast/shell.js +131 -0
- package/lib/ast/size.js +378 -0
- package/lib/ast/symbol-index.js +399 -0
- package/lib/ast/tokenizer.js +341 -0
- package/lib/audit/analysis-coverage.js +98 -0
- package/lib/audit/audit-file.js +182 -0
- package/lib/audit/checks.js +17 -0
- package/lib/audit/client-entry.js +93 -0
- package/lib/audit/collector.js +477 -0
- package/lib/audit/ext-runner.js +80 -0
- package/lib/audit/file-context.js +138 -0
- package/lib/audit/finding.js +50 -0
- package/lib/audit/gitignore-match.js +113 -0
- package/lib/audit/glob.js +63 -0
- package/lib/audit/history-report.js +100 -0
- package/lib/audit/history.js +126 -0
- package/lib/audit/ignore-blind.js +101 -0
- package/lib/audit/index.js +33 -0
- package/lib/audit/orchestrate.js +476 -0
- package/lib/audit/public.js +24 -0
- package/lib/audit/repo-level.js +30 -0
- package/lib/audit/report-yaml.js +86 -0
- package/lib/audit/slot.js +65 -0
- package/lib/audit-defaults.js +18 -0
- package/lib/audit-rules/audit-rules-comment.yml +247 -0
- package/lib/audit-rules/audit-rules-cpp.yml +22 -0
- package/lib/audit-rules/audit-rules-docs.yml +32 -0
- package/lib/audit-rules/audit-rules-dsh.yml +162 -0
- package/lib/audit-rules/audit-rules-filehealth.yml +42 -0
- package/lib/audit-rules/audit-rules-folder.yml +99 -0
- package/lib/audit-rules/audit-rules-frontend.yml +335 -0
- package/lib/audit-rules/audit-rules-go.yml +22 -0
- package/lib/audit-rules/audit-rules-i18n.yml +45 -0
- package/lib/audit-rules/audit-rules-java.yml +105 -0
- package/lib/audit-rules/audit-rules-nodejs.yml +647 -0
- package/lib/audit-rules/audit-rules-npm.yml +187 -0
- package/lib/audit-rules/audit-rules-paths.yml +92 -0
- package/lib/audit-rules/audit-rules-performance.yml +207 -0
- package/lib/audit-rules/audit-rules-php.yml +22 -0
- package/lib/audit-rules/audit-rules-private.yml +24 -0
- package/lib/audit-rules/audit-rules-python.yml +106 -0
- package/lib/audit-rules/audit-rules-robustness.yml +27 -0
- package/lib/audit-rules/audit-rules-rust.yml +22 -0
- package/lib/audit-rules/audit-rules-structure.yml +159 -0
- package/lib/audit-rules/audit-rules-swift.yml +22 -0
- package/lib/audit-rules/audit-rules-template.yml +108 -0
- package/lib/checks/button-bind.js +276 -0
- package/lib/checks/common.js +170 -0
- package/lib/checks/credential-file.js +43 -0
- package/lib/checks/dataflow.js +40 -0
- package/lib/checks/dispatch.js +112 -0
- package/lib/checks/dup-code.js +69 -0
- package/lib/checks/dup-const.js +232 -0
- package/lib/checks/file-health.js +150 -0
- package/lib/checks/filter.js +131 -0
- package/lib/checks/folder.js +173 -0
- package/lib/checks/hardcoded-path.js +98 -0
- package/lib/checks/index.js +46 -0
- package/lib/checks/io.js +66 -0
- package/lib/checks/magic-number.js +42 -0
- package/lib/checks/npm-json.js +273 -0
- package/lib/checks/private.js +63 -0
- package/lib/checks/regex.js +256 -0
- package/lib/checks/semantic.js +134 -0
- package/lib/checks/structural.js +274 -0
- package/lib/cli/commands-account.mjs +107 -0
- package/lib/cli/commands-audit.mjs +144 -0
- package/lib/cli/commands-doc.mjs +127 -0
- package/lib/cli/commands-vcs.mjs +175 -0
- package/lib/client/index.js +162 -0
- package/lib/client-parts/00-loader.js +33 -0
- package/lib/client-parts/01-const.js +207 -0
- package/lib/client-parts/02-utils.js +62 -0
- package/lib/client-parts/03-icons.js +12 -0
- package/lib/client-parts/10-account.js +83 -0
- package/lib/client-parts/11-browser.js +95 -0
- package/lib/client-parts/20-repo.js +374 -0
- package/lib/client-parts/21-audit.js +301 -0
- package/lib/client-parts/22-settings.js +146 -0
- package/lib/client-parts/23-npm.js +383 -0
- package/lib/client-parts/30-page.js +56 -0
- package/lib/client-parts/40-controller.js +1092 -0
- package/lib/client-parts/41-actions.js +279 -0
- package/lib/client-parts/99-apply.js +72 -0
- package/lib/client.js +3195 -0
- package/lib/commit-push.js +144 -0
- package/lib/context/index.js +363 -0
- package/lib/core/atomic-json.js +98 -0
- package/lib/core/config.js +40 -0
- package/lib/core/credentials.js +274 -0
- package/lib/core/endpoints.js +109 -0
- package/lib/core/exec.js +162 -0
- package/lib/exempt/index.js +292 -0
- package/lib/fs/edit-after-read.js +103 -0
- package/lib/fsx.js +87 -0
- package/lib/git/account-status.js +140 -0
- package/lib/git/account.js +233 -0
- package/lib/git/api.js +136 -0
- package/lib/git/atomic-json.js +6 -0
- package/lib/git/browse.js +38 -0
- package/lib/git/clone-download.js +397 -0
- package/lib/git/clone-history-mode.js +143 -0
- package/lib/git/clone-history.js +175 -0
- package/lib/git/clone-jobs.js +250 -0
- package/lib/git/clone-replay-io.js +172 -0
- package/lib/git/clone-replay.js +275 -0
- package/lib/git/clone.js +445 -0
- package/lib/git/cloud.js +46 -0
- package/lib/git/commit-message.js +141 -0
- package/lib/git/config.js +6 -0
- package/lib/git/cred-env.js +88 -0
- package/lib/git/credentials.js +6 -0
- package/lib/git/endpoints.js +6 -0
- package/lib/git/exec.js +6 -0
- package/lib/git/identity-rewrite.js +297 -0
- package/lib/git/identity.js +150 -0
- package/lib/git/ignore.js +69 -0
- package/lib/git/index.js +41 -0
- package/lib/git/module-splitter.js +47 -0
- package/lib/git/post-push.js +101 -0
- package/lib/git/push-diagnose.js +142 -0
- package/lib/git/push.js +470 -0
- package/lib/git/remote-resolve.js +146 -0
- package/lib/git/remote.js +128 -0
- package/lib/git/repo-index.js +512 -0
- package/lib/git/repos.js +235 -0
- package/lib/git/scan-runner.js +159 -0
- package/lib/git/sensitive.js +134 -0
- package/lib/git/transport.js +430 -0
- package/lib/git/visibility.js +35 -0
- package/lib/git/wrapped-git.js +68 -0
- package/lib/http/index.js +170 -0
- package/lib/index.js +24 -0
- package/lib/link-check/index.js +187 -0
- package/lib/npm/account.js +64 -0
- package/lib/npm/install.js +211 -0
- package/lib/npm/npmrc.js +56 -0
- package/lib/npm/publish.js +167 -0
- package/lib/npm/registry.js +147 -0
- package/lib/plugin/auto-detect.js +92 -0
- package/lib/plugin/auto-push.js +154 -0
- package/lib/plugin/index.js +250 -0
- package/lib/readme-templates/readme.yml +38 -0
- package/lib/rule/compilers/credential.js +62 -0
- package/lib/rule/compilers/dataflow.js +38 -0
- package/lib/rule/compilers/file-health.js +75 -0
- package/lib/rule/compilers/frontend.js +86 -0
- package/lib/rule/compilers/func.js +35 -0
- package/lib/rule/compilers/helpers.js +71 -0
- package/lib/rule/compilers/index.js +29 -0
- package/lib/rule/compilers/link-semantic.js +45 -0
- package/lib/rule/compilers/list-kinds.js +12 -0
- package/lib/rule/compilers/numeric-kinds.js +66 -0
- package/lib/rule/compilers/regex.js +78 -0
- package/lib/rule/compilers/structure.js +80 -0
- package/lib/rule/compilers.js +17 -0
- package/lib/rule/homoglyph.js +45 -0
- package/lib/rule/loader.js +270 -0
- package/lib/rule/registry.js +115 -0
- package/lib/rule/scope.js +67 -0
- package/lib/score/docs-score.js +166 -0
- package/lib/score/index.js +140 -0
- package/lib/self/index.js +193 -0
- package/lib/skip-dirs.js +64 -0
- package/lib/tool-probes.json +50 -0
- package/lib/user-requirements.json +14 -0
- package/lib/vendor/js-yaml/LICENSE +21 -0
- package/lib/vendor/js-yaml/js-yaml.mjs +3073 -0
- package/package.json +95 -4
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +34 -0
- package/scripts/audit-ext/variable-min-length.mjs +102 -0
- package/scripts/audit-runner.mjs +35 -0
- package/scripts/audit-runtime-check.mjs +129 -0
- package/scripts/browser-page-probe.mjs +271 -0
- package/scripts/build-client.mjs +119 -0
- package/scripts/check.mjs +88 -0
- package/scripts/clean-date-comments.py +102 -0
- package/scripts/doc-drift.mjs +200 -0
- package/scripts/doc-func.mjs +212 -0
- package/scripts/doc-tree.mjs +456 -0
- package/scripts/doc-version.mjs +220 -0
- package/scripts/frontend-preview-check.mjs +124 -0
- package/scripts/hot-path-probe-cjs.cjs +71 -0
- package/scripts/hot-path-probe-fs-shim.mjs +28 -0
- package/scripts/hot-path-probe-fsp-shim.mjs +31 -0
- package/scripts/hot-path-probe.mjs +153 -0
- package/scripts/module-splitter.py +800 -0
- package/scripts/platform-scan.mjs +168 -0
- package/scripts/preview-gen.mjs +489 -0
- package/scripts/preview-local-runner.mjs +55 -0
- package/scripts/preview-server-direct.mjs +116 -0
- package/scripts/preview-server.mjs +305 -0
- package/scripts/preview-slot-data.mjs +123 -0
- package/scripts/probe-recheck.mjs +47 -0
- package/scripts/readme-gen.mjs +268 -0
- package/scripts/rename-locator.mjs +97 -0
- package/scripts/rule-switch.mjs +97 -0
- package/scripts/rules-solo-audit.mjs +173 -0
- package/scripts/scan-file-io.mjs +713 -0
- package/scripts/scan-hardcoded-paths.mjs +125 -0
- package/scripts/scan-repos.mjs +167 -0
- package/scripts/scan-version.mjs +122 -0
- package/scripts/scrub-user-wording.mjs +420 -0
- package/scripts/symbol-index.mjs +85 -0
- package/scripts/sync-plugin-install.mjs +223 -0
- package/scripts/sync-plugin.mjs +279 -0
- package/scripts/verify-prestep.mjs +100 -0
- package/skills/dev/audit-rule-authoring.md +58 -0
- package/skills/dev/dsh-git-push.md +323 -0
- package/skills/dev/git-push-live-fix.md +45 -0
- package/skills/dsh-repo-index.md +35 -0
- package/skills/guide/guide-audit.md +167 -0
- package/skills/guide/guide-cli-scripts.md +104 -0
- package/skills/guide/guide-clone-repos.md +173 -0
- package/skills/guide/guide-commit-push.md +156 -0
- package/skills/guide/guide-inject-ui.md +166 -0
- package/skills/guide/guide-slash-completion.md +118 -0
- package/skills/workflow/README.md +21 -0
- package/skills/workflow/git-commit-discipline.md +68 -0
- package/skills/workflow/git-project-startup.md +85 -0
- package/skills/workflow/git-rebuild-process.md +57 -0
- package/skills/workflow/github-operations.md +91 -0
- package/skills/workflow/readme-sync-git-md.md +28 -0
- package/skills/workflow/task-completion-report.md +73 -0
- package/skills/workflow/tool-json-add-ask.md +35 -0
- package/skills/workflow/versioning-rule.md +211 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检查层 · 公共设施
|
|
3
|
+
*
|
|
4
|
+
* 职责:检查器共用的豁免提示、severity 封顶、按 kind 分组,
|
|
5
|
+
* 以及各 astConfirm 精筛集合的取值函数(把 AST 判定结果喂给 regex 类规则)。
|
|
6
|
+
*
|
|
7
|
+
* 为什么集中在这里:这些是多个检查器的共同依赖,散落各处会造成
|
|
8
|
+
* 「同一语义两处定义、改一处漏一处」——精筛集合尤其如此。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// 性能:循环体内做全量集合物化(判据与实现见 lib/ast/perf.js,含真实样本与实测数据)。
|
|
12
|
+
// 规则层统一从本模块取精筛集合,保持「精筛集合的取值函数都在 common.js」这一约定。
|
|
13
|
+
export { loopFullCollectionLines, repeatIdenticalCallLines } from '../ast/perf.js';
|
|
14
|
+
// 2026-10-12 实战沉淀:循环后缺兜底 return / 循环内整份重写 / 嵌套线性扫描 / 长同步循环无让出点
|
|
15
|
+
export { loopNoFallbackReturnLines, loopFullRewriteCallLines, nestedLinearScanLines, longSyncLoopLines } from '../ast/perf.js';
|
|
16
|
+
|
|
17
|
+
import { normalizeScopeRules } from '../rule/scope.js';
|
|
18
|
+
|
|
19
|
+
import { checkMagicNumberSmartAst, checkSmallFileReadAst, checkConsoleLogJsonAst, checkShortFunctionNameAst, checkPlaceholderCredentialAst, checkCredentialRefAst, shellCdDynamicLines as shellCdDynamicAst, writeIntoGitignoredLines as writeIntoGitignoredAst, scanIoRiskAst } from '../ast/index.js';
|
|
20
|
+
|
|
21
|
+
/** 质量类检查的豁免提示(文件头=整文件豁免)。单处定义,多处复用,避免重复字面量。 */
|
|
22
|
+
export const HINT_QUALITY = 'dsh-skip-quality(文件头=整文件)';
|
|
23
|
+
/** 大文件/目录类检查的豁免提示(只能文件头)。 */
|
|
24
|
+
export const HINT_SIZE = 'dsh-skip-size(文件头=整文件)';
|
|
25
|
+
/** 敏感/凭据类检查的豁免提示(文件头=整文件 / 行尾=本行)。 */
|
|
26
|
+
export const HINT_SENSITIVE = 'dsh-skip-sensitive(文件头=整文件)';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 规则声明的 severity 是上限:检查器内部风险升级不得超过规则自身声明。
|
|
30
|
+
* (修正:max-complexity 规则声明 warning,不应因内部 block 阈值而升为 blocker)
|
|
31
|
+
* @param {string} ruleSeverity yml 声明的 severity
|
|
32
|
+
* @param {string} internalLevel 检查器内部评估等级
|
|
33
|
+
* @returns {string} 最终 severity
|
|
34
|
+
*/
|
|
35
|
+
export function capSeverity(ruleSeverity, internalLevel) {
|
|
36
|
+
const rank = { notice: 0, info: 0, warning: 1, error: 2, blocker: 2 };
|
|
37
|
+
const declared = rank[ruleSeverity] ?? 1;
|
|
38
|
+
const internal = rank[internalLevel] ?? 1;
|
|
39
|
+
const final = Math.min(declared, internal);
|
|
40
|
+
return final >= 2 ? (ruleSeverity === 'error' ? 'error' : 'blocker') : (final === 1 ? 'warning' : 'notice');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 按 kind 分组编译规则。
|
|
45
|
+
*
|
|
46
|
+
* ⚠️ 这里必须补 `normalizeScopeRules`:规范化原本只在 **yml 加载路径**(lib/rule/loader.js)执行,
|
|
47
|
+
* 而检查器拿到的是**编译后**的规则对象(本函数是必经环节)⇒ 若此处不补,规则 yml 里的
|
|
48
|
+
* `scope_rules` 会在编译环节**丢失**,导致作用域降级/豁免在生产中永不生效
|
|
49
|
+
* (实测:DSH 客户端单文件的 downgrade 声明因此不触发,看起来只有检查器里的兜底在起作用)。
|
|
50
|
+
* normalizeScopeRules 幂等,故与 loader 重复调用无害。
|
|
51
|
+
*/
|
|
52
|
+
export function groupByKind(compiled) {
|
|
53
|
+
const grouped = {};
|
|
54
|
+
for (const r of compiled || []) {
|
|
55
|
+
const withScope = r && typeof r === 'object' && !Array.isArray(r)
|
|
56
|
+
? { ...r, scopeRules: normalizeScopeRules(r) }
|
|
57
|
+
: r;
|
|
58
|
+
(grouped[withScope.kind] ||= []).push(withScope);
|
|
59
|
+
}
|
|
60
|
+
return grouped;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* astConfirm: "magic-number" 的值级精筛集合——返回 checkMagicNumberSmartAst
|
|
65
|
+
* 判定为「真魔数」的行的 Set(版本号/日期/HTTP 状态码/命名常量值/参数默认值/
|
|
66
|
+
* 索引运算 i+1 等豁免行不在其中)。正则初筛出的候选行必须在此集合内才报。
|
|
67
|
+
* @param {string} text 文件全文
|
|
68
|
+
* @returns {Set<number>} 命中行号集合
|
|
69
|
+
*/
|
|
70
|
+
export function smartHitLines(text) {
|
|
71
|
+
const hits = checkMagicNumberSmartAst(String(text || ''));
|
|
72
|
+
return new Set(hits.map((h) => h.line));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* astConfirmKind: "small-file-read" 的精筛集合——受控小文件读取的行号
|
|
77
|
+
* (读 JSON 数据/配置/缓存,不构成 memory-bomb 风险)。
|
|
78
|
+
* 并集 console.log(JSON.stringify(大对象)) 一次性输出行(CLI --json 出口
|
|
79
|
+
* 序列化一次即打印退出,不常驻内存)——两类都属 memory-bomb 的合法豁免场景。
|
|
80
|
+
* @param {string} text 文件全文
|
|
81
|
+
* @returns {Set<number>} 应豁免的行号集合
|
|
82
|
+
*/
|
|
83
|
+
export function smallFileReadLines(text) {
|
|
84
|
+
const s = checkSmallFileReadAst(String(text || ''));
|
|
85
|
+
for (const line of checkConsoleLogJsonAst(String(text || ''))) s.add(line);
|
|
86
|
+
return s;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* astConfirmKind: "console-log-json" 的精筛集合——`console.log(JSON.stringify(大对象))`
|
|
91
|
+
* 一次性输出行(CLI --json 出口序列化一次即打印退出,不常驻内存),豁免 memory-bomb。
|
|
92
|
+
* @param {string} text 文件全文
|
|
93
|
+
* @returns {Set<number>} 豁免行号集合
|
|
94
|
+
*/
|
|
95
|
+
export function consoleLogJsonLines(text) {
|
|
96
|
+
return checkConsoleLogJsonAst(String(text || ''));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* astConfirmKind: "short-func-name" 的精筛集合——token 级判定为「真的过短且非公认
|
|
101
|
+
* 缩写」的函数声明行(i18n 的 tr/t、字典 L 等不在其中)。
|
|
102
|
+
* @param {string} text 文件全文
|
|
103
|
+
* @returns {Set<number>} 允许报的行号集合
|
|
104
|
+
*/
|
|
105
|
+
export function shortFuncNameLines(text) {
|
|
106
|
+
return new Set(checkShortFunctionNameAst(String(text || '')).map((h) => h.line));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* astConfirmKind: "credential-value" 的精筛集合——token 级确认「凭据标识符右侧
|
|
111
|
+
* 直接是有效字符串字面量」的行号(类型检查/字段透传/占位符/取环境变量都不算硬编码)。
|
|
112
|
+
* 见 ast.js checkCredentialRefAst。
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* astConfirmKind: "placeholder-credential" 的集合取值——占位符密钥串所在行。
|
|
116
|
+
* 集合内 = 命中行是演示/占位值(豁免);集合外 = 疑似真凭据(照报)。
|
|
117
|
+
*/
|
|
118
|
+
export function placeholderCredentialLines(text, file) {
|
|
119
|
+
return checkPlaceholderCredentialAst(text, file);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function credentialValueLines(text) {
|
|
123
|
+
return checkCredentialRefAst(String(text || ''));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* astConfirmKind: "shell-cd-dynamic" 的集合取值——cd 到动态路径($VAR/$())且无兜底
|
|
128
|
+
* 的行号(报)。见 lib/ast/shell.js shellCdDynamicLines。
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* astConfirmKind: "io-risk" 的集合取值——AST 确认为「真实 fs 调用」的行号。
|
|
132
|
+
* 正则初筛只看字符(会把注释/字符串里的 `fs.writeFile(...)` 示例、规则描述文本
|
|
133
|
+
* 一并命中);本集合由 scanIoRiskAst 经 tokenizer 给出,注释与字符串内的调用
|
|
134
|
+
* 不会出现在其中 —— 正则误报在此被 AST 修正。
|
|
135
|
+
* @param {string} text 文件全文
|
|
136
|
+
* @returns {Set<number>} 真实 I/O 调用所在行号集合
|
|
137
|
+
*/
|
|
138
|
+
export function ioRiskLines(text) {
|
|
139
|
+
return new Set(scanIoRiskAst(String(text || '')).map((h) => h.line));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* astConfirmKind: "io-risk" 的分级详情——行号 → 该行的分级命中(供 finding message 用)。
|
|
144
|
+
* 同一行可能有多个 I/O 调用,取**风险最高**的一条描述(改造优先级视角)。
|
|
145
|
+
* @param {string} text 文件全文
|
|
146
|
+
* @returns {Map<number, object>} 行号 → 命中项
|
|
147
|
+
*/
|
|
148
|
+
export function ioRiskDetail(text) {
|
|
149
|
+
const out = new Map();
|
|
150
|
+
const rank = { high: 0, medium: 1, low: 2, safe: 3 };
|
|
151
|
+
for (const h of scanIoRiskAst(String(text || ''))) {
|
|
152
|
+
const prev = out.get(h.line);
|
|
153
|
+
if (!prev || (rank[h.risk] ?? 9) < (rank[prev.risk] ?? 9)) out.set(h.line, h);
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function shellCdDynamicLines(text) {
|
|
159
|
+
return shellCdDynamicAst(String(text || ''));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* astConfirmKind: "write-into-gitignored" 的集合取值——写文件/目录目标命中仓库
|
|
164
|
+
* .gitignore 且可能不存在的行号(报)。需仓库上下文 repoPath(读 .gitignore),
|
|
165
|
+
* 无上下文时返回空集合(不报)。见 lib/ast/shell.js writeIntoGitignoredLines。
|
|
166
|
+
*/
|
|
167
|
+
export function writeIntoGitignoredLines(text, repoPath) {
|
|
168
|
+
return writeIntoGitignoredAst(String(text || ''), String(repoPath || ''));
|
|
169
|
+
}
|
|
170
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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 { checkFileHealth } from './file-health.js';
|
|
28
|
+
import { filterRulesByExt, filterRulesByPath, filterRulesByFileText } from './filter.js';
|
|
29
|
+
import { resolveDshClientEntries, resolveDshClientFragments } from '../audit/client-entry.js';
|
|
30
|
+
|
|
31
|
+
/** 客户端入口集合缓存:repoPath → Set(相对路径)(同一次审计内只算一次)。 */
|
|
32
|
+
const clientEntryCache = new Map();
|
|
33
|
+
/** 客户端分片目录前缀缓存:repoPath → Set(相对路径前缀)(同一次审计内只算一次)。 */
|
|
34
|
+
const clientFragmentCache = new Map();
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 推导当前文件的**文件级作用域**(供规则 yml 的 scope_rules 命中)。
|
|
38
|
+
* 两档(都由仓库 package.json 推导,见 lib/audit/client-entry.js):
|
|
39
|
+
* · `dsh-client-fragment` —— 客户端**分片源码**目录(`<入口目录>/<入口名>-parts/`,或
|
|
40
|
+
* package.json 的 `dsh.client.fragments` 显式声明)。分片是**片段**:单文件语法不成立、
|
|
41
|
+
* 行数天然可能超阈值 ⇒ 按片段定级(仍报、不扣分),而不是靠「不可拆分」把整类问题挡掉。
|
|
42
|
+
* · `dsh-client-entry` —— 客户端**构建产物**入口本身(运行态单文件的那一份)。
|
|
43
|
+
* 放在调度层是因为这里能拿到 opts.repoPath(仓库上下文),而检查器是纯函数。
|
|
44
|
+
* @param {{repoPath?:string, relPath?:string}} args
|
|
45
|
+
* @returns {string} 作用域名;无命中返回 ''(空串 = 无作用域)
|
|
46
|
+
*/
|
|
47
|
+
function resolveFileScope({ repoPath, relPath }) {
|
|
48
|
+
if (!repoPath) return '';
|
|
49
|
+
const rel = String(relPath || '').replace(/^\.\//, '');
|
|
50
|
+
if (!rel) return '';
|
|
51
|
+
if (!clientFragmentCache.has(repoPath)) clientFragmentCache.set(repoPath, resolveDshClientFragments(repoPath));
|
|
52
|
+
for (const prefix of clientFragmentCache.get(repoPath)) if (rel.startsWith(prefix)) return 'dsh-client-fragment';
|
|
53
|
+
if (!clientEntryCache.has(repoPath)) clientEntryCache.set(repoPath, resolveDshClientEntries(repoPath));
|
|
54
|
+
return clientEntryCache.get(repoPath).has(rel) ? 'dsh-client-entry' : '';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function runChecks({ file, relPath, text, grouped }, opts = {}) {
|
|
58
|
+
const quick = opts.level === 'quick';
|
|
59
|
+
// exts 过滤:规则声明目标扩展名时按当前文件裁剪(如 patch 规则只查 yml/yaml)
|
|
60
|
+
grouped = filterRulesByExt(grouped, relPath || file || '');
|
|
61
|
+
// 路径级过滤——exclude_paths 声明排除的目录整规则跳过(如 server/ lib/ test/)
|
|
62
|
+
grouped = filterRulesByPath(grouped, relPath || file || '');
|
|
63
|
+
// 三层审计 L1——file_patterns 文件内容级初筛(未命中 = 非候选,剔除进 L2)
|
|
64
|
+
grouped = filterRulesByFileText(grouped, text);
|
|
65
|
+
let findings = [];
|
|
66
|
+
if (grouped['[FUNC]']) findings = findings.concat(checkRegexRules({ file, text, rules: grouped['[FUNC]'], repoPath: opts?.repoPath }));
|
|
67
|
+
if (grouped['credential-ref']) findings = findings.concat(checkRegexRules({ file, text, rules: grouped['credential-ref'], repoPath: opts?.repoPath }));
|
|
68
|
+
if (grouped['regex']) findings = findings.concat(checkRegexRules({ file, text, rules: grouped['regex'], repoPath: opts?.repoPath }));
|
|
69
|
+
if (grouped['path-regex']) findings = findings.concat(checkPathRegexRules({ file, relPath, rules: grouped['path-regex'] }));
|
|
70
|
+
if (!quick && grouped['func-lines']) findings = findings.concat(checkFuncLines({ file, text, rules: grouped['func-lines'] }));
|
|
71
|
+
// 此前 9 个 kind 编译后无人消费(死桶)
|
|
72
|
+
if (!quick && grouped['credential-file']) findings = findings.concat(checkCredentialFiles({ file, relPath, rules: grouped['credential-file'] }));
|
|
73
|
+
if (!quick && grouped['min-length']) findings = findings.concat(checkMinLength({ file, text, rules: grouped['min-length'] }));
|
|
74
|
+
if (!quick && grouped['max-complexity']) findings = findings.concat(checkComplexity({ file, text, rules: grouped['max-complexity'] }));
|
|
75
|
+
if (!quick && grouped['max-depth']) findings = findings.concat(checkDepth({ file, text, rules: grouped['max-depth'] }));
|
|
76
|
+
if (!quick && grouped['max-lines']) findings = findings.concat(checkMaxLines({ file, text, rules: grouped['max-lines'], fileScope: resolveFileScope({ repoPath: opts?.repoPath, relPath: relPath || file }) }));
|
|
77
|
+
// 注释冗余密度(用户规则反馈①——减少行注释、保留块注释)。
|
|
78
|
+
// 测试文件豁免:测试 setup 标头注释/用例说明是常规写法(fixture 语义的一部分),与
|
|
79
|
+
// repeated-string 对 test 的豁免口径一致(audit-file.js maintainabilityScoped)。
|
|
80
|
+
if (!quick && grouped['comment-density'] && !/(^|[/\\])(test|tests|__tests__|spec)[/\\]|\.(test|spec)\.(js|mjs|cjs|ts|py|tsx)$/i.test(String(relPath || file))) {
|
|
81
|
+
findings = findings.concat(checkCommentDensity({ file, text, rules: grouped['comment-density'] }));
|
|
82
|
+
}
|
|
83
|
+
// 1.0.13:文件健康度矩阵(行数/大小/行长三维评分)
|
|
84
|
+
if (!quick && grouped['file-health']) findings = findings.concat(checkFileHealth({ file, relPath, text, rules: grouped['file-health'] }));
|
|
85
|
+
// repeated-string 与 min-occurrences 是两个 kind 但检测逻辑相同(同文本双报的根因)——
|
|
86
|
+
// 合并规则列表一次运行,checkRepeated 内按文本去重(同一文本只报一条,不再「no-duplicate-code +
|
|
87
|
+
// no-repeated-hardcoded-literals」同文本报两条)。
|
|
88
|
+
if (!quick && (grouped['repeated-string'] || grouped['min-occurrences'])) {
|
|
89
|
+
findings = findings.concat(checkRepeated({
|
|
90
|
+
file, text,
|
|
91
|
+
rules: [...(grouped['repeated-string'] || []), ...(grouped['min-occurrences'] || [])],
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
if (!quick && grouped['semantic']) findings = findings.concat(checkSemantic({ file, relPath, rules: grouped['semantic'], repoLevelRules: opts?.repoLevelRules }));
|
|
95
|
+
if (grouped['blacklist']) findings = findings.concat(checkBlacklist({ file, text, rules: grouped['blacklist'] }));
|
|
96
|
+
if (grouped['npm-json']) findings = findings.concat(checkNpmJson({ file, text, rules: grouped['npm-json'], repoHasJsYamlImport: opts?.repoHasJsYamlImport, repoPath: opts?.repoPath }));
|
|
97
|
+
// patch insert 语义检查(yml 文件 insert/覆盖 id 冲突才报)
|
|
98
|
+
if (grouped['patch-insert']) findings = findings.concat(checkPatchInsert({ file, text, rules: grouped['patch-insert'] }));
|
|
99
|
+
// 1.0.5:按钮事件绑定交叉比对(油猴脚本/扩展——HTML 在 JS 字符串,事件在 JS 绑定)
|
|
100
|
+
if (grouped['button-bind']) findings = findings.concat(checkButtonBindings({ file, text, rules: grouped['button-bind'] }));
|
|
101
|
+
// 1.0.7:硬编码魔数检测(版本号豁免版——版本号/日期/HTTP 状态码/常见合法常量/状态枚举自动豁免)
|
|
102
|
+
if (grouped['magic-number-smart']) findings = findings.concat(checkMagicNumberSmart({ file, text, rules: grouped['magic-number-smart'] }));
|
|
103
|
+
// 三层审计 L2——数据流检查(kind=dataflow,如 clear-then-access 同函数清空后访问)
|
|
104
|
+
// L1 已在 filterRulesByFileText 初筛:file_patterns 未命中的文件这里 grouped 已空,空规则短路
|
|
105
|
+
if (!quick && grouped['dataflow']) findings = findings.concat(checkDataflow({ file, text, rules: grouped['dataflow'] }));
|
|
106
|
+
findings = findings.concat(checkEmptyCatch({ file, text }));
|
|
107
|
+
// I/O 风险分级 —— 经「正则初筛 → AST 精筛」产出(robustness/io-risk
|
|
108
|
+
// 规则 + astConfirmKind: io-risk),不在此独立调用检查器:两条路径会为同一处 I/O
|
|
109
|
+
// 各报一次(实测 6 处重叠),且既有架构里 astConfirmKind 类规则一律只走正则路径、
|
|
110
|
+
// AST 只做过滤(与 shell-cd-dynamic / write-into-gitignored 等一致)。
|
|
111
|
+
return findings;
|
|
112
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
}
|