dsh-git-push 0.0.0-stage → 2.8.6
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 +1486 -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 +266 -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 +192 -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 +672 -0
- package/lib/app/tools.js +34 -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 +215 -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 +137 -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 +148 -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 +476 -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 +171 -0
- package/lib/npm/registry.js +168 -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 +196 -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/cpu-sample.mjs +188 -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
package/lib/app/tools.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件入口层 · 工具定义(单源:从注册表生成——不再手写双源)
|
|
3
|
+
*
|
|
4
|
+
* listTools 由 lib/app/command-registry.js 的 TOOL_REGISTRY 生成——
|
|
5
|
+
* name/description/parameters 单一规则源:增删工具只改注册表一处,
|
|
6
|
+
* CLI 命令 / 宿主工具 schema / 帮助文档自动对齐(方案 B,免维护)。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { TOOL_REGISTRY } from './command-registry.js';
|
|
10
|
+
|
|
11
|
+
/** 注册表 params({flag,type,desc,...})→ 宿主工具 parameters 对象格式。 */
|
|
12
|
+
function paramsToParameters(params = []) {
|
|
13
|
+
const out = {};
|
|
14
|
+
for (const p of params) {
|
|
15
|
+
// 参数类型 → JSON schema:**只产出宿主认的标量类型**(boolean / string / array)——
|
|
16
|
+
// 实测宿主校验不认 anyOf、也不认 JSON Schema 的「类型数组」写法(都按 string 处理 ✗,
|
|
17
|
+
// 报 invalid arguments: "x" must be a string)⇒ 复杂形态一律拆成独立参数 ✓
|
|
18
|
+
if (p.type === 'array') {
|
|
19
|
+
out[p.flag] = { type: 'array', description: p.desc || '' };
|
|
20
|
+
} else {
|
|
21
|
+
out[p.flag] = { type: p.type === 'boolean' ? 'boolean' : 'string', description: p.desc || '' };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 工具清单(名 + 说明 + 参数 spec——由注册表生成,单源;lib/plugin/normalizeParameters 转 DSH 形状注册)。 */
|
|
28
|
+
export function listTools() {
|
|
29
|
+
return TOOL_REGISTRY.map((t) => ({
|
|
30
|
+
name: t.name,
|
|
31
|
+
description: t.description,
|
|
32
|
+
parameters: paramsToParameters(t.params),
|
|
33
|
+
}));
|
|
34
|
+
}
|
package/lib/ast/brace.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST 实现层 · 括号与区间工具
|
|
3
|
+
*
|
|
4
|
+
* 职责:括号配对、区间包含判定。供复杂度/嵌套/函数行数检查复用,
|
|
5
|
+
* 让「某个 token 属于哪个函数体」这类判断只在一处实现。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// 从 function 关键字向后找函数体 '{' 的最大 token 跨度:与 control-flow.js 共用同一来源
|
|
9
|
+
// (lib/ast/consts.js)——两处必须永远相等(自审 duplicate-constant 报的真重复)
|
|
10
|
+
import { FN_BODY_LOOKAHEAD } from './consts.js';
|
|
11
|
+
|
|
12
|
+
/** 括号平衡扫描:从 startIdx 的 '{' 找匹配 '}',返回 [openIdx, closeIdx] 或 null。 */
|
|
13
|
+
export function matchBrace(tokens, openIdx) {
|
|
14
|
+
let depth = 0;
|
|
15
|
+
for (let i = openIdx; i < tokens.length; i++) {
|
|
16
|
+
const t = tokens[i];
|
|
17
|
+
if (t.type !== 'punct') continue;
|
|
18
|
+
if (t.value === '{') depth++;
|
|
19
|
+
else if (t.value === '}') {
|
|
20
|
+
depth--;
|
|
21
|
+
if (depth === 0) return [openIdx, i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 从 closeIdx 处 closePunct 向右→左配对,返回 openPunct 下标;不匹配返回 -1。
|
|
29
|
+
* 抽公共:原 matchingOpen({})/ matchingBracketBack([])/ matchingParenBack(())
|
|
30
|
+
* + isBlockParen 的配对循环是同一逻辑四处复制,统一为本函数(dup-code 规则命中案例)。
|
|
31
|
+
* @param {Array} tokens token 流
|
|
32
|
+
* @param {number} closeIdx 右括号下标
|
|
33
|
+
* @param {string} openPunct 左括号字符('{' / '[' / '(')
|
|
34
|
+
* @param {string} closePunct 右括号字符('}' / ']' / ')')
|
|
35
|
+
* @returns {number} 配对左括号下标;无配对返回 -1
|
|
36
|
+
*/
|
|
37
|
+
export function matchPairBack(tokens, closeIdx, openPunct, closePunct) {
|
|
38
|
+
let level = 0;
|
|
39
|
+
for (let j = closeIdx; j >= 0; j--) {
|
|
40
|
+
const tj = tokens[j];
|
|
41
|
+
if (tj.type !== 'punct') continue;
|
|
42
|
+
if (tj.value === closePunct) level++;
|
|
43
|
+
else if (tj.value === openPunct) { level--; if (level === 0) return j; }
|
|
44
|
+
}
|
|
45
|
+
return -1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 判断 `)` 是否属于控制流/函数签名的参数表尾(即其后 `{` 是块语句)。
|
|
50
|
+
* 从 `)` 向前做括号配对找起始 `(`,再看 `(` 前一个 token:
|
|
51
|
+
* if/for/while/switch/catch/function/标识符(函数声明/表达式/方法名) → 是块语句头。
|
|
52
|
+
* @param {Array} tokens token 流
|
|
53
|
+
* @param {object} closeTok `)` token
|
|
54
|
+
* @returns {boolean}
|
|
55
|
+
*/
|
|
56
|
+
export function isBlockParen(tokens, closeTok) {
|
|
57
|
+
const closeIdx = tokens.indexOf(closeTok);
|
|
58
|
+
const openIdx = matchPairBack(tokens, closeIdx, '(', ')');
|
|
59
|
+
if (openIdx === -1) return false;
|
|
60
|
+
for (let j = openIdx - 1; j >= 0; j--) {
|
|
61
|
+
const tj = tokens[j];
|
|
62
|
+
if (tj.type === 'ws' || tj.type === 'comment') continue;
|
|
63
|
+
if (tj.type === 'ident') return true; // if (…) / myFunc (…) / function f (…)
|
|
64
|
+
// `)` `]` `.` 等 → 调用表达式(如 foo.bar(…)),其后 `{` 多半是对象实参 → 不计
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 找与 closeIdx 处 `}` 配对的 `{` 下标(无配对返回 -1)。 */
|
|
71
|
+
export function matchingOpen(tokens, closeIdx) {
|
|
72
|
+
return matchPairBack(tokens, closeIdx, '{', '}');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 检查圈复杂度(max-complexity kind,AST 级)。
|
|
77
|
+
* 复杂度 = 1 + 分支关键字数(if/for/while/case/catch/&&/||/?/??)。
|
|
78
|
+
* @param {string} text 文件全文
|
|
79
|
+
* @param {object} [opts] { warn=10, block=20 }
|
|
80
|
+
* @returns {Array<{line:number, name:string, complexity:number, level:string}>}
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* 收集 [from, to) 区间内所有「嵌套 function 体」的 token 区间。
|
|
84
|
+
* 供圈复杂度计算排除内层贡献(内层由外层循环单独评估)。
|
|
85
|
+
*
|
|
86
|
+
* @param {Array} tokens token 流
|
|
87
|
+
* @param {number} from 起始下标(含)
|
|
88
|
+
* @param {number} to 结束下标(不含)
|
|
89
|
+
* @returns {Array<[number, number]>} 各内层函数体的 [openBraceIdx, closeBraceIdx]
|
|
90
|
+
*/
|
|
91
|
+
export function collectInnerFnRanges(tokens, from, to) {
|
|
92
|
+
const ranges = [];
|
|
93
|
+
for (let i = from; i < to; i++) {
|
|
94
|
+
const t = tokens[i];
|
|
95
|
+
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
96
|
+
let openIdx = -1;
|
|
97
|
+
for (let j = i + 1; j < Math.min(to, i + FN_BODY_LOOKAHEAD); j++) {
|
|
98
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '{') { openIdx = j; break; }
|
|
99
|
+
}
|
|
100
|
+
if (openIdx === -1) continue;
|
|
101
|
+
const r = matchBrace(tokens, openIdx);
|
|
102
|
+
if (r) ranges.push(r);
|
|
103
|
+
}
|
|
104
|
+
return ranges;
|
|
105
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 调用链追踪(方案 Step 5/P3)——单文件调用图
|
|
3
|
+
*
|
|
4
|
+
* 用途:io-risk 等规则的「请求路径」判定升级——不只靠函数体内特征(detectRequestContext),
|
|
5
|
+
* 还看**调用链**:被请求路径函数调用的函数,也在请求路径上。
|
|
6
|
+
*
|
|
7
|
+
* 局限(按方案标记「需人工复核」不强行判断):跨文件调用、动态调用 obj[method]()、
|
|
8
|
+
* 回调传递断链。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { tokenize } from './tokenizer.js';
|
|
12
|
+
|
|
13
|
+
/** 请求路径函数名模式(子串匹配,与 io-risk 的 REQUEST_PATTERNS 语义互补)。 */
|
|
14
|
+
const REQUEST_PATH_NAMES = ['handle', 'request', 'route', 'onrequest', 'middleware', 'api', 'controller', 'service'];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 构建单文件调用图:calleeName → Set(callerName)(调用者列表)。
|
|
18
|
+
* 只统计「具名函数体内」的具名调用(ident(...) 且被调名是 ident),
|
|
19
|
+
* 动态调用(obj[method](...) / 匿名回调)天然落不进图——按盲区处理。
|
|
20
|
+
* @param {string} text 文件全文
|
|
21
|
+
* @returns {Map<string, Set<string>>} callee → callers
|
|
22
|
+
*/
|
|
23
|
+
export function buildCallGraph(text) {
|
|
24
|
+
const tokens = tokenize(text);
|
|
25
|
+
const graph = new Map();
|
|
26
|
+
// 函数边界(名称 → [startIdx, endIdx]):function NAME / const NAME = function / NAME = (...) => / NAME(...) { }
|
|
27
|
+
const fnRanges = collectNamedFnRanges(tokens);
|
|
28
|
+
for (const [fnName, [startIdx, endIdx]] of fnRanges) {
|
|
29
|
+
for (let i = startIdx; i < endIdx; i++) {
|
|
30
|
+
const t = tokens[i];
|
|
31
|
+
if (t.type !== 'ident') continue;
|
|
32
|
+
const nx = tokens[i + 1];
|
|
33
|
+
if (!(nx && nx.type === 'punct' && nx.value === '(')) continue;
|
|
34
|
+
// 排除函数自身声明(function NAME(处的 NAME 后是 '(' 但那是声明)——fnName 的声明 token 不在本函数体前
|
|
35
|
+
if (t.value === fnName && tokens[i - 1] && tokens[i - 1].type === 'ident' && ['function', 'const', 'let', 'var'].includes(tokens[i - 1].value)) continue;
|
|
36
|
+
const callee = t.value;
|
|
37
|
+
if (!graph.has(callee)) graph.set(callee, new Set());
|
|
38
|
+
graph.get(callee).add(fnName);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return graph;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 判定函数是否在请求路径上(调用链向上追溯:本函数被某请求路径函数调用,或
|
|
46
|
+
* 该调用者又被请求路径函数调用……)。向上遍历防环。
|
|
47
|
+
* @param {string} fnName 目标函数名
|
|
48
|
+
* @param {Map<string, Set<string>>} graph buildCallGraph 结果
|
|
49
|
+
* @param {(name:string)=>boolean} [isRequestName] 请求路径函数名判定(缺省 REQUEST_PATH_NAMES)
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
export function isInRequestPath(fnName, graph, isRequestName) {
|
|
53
|
+
const isReq = isRequestName || ((n) => REQUEST_PATH_NAMES.some((p) => String(n).toLowerCase().includes(p)));
|
|
54
|
+
const visited = new Set();
|
|
55
|
+
const stack = [fnName];
|
|
56
|
+
while (stack.length) {
|
|
57
|
+
const cur = stack.pop();
|
|
58
|
+
if (visited.has(cur)) continue;
|
|
59
|
+
visited.add(cur);
|
|
60
|
+
const callers = graph.get(cur);
|
|
61
|
+
if (!callers) continue;
|
|
62
|
+
for (const caller of callers) {
|
|
63
|
+
if (isReq(caller)) return true;
|
|
64
|
+
stack.push(caller);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** ① function NAME(...) { ... } 声明收集(找函数体 `{` 限 24 token)。 */
|
|
71
|
+
function collectFunctionDecls(tokens) {
|
|
72
|
+
const out = [];
|
|
73
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
74
|
+
const t = tokens[i];
|
|
75
|
+
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
76
|
+
let j = i + 1;
|
|
77
|
+
let name = '(匿名)';
|
|
78
|
+
if (tokens[j] && tokens[j].type === 'ident') { name = tokens[j].value; j += 1; }
|
|
79
|
+
// 找函数体 {(限 24 token)
|
|
80
|
+
let openIdx = -1;
|
|
81
|
+
for (let k = j; k < Math.min(tokens.length, j + 24); k++) {
|
|
82
|
+
if (tokens[k].type === 'punct' && tokens[k].value === '{') { openIdx = k; break; }
|
|
83
|
+
if (tokens[k].type === 'punct' && tokens[k].value === ';') break;
|
|
84
|
+
}
|
|
85
|
+
if (openIdx === -1) continue;
|
|
86
|
+
const closeIdx = matchBraceIdx(tokens, openIdx);
|
|
87
|
+
if (closeIdx > openIdx) { if (name !== '(匿名)') out.push([name, [openIdx, closeIdx]]); i = closeIdx; }
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** ② const NAME = (...) => { } 箭头函数收集(找 => 后第一个 { 限 30 token)。 */
|
|
93
|
+
function collectArrowFns(tokens) {
|
|
94
|
+
const out = [];
|
|
95
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
96
|
+
const t = tokens[i];
|
|
97
|
+
if (!(t.type === 'ident' && ['const', 'let', 'var'].includes(t.value))) continue;
|
|
98
|
+
const name = tokens[i + 1];
|
|
99
|
+
if (!(name && name.type === 'ident')) continue;
|
|
100
|
+
// 向后找 => 后第一个 {(限 30 token)
|
|
101
|
+
for (let j = i + 2; j < Math.min(tokens.length, i + 30); j++) {
|
|
102
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '=>') {
|
|
103
|
+
let openIdx = -1;
|
|
104
|
+
for (let k = j + 1; k < Math.min(tokens.length, j + 24); k++) {
|
|
105
|
+
if (tokens[k].type === 'punct' && tokens[k].value === '{') { openIdx = k; break; }
|
|
106
|
+
}
|
|
107
|
+
if (openIdx !== -1) {
|
|
108
|
+
const closeIdx = matchBraceIdx(tokens, openIdx);
|
|
109
|
+
if (closeIdx > openIdx) out.push([name.value, [openIdx, closeIdx]]);
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
if (tokens[j].type === 'punct' && tokens[j].value === ';') break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 收集具名函数体 token 区间:[name, [startIdx, endIdx]](function NAME / const NAME = (...) => {} / NAME = function)。 */
|
|
120
|
+
export function collectNamedFnRanges(tokens) {
|
|
121
|
+
return [...collectFunctionDecls(tokens), ...collectArrowFns(tokens)];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 从 openIdx(应为 `{`)找配对闭括号下标(无配对返回 openIdx)。 */
|
|
125
|
+
function matchBraceIdx(tokens, openIdx) {
|
|
126
|
+
let depth = 0;
|
|
127
|
+
for (let j = openIdx; j < tokens.length; j++) {
|
|
128
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '{') depth++;
|
|
129
|
+
else if (tokens[j].type === 'punct' && tokens[j].value === '}') {
|
|
130
|
+
depth--;
|
|
131
|
+
if (depth === 0) return j;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return openIdx;
|
|
135
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST 实现层 · 代码行判定
|
|
3
|
+
*
|
|
4
|
+
* 职责:判断某行是否「真在代码里」(而非注释/字符串内),并取出代码里的字符串字面量。
|
|
5
|
+
* 供 regex 类规则做二次确认,避免注释中的示例被当真实代码报错。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { tokenize } from './tokenizer.js';
|
|
9
|
+
|
|
10
|
+
/* ───────────────────────── 正则初筛 → AST 精筛(通用设施) ───────────────────────── */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 正则初筛 → token 级精筛(所有文本型检查的通用两段式)。
|
|
14
|
+
*
|
|
15
|
+
* 为什么两段式:正则快、能一次筛出候选行;但正则无法区分「代码」与「注释/字符串」,
|
|
16
|
+
* 单用必然误报(注释版本号、CSS 字号、i18n 字典值)。tokenizer 能精确分类,但全量
|
|
17
|
+
* token 化成本高于正则。两段式 = 正则初筛候选行 → 只对候选行做 token 判定。
|
|
18
|
+
*
|
|
19
|
+
* @param {string} text 文件全文
|
|
20
|
+
* @param {number[]} candidateLines 正则初筛命中的行号(1-based)
|
|
21
|
+
* @returns {{isCode: (line:number)=>boolean}} isCode(line) 判定该行是否含「代码 token」
|
|
22
|
+
* (注释/字符串/模板串/纯空白行 → false;含 ident/num/punct 的行 → true)
|
|
23
|
+
*/
|
|
24
|
+
export function makeCodeLineFilter(text = '', candidateLines = [], opts = {}) {
|
|
25
|
+
// 散文体文档(markdown/txt):tokenizer 会把自然语言词判成 ident
|
|
26
|
+
// (「CIFS」「挂载适配」都是 ident),于是整段散文被当成「含代码 token 的行」,
|
|
27
|
+
// astConfirm 形同虚设——实测 README 版本记录里说明「token 被误报」的那句话
|
|
28
|
+
// 本身被 security/no-hardcoded-credentials 报成 blocker。
|
|
29
|
+
// 故对这类文件改用文档语义:只有**围栏代码块内**与**行内代码**才算代码。
|
|
30
|
+
if (isProseDoc(text, opts.file)) return makeProseCodeFilter(text, candidateLines);
|
|
31
|
+
|
|
32
|
+
const want = new Set(candidateLines);
|
|
33
|
+
const codeLines = new Set();
|
|
34
|
+
const suspect = new Set();
|
|
35
|
+
for (const t of tokenize(text)) {
|
|
36
|
+
if (!want.has(t.line)) continue;
|
|
37
|
+
if (t.type === 'ident' || t.type === 'num') codeLines.add(t.line);
|
|
38
|
+
if (t.type === 'punct') suspect.add(t.line);
|
|
39
|
+
}
|
|
40
|
+
for (const lineNo of suspect) {
|
|
41
|
+
// 只有标点(如 `}` `);`)不算代码行,需同时有 ident/num
|
|
42
|
+
if (!codeLines.has(lineNo)) codeLines.delete(lineNo);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
isCode: (line) => codeLines.has(line),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 是否散文体文档 —— **只按扩展名判定**。
|
|
51
|
+
*
|
|
52
|
+
* 刻意不做内容特征兜底:shell 注释 `# cd "$X"` 与 markdown 标题 `# 标题`
|
|
53
|
+
* 在文本层面完全同形,任何基于「以 # 开头」的启发式都会把 shell 脚本
|
|
54
|
+
* 误判为文档,进而让所有行按「非围栏、非行内代码」被剔除——实测使
|
|
55
|
+
* folder/cd-to-maybe-missing 规则在 .sh 上整体失效(回归测试 test 542 失败)。
|
|
56
|
+
* 调用方(lib/ast/shell.js)不传文件名,此时按代码处理是正确的默认。
|
|
57
|
+
* @param {string} text 文件全文(当前未使用,保留签名以备扩展)
|
|
58
|
+
* @param {string} file 文件路径(用于取扩展名)
|
|
59
|
+
* @returns {boolean}
|
|
60
|
+
*/
|
|
61
|
+
function isProseDoc(text, file) {
|
|
62
|
+
return /\.(md|markdown|txt|rst)$/i.test(String(file || ''));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 文档语义的代码行判定:只有围栏代码块内、以及**行内代码片段本身**算「代码」。
|
|
67
|
+
*
|
|
68
|
+
* 关键区别在行内代码:不能「该行有行内代码 → 整行放行」。文档里一行往往既有
|
|
69
|
+
* 散文又有多个行内片段(如版本记录一行写「...`chmod`/`utimes` 因 SMB 无 POSIX
|
|
70
|
+
* Extensions...`lib/fsx.js` 统一收口...」),整行放行等于把散文也当代码。
|
|
71
|
+
* 实测 README 说明「token 被误报」的那句话因此被报成 blocker。
|
|
72
|
+
* 做法:把该行的行内代码片段拼成「代码视图」,仅当代码视图里含 ident/num
|
|
73
|
+
* (即片段本身像代码)时才标为候选,供后续 token 级精筛继续判定。
|
|
74
|
+
*
|
|
75
|
+
* @returns {{isCode: (line:number)=>boolean}}
|
|
76
|
+
*/
|
|
77
|
+
function makeProseCodeFilter(text, candidateLines) {
|
|
78
|
+
const want = new Set(candidateLines);
|
|
79
|
+
const lines = String(text).split('\n');
|
|
80
|
+
const codeLines = new Set();
|
|
81
|
+
let inFence = false;
|
|
82
|
+
for (let i = 0; i < lines.length; i++) {
|
|
83
|
+
const line = lines[i];
|
|
84
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
85
|
+
// 围栏标记行本身不算代码(它是文档语法)
|
|
86
|
+
inFence = !inFence;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (inFence) { codeLines.add(i + 1); continue; }
|
|
90
|
+
if (!want.has(i + 1)) continue; // 只判候选行,省 token 化成本
|
|
91
|
+
// 取该行全部行内代码片段,判断「片段自身」是否像代码
|
|
92
|
+
const snippets = String(line).match(/`[^`]+`/g) || [];
|
|
93
|
+
if (!snippets.length) continue;
|
|
94
|
+
const snippetText = snippets.map((x) => x.slice(1, -1)).join('\n');
|
|
95
|
+
for (const t of tokenize(snippetText)) {
|
|
96
|
+
if (t.type === 'ident' || t.type === 'num' || t.type === 'str') { codeLines.add(i + 1); break; }
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { isCode: (line) => codeLines.has(line) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 判定一组字符串字面量值是否出现在代码里(供「重复硬编码串」等规则精筛)。
|
|
104
|
+
* @param {string} text 文件全文
|
|
105
|
+
* @returns {Set<string>} 代码中出现的字符串字面量裸值集合
|
|
106
|
+
*/
|
|
107
|
+
export function codeStringLiterals(text = '') {
|
|
108
|
+
const out = new Set();
|
|
109
|
+
for (const t of tokenize(text)) {
|
|
110
|
+
if (t.type !== 'str' && t.type !== 'tmpl') continue;
|
|
111
|
+
out.add(String(t.value).replace(/^(['"`])([\s\S]*)\1$/, '$2'));
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// AST 判定层公共常量(单一来源)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独成模块:同一常量此前在 lib/ast 的多个文件各写一份(如 FN_BODY_LOOKAHEAD 在
|
|
4
|
+
// brace.js 与 control-flow.js),两处必须永远相等——漏改一处会出现「括号匹配按 20 回看、
|
|
5
|
+
// 控制流按别的值回看」这类静默不一致(自审 maintainability/duplicate-constant-def 报的真重复)。
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 找函数体起始 `{` 时向后回看的 token 数。
|
|
9
|
+
* 函数签名可能很长(多行形参/默认值/泛型/装饰器),回看窗口太小会找不到 `{` 而漏判函数体,
|
|
10
|
+
* 太大则可能越过边界把下一个块的 `{` 当成函数体——20 是实测能覆盖本仓库全部形态的值。
|
|
11
|
+
*/
|
|
12
|
+
export const FN_BODY_LOOKAHEAD = 20;
|