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
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-git-push — I/O 风险分级:函数边界识别与 token 配对
|
|
3
|
+
*
|
|
4
|
+
* 职责:从 token 流里认出函数边界(普通函数 / 箭头函数 / 对象方法简写 / 类方法),
|
|
5
|
+
* 以及圆括号、花括号、方括号的前后向配对 —— 上下文判定的公共基础设施。
|
|
6
|
+
* 不含「风险等级」概念,只回答「这段代码属于哪个函数体」。
|
|
7
|
+
*
|
|
8
|
+
* 分层:lib/ast/io-risk.js 的从属模块(由该文件再导出),调用方不应直接 import。
|
|
9
|
+
*/
|
|
10
|
+
import { matchBrace } from './brace.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 取包含该行的最内层循环范围(含 smallFixed 标记)。
|
|
14
|
+
* 嵌套循环时取最内层:内层规模才是单次 I/O 的实际重复次数。
|
|
15
|
+
* @returns {Array|null} [起行, 止行, smallFixed]
|
|
16
|
+
*/
|
|
17
|
+
export function enclosingLoop(loops, line) {
|
|
18
|
+
let best = null;
|
|
19
|
+
for (const entry of loops) {
|
|
20
|
+
if (line < entry[0] || line > entry[1]) continue;
|
|
21
|
+
if (!best || entry[0] >= best[0]) best = entry;
|
|
22
|
+
}
|
|
23
|
+
return best;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 函数体 '{' 的搜索窗口(token 数):够覆盖 `async function name(a, b, c) {` 这类长签名。 */
|
|
27
|
+
export const BODY_SEARCH_TOKENS = 40;
|
|
28
|
+
/** 循环体 '{' 的搜索窗口(token 数):够覆盖 `for (const x of someLongExpression) {`。 */
|
|
29
|
+
export const LOOP_BODY_SEARCH_TOKENS = 30;
|
|
30
|
+
/** 不能作为函数名的关键字:这些后面跟 `(...) {` 时是语句块,不是方法简写。 */
|
|
31
|
+
export const FUNCTION_HEAD_EXCLUDE = new Set([
|
|
32
|
+
'for', 'while', 'if', 'else', 'switch', 'do', 'try', 'catch', 'finally',
|
|
33
|
+
'return', 'typeof', 'new', 'delete', 'void', 'in', 'of', 'await', 'yield',
|
|
34
|
+
]);
|
|
35
|
+
/** 箭头函数 `=>` 的搜索窗口(token 数):够覆盖 `const f = (a, b) => {`。 */
|
|
36
|
+
export const ARROW_SEARCH_TOKENS = 60;
|
|
37
|
+
/** 「固定小循环」的元素数上限:字面量数组不超过此值时,循环内同步 I/O 不算高风险。 */
|
|
38
|
+
export const SMALL_FIXED_LOOP_MAX = 5;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 找函数/循环体的 '{'。
|
|
42
|
+
* @returns {number} '{' 的 token 下标;遇 ';' 或超窗口返回 -1
|
|
43
|
+
*/
|
|
44
|
+
export function findBody(tokens, from, window = BODY_SEARCH_TOKENS) {
|
|
45
|
+
for (let j = from; j < Math.min(tokens.length, from + window); j++) {
|
|
46
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '{') return j;
|
|
47
|
+
if (tokens[j].type === 'punct' && tokens[j].value === ';') return -1;
|
|
48
|
+
}
|
|
49
|
+
return -1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 识别 `async` 开头的函数/箭头函数,返回其函数体 '{' 下标。
|
|
54
|
+
* 覆盖三种形态:`async function f() {` / `async (...) => {` / `async x => {`
|
|
55
|
+
* @returns {number} '{' 下标;无法识别返回 -1
|
|
56
|
+
*/
|
|
57
|
+
export function findAsyncBody(tokens, i) {
|
|
58
|
+
const nx = tokens[i + 1];
|
|
59
|
+
if (nx && nx.type === 'ident' && nx.value === 'function') return findBody(tokens, i + 2);
|
|
60
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + BODY_SEARCH_TOKENS); j++) {
|
|
61
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '=>') return findBody(tokens, j + 1);
|
|
62
|
+
if (tokens[j].type === 'punct' && tokens[j].value === ';') break;
|
|
63
|
+
}
|
|
64
|
+
return -1;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 识别循环体范围。
|
|
69
|
+
* 两种收尾形态:① `for (...) {` 花括号配对;② 单语句体 `for (...) x++;`
|
|
70
|
+
* @returns {[number, number]|null} [起行, 止行]
|
|
71
|
+
*/
|
|
72
|
+
export function findLoopRange(tokens, i) {
|
|
73
|
+
const found = seekLoopBody(tokens, i);
|
|
74
|
+
if (!found) return null;
|
|
75
|
+
// 单语句循环体:结束行由扫描时确定
|
|
76
|
+
const endLine = found.singleStmt ? found.endLine : endLineOf(tokens, matchBrace(tokens, found.brace));
|
|
77
|
+
if (endLine === null) return null;
|
|
78
|
+
// 起始行取**循环体**而非循环头:
|
|
79
|
+
// `for (const line of readFileSync(f).split('\n')) {` 里 readFileSync 只执行一次,
|
|
80
|
+
// 若把循环头那一行也算进范围,就会被误报成「循环内 I/O」(实测 repos.js:81)。
|
|
81
|
+
// 有花括号体时用 '{' 所在行;单语句体时用语句自身所在行。
|
|
82
|
+
const bodyStart = found.singleStmt
|
|
83
|
+
? (tokens[i + 1]?.line ?? tokens[i].line)
|
|
84
|
+
: tokens[found.brace].line;
|
|
85
|
+
return [Math.max(tokens[i].line, bodyStart), endLine];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 花括号配对结果的结束行;配对失败返回 null。 */
|
|
89
|
+
export function endLineOf(tokens, range) {
|
|
90
|
+
return range ? tokens[range[1]].line : null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 从循环关键字找循环体。
|
|
95
|
+
* 拆自 findLoopRange(原函数圈复杂度 14,超阈值 10)。
|
|
96
|
+
* @returns {{brace:number}|{singleStmt:true,endLine:number}|null}
|
|
97
|
+
*/
|
|
98
|
+
export function seekLoopBody(tokens, i) {
|
|
99
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + LOOP_BODY_SEARCH_TOKENS); j++) {
|
|
100
|
+
const tj = tokens[j];
|
|
101
|
+
if (tj.type === 'punct' && tj.value === '{') return { brace: j };
|
|
102
|
+
if (tj.type === 'punct' && tj.value === ';') return null;
|
|
103
|
+
if (isSingleStmtLoopClose(tokens, j)) return { singleStmt: true, endLine: tj.line };
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 单语句循环体(`for (...) x++;`):括号配对完成却没 '{' → 认到该语句结束。 */
|
|
109
|
+
export function isSingleStmtLoopClose(tokens, j) {
|
|
110
|
+
const tj = tokens[j];
|
|
111
|
+
if (tj.type !== 'punct' || tj.value !== ')') return false;
|
|
112
|
+
const nextT = tokens[j + 1];
|
|
113
|
+
if (!nextT) return false;
|
|
114
|
+
return !(nextT.type === 'punct' && nextT.value === '{');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 识别**同步**函数体:`function f(){}` / `const f = () => {}` / `{ f(){} }` 方法简写。
|
|
119
|
+
* async 形态由 classifyFnHead 处理(此处跳过,避免与它重复收录)。
|
|
120
|
+
* @returns {[number, number]|null} [起行, 止行]
|
|
121
|
+
*/
|
|
122
|
+
export function classifySyncFnHead(tokens, i, t) {
|
|
123
|
+
// `function name(` —— 前面不是 async(async 已在上一分支处理)
|
|
124
|
+
if (t.value === 'function') {
|
|
125
|
+
const open = findBody(tokens, i + 1);
|
|
126
|
+
return rangeOf(tokens, open);
|
|
127
|
+
}
|
|
128
|
+
// 关键字一律不是函数名:`for (...) {` / `if (...) {` / `while (...) {` 等
|
|
129
|
+
// 与「方法简写 name(...) {」在 token 形态上完全同形,不排除会把循环/分支
|
|
130
|
+
// 体误收成函数体,进而让其中的 I/O 丢掉 inLoop/inAsync 上下文
|
|
131
|
+
// (实测:for 循环被当成方法后 inLoop 恒 false,三条循环用例全挂)。
|
|
132
|
+
if (FUNCTION_HEAD_EXCLUDE.has(t.value)) return null;
|
|
133
|
+
const next = tokens[i + 1];
|
|
134
|
+
if (!next || next.type !== 'punct') return null;
|
|
135
|
+
// 方法简写**先判**:`name(...) {` —— '(' 配对后紧跟 '{' 即是方法体。
|
|
136
|
+
// 不能先找 `=>`:那样会把 `h(req,res){ ... }` 的方法体一路扫成箭头候选
|
|
137
|
+
// (深度归零后继续前进,越过分号,最终撞到外层 '}' 才以 -1 退出),
|
|
138
|
+
// 导致对象方法简写的函数体从未被收录(实测 inRequest 漏判)。
|
|
139
|
+
if (next.value === '(') {
|
|
140
|
+
const brace = findBodyAfterParen(tokens, i + 1);
|
|
141
|
+
if (brace !== -1) return rangeOf(tokens, brace);
|
|
142
|
+
}
|
|
143
|
+
// 箭头函数:`name = (...) => {` / `name: (...) => {`
|
|
144
|
+
if (next.value === '=' || next.value === ':') {
|
|
145
|
+
const arrow = findArrowBody(tokens, i);
|
|
146
|
+
if (arrow !== -1) return rangeOf(tokens, arrow);
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 花括号配对 → 行范围;open 非法返回 null。 */
|
|
152
|
+
export function rangeOf(tokens, open) {
|
|
153
|
+
if (open === -1) return null;
|
|
154
|
+
const range = matchBrace(tokens, open);
|
|
155
|
+
return range ? [tokens[open].line, tokens[range[1]].line] : null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 从标识符起找同一层的 '=>',返回其后函数体 '{' 下标(无则 -1)。 */
|
|
159
|
+
export function findArrowBody(tokens, i) {
|
|
160
|
+
let depth = 0;
|
|
161
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + ARROW_SEARCH_TOKENS); j++) {
|
|
162
|
+
const tj = tokens[j];
|
|
163
|
+
if (tj.type !== 'punct') continue;
|
|
164
|
+
if (tj.value === '(' || tj.value === '[' || tj.value === '{') { depth++; continue; }
|
|
165
|
+
if (tj.value === ')' || tj.value === ']' || tj.value === '}') { depth--; if (depth < 0) return -1; continue; }
|
|
166
|
+
if (tj.value === ';' && depth === 0) return -1;
|
|
167
|
+
if (tj.value === '=>' && depth === 0) return findBody(tokens, j + 1);
|
|
168
|
+
}
|
|
169
|
+
return -1;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* '(' 配对完成后的 '{'(方法简写);无则 -1。
|
|
174
|
+
* 注意 matchBrace 只配对**花括号**,不能拿它配圆括号——否则会从 '(' 位置
|
|
175
|
+
* 一路扫到方法体的 '{...}' 并返回方法体本身,`after` 落到体后的 '}' 上,
|
|
176
|
+
* 方法简写永远识别不出来(实测对象方法漏判的根因)。
|
|
177
|
+
*/
|
|
178
|
+
export function findBodyAfterParen(tokens, parenIdx) {
|
|
179
|
+
const close = matchParen(tokens, parenIdx);
|
|
180
|
+
if (close === -1) return -1;
|
|
181
|
+
const after = tokens[close + 1];
|
|
182
|
+
return after && after.type === 'punct' && after.value === '{' ? close + 1 : -1;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 圆括号配对:返回右括号下标;不匹配返回 -1。 */
|
|
186
|
+
export function matchParen(tokens, parenIdx) {
|
|
187
|
+
let depth = 0;
|
|
188
|
+
for (let i = parenIdx; i < tokens.length; i++) {
|
|
189
|
+
const t = tokens[i];
|
|
190
|
+
if (t.type !== 'punct') continue;
|
|
191
|
+
if (t.value === '(') depth++;
|
|
192
|
+
else if (t.value === ')') { depth--; if (depth === 0) return i; }
|
|
193
|
+
}
|
|
194
|
+
return -1;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 判断 token 是否为 async 函数头,并返回函数体范围。
|
|
199
|
+
* 只收 async 函数:同步函数体不参与 inAsync 判定(用它反而是误报来源)。
|
|
200
|
+
* @returns {[number, number]|null} [起行, 止行]
|
|
201
|
+
*/
|
|
202
|
+
export function classifyFnHead(tokens, i, t) {
|
|
203
|
+
if (t.value !== 'async') return null;
|
|
204
|
+
const open = findAsyncBody(tokens, i);
|
|
205
|
+
if (open === -1) return null;
|
|
206
|
+
const range = matchBrace(tokens, open);
|
|
207
|
+
return range ? [tokens[open].line, tokens[range[1]].line] : null;
|
|
208
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-git-push — I/O 风险分级:循环判定
|
|
3
|
+
*
|
|
4
|
+
* 职责:判断某处 I/O 是否在循环体内、以及该循环执行次数能否静态确定为很少
|
|
5
|
+
* (小字面量数组 → 判定降级)。这是分级里最易出错的一块:
|
|
6
|
+
* 「迭代器表达式内的 I/O 只执行一次」与「数组方法回调体内的 I/O 逐元素执行」
|
|
7
|
+
* 形态相邻而语义相反,判据与反例都记在各函数注释里。
|
|
8
|
+
*
|
|
9
|
+
* 分层:lib/ast/io-risk.js 的从属模块(由该文件再导出),调用方不应直接 import。
|
|
10
|
+
*/
|
|
11
|
+
import { LOOP_KEYWORDS, LOOP_METHODS, isLoopHead } from './io-risk-const.js';
|
|
12
|
+
import {
|
|
13
|
+
SMALL_FIXED_LOOP_MAX, LOOP_BODY_SEARCH_TOKENS, findLoopRange, matchParen, endLineOf, seekLoopBody,
|
|
14
|
+
isSingleStmtLoopClose, classifyFnHead, classifySyncFnHead,
|
|
15
|
+
} from './io-risk-fn.js';
|
|
16
|
+
import { matchPairBack } from './brace.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 数组方法调用(`.some(` / `.map(` 等)的接收者是否为固定小字面量数组。
|
|
20
|
+
* 形如 `['a','b','c'].some(...)`:向前找到匹配的 `]`,其配对 `[` 即数组字面量,
|
|
21
|
+
* 元素数 ≤ SMALL_FIXED_LOOP_MAX 才算固定小集合。
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
*/
|
|
24
|
+
export function isSmallLiteralCallee(tokens, methodIdx) {
|
|
25
|
+
// methodIdx 指向方法名,其前应为 `.`,再前应为 `]`
|
|
26
|
+
if (tokens[methodIdx - 1]?.value !== '.') return false;
|
|
27
|
+
const close = methodIdx - 2;
|
|
28
|
+
if (tokens[close]?.value !== ']') return false;
|
|
29
|
+
const open = matchPairBack(tokens, close, '[', ']');
|
|
30
|
+
return open !== -1 && countLiteralElements(tokens, open) <= SMALL_FIXED_LOOP_MAX;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**:字面量数组起手 + 仅少量 push 追加。
|
|
34
|
+
*
|
|
35
|
+
* 识别 `const candidates = ['a']; if (x) candidates.push(b); for (const f of candidates)`
|
|
36
|
+
* 这类**候选路径探测**——它规模确定(≤ SMALL_FIXED_LOOP_MAX)、执行次数极少,
|
|
37
|
+
* 与字面量数组同义;不识别会导致「循环内 I/O 高风险」误报(实测 config.js /
|
|
38
|
+
* readme-gen / credentials.js 共 8 处候选探测全被误判)。
|
|
39
|
+
*
|
|
40
|
+
* 保守策略:只要出现字面量数组**之外**的写入方式(concat/splice/展开赋值等),
|
|
41
|
+
* 即视为规模未知,返回 false(宁可保守报高风险,也不放过真问题)。
|
|
42
|
+
* @param {Array} tokens tokenize 结果
|
|
43
|
+
* @param {string} name 数组变量名
|
|
44
|
+
* @param {number} ofIdx 循环头 'of'/'in' 的位置(只回看它之前)
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
export function isSmallPushedArray(tokens, name, ofIdx) {
|
|
48
|
+
// ① 声明处必须是字面量数组:`const name = [` / `let name = [`
|
|
49
|
+
const declIdx = findLiteralArrayDecl(tokens, name, ofIdx);
|
|
50
|
+
if (declIdx === -1) return false;
|
|
51
|
+
|
|
52
|
+
// ② 统计其后的 push 次数;出现任何让规模不可静态确定的写法即判未知
|
|
53
|
+
const pushes = countUnsafePushes(tokens, name, ofIdx);
|
|
54
|
+
if (pushes === null) return false;
|
|
55
|
+
|
|
56
|
+
// ③ 字面量区元素数 + push 次数 ≤ 上限
|
|
57
|
+
return countLiteralElements(tokens, declIdx) + pushes <= SMALL_FIXED_LOOP_MAX;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 回看 `name = [` 形态的声明位置;未以字面量数组起手返回 -1。
|
|
62
|
+
* 取**最后一次**匹配(同名变量被重新赋值时,以离循环最近的一次为准)。
|
|
63
|
+
* @returns {number} 数组 `[` 的 token 下标;未找到为 -1
|
|
64
|
+
*/
|
|
65
|
+
export function findLiteralArrayDecl(tokens, name, ofIdx) {
|
|
66
|
+
let declIdx = -1;
|
|
67
|
+
for (let j = 0; j < ofIdx; j++) {
|
|
68
|
+
if (tokens[j].type !== 'ident' || tokens[j].value !== name) continue;
|
|
69
|
+
const eq = tokens[j + 1];
|
|
70
|
+
const open = tokens[j + 2];
|
|
71
|
+
if (eq?.value === '=' && open?.value === '[') declIdx = j + 2;
|
|
72
|
+
}
|
|
73
|
+
return declIdx;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 让数组规模不可静态确定的写法(合并/改写类方法)。 */
|
|
77
|
+
export const UNSAFE_ARRAY_METHODS = new Set([
|
|
78
|
+
'concat', 'splice', 'unshift', 'apply', 'flat', 'flatMap',
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 数「安全 push」的次数。
|
|
83
|
+
* 出现以下任一即返回 null(表示规模未知,调用方按高风险处理):
|
|
84
|
+
* · 展开运算符 `...`
|
|
85
|
+
* · 合并/改写类方法(UNSAFE_ARRAY_METHODS)
|
|
86
|
+
* · push 发生在循环体内(元素来自被遍历集合,次数不可静态确定)
|
|
87
|
+
* · push 次数已超上限(早退)
|
|
88
|
+
* @returns {number|null} push 次数;规模未知为 null
|
|
89
|
+
*/
|
|
90
|
+
export function countUnsafePushes(tokens, name, ofIdx) {
|
|
91
|
+
let pushes = 0;
|
|
92
|
+
for (let j = 0; j < ofIdx; j++) {
|
|
93
|
+
const t = tokens[j];
|
|
94
|
+
// 展开运算符 `[...other]` 是 punct,会让规模不可静态确定
|
|
95
|
+
if (t.type === 'punct' && t.value === '...') return null;
|
|
96
|
+
if (t.type !== 'ident') continue;
|
|
97
|
+
// 合并/改写类方法:仅当是 **name.method** 形态才算(此前不区分方法归属,
|
|
98
|
+
// 扫描 0..ofIdx 会把声明前无关代码里的 concat/splice 误判为本数组的改写,
|
|
99
|
+
// 导致「声明后小数组循环」被错误判为规模未知 → 循环内 I/O 误报中风险)。
|
|
100
|
+
if (UNSAFE_ARRAY_METHODS.has(t.value)) {
|
|
101
|
+
if (tokens[j - 1]?.value === '.' && tokens[j - 2]?.value === name) return null;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (t.value !== 'push') continue;
|
|
105
|
+
// 形态:name . push (
|
|
106
|
+
if (tokens[j - 1]?.value !== '.' || tokens[j - 2]?.value !== name) continue;
|
|
107
|
+
// push 若发生在循环体内(`for (const x of all) c.push(x)`),元素来自循环变量,
|
|
108
|
+
// 规模取决于被遍历的集合 —— 不可静态确定。
|
|
109
|
+
// 注意从 **push 本身** 的位置起扫:若从参数位置起扫,push 自己的 '(' 会让
|
|
110
|
+
// 深度立刻变负而被误判为「不在循环内」。
|
|
111
|
+
if (insideLoopOver(tokens, j)) return null;
|
|
112
|
+
pushes++;
|
|
113
|
+
if (pushes + 1 > SMALL_FIXED_LOOP_MAX) return null;
|
|
114
|
+
}
|
|
115
|
+
return pushes;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 循环头 `(...)` 的 token 下标范围 [openIdx, closeIdx];无括号返回 null。
|
|
120
|
+
*/
|
|
121
|
+
export function loopHeadSpan(tokens, i) {
|
|
122
|
+
// 只对关键字循环(for/while)剔除头部表达式:
|
|
123
|
+
// 数组方法(`.forEach(cb)` / `.find(cb)`)的括号里是**回调体**,其中的 I/O
|
|
124
|
+
// 会随每个元素执行,正是「循环内 I/O」,绝不能剔除(实测误判为 low)。
|
|
125
|
+
if (!LOOP_KEYWORDS.has(tokens[i].value)) return null;
|
|
126
|
+
let open = -1;
|
|
127
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + LOOP_BODY_SEARCH_TOKENS); j++) {
|
|
128
|
+
const t = tokens[j];
|
|
129
|
+
if (t.type !== 'punct') continue;
|
|
130
|
+
if (t.value === '(') { open = j; break; }
|
|
131
|
+
if (t.value === '{' || t.value === ';') return null;
|
|
132
|
+
}
|
|
133
|
+
if (open === -1) return null;
|
|
134
|
+
const close = matchParenIn(tokens, open);
|
|
135
|
+
return close === -1 ? null : [open, close];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 前向圆括号配对:返回右括号下标;不匹配返回 -1。 */
|
|
139
|
+
function matchParenIn(tokens, openIdx) {
|
|
140
|
+
let depth = 0;
|
|
141
|
+
for (let j = openIdx; j < tokens.length; j++) {
|
|
142
|
+
const t = tokens[j];
|
|
143
|
+
if (t.type !== 'punct') continue;
|
|
144
|
+
if (t.value === '(') depth++;
|
|
145
|
+
else if (t.value === ')') { depth--; if (depth === 0) return j; }
|
|
146
|
+
}
|
|
147
|
+
return -1;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 位置 idx 的 push 是否发生在某个循环体内。
|
|
152
|
+
*
|
|
153
|
+
* 用于识别「从大集合填充」:`for (const x of all) c.push(x)` 静态看只 push 一次,
|
|
154
|
+
* 但实际执行次数 = all 的长度 —— 规模不可静态确定,必须按未知处理。
|
|
155
|
+
*
|
|
156
|
+
* 朴素但够用的判据:向前扫到最近的 `{`,若该 `{` 之前紧跟 `)` 且括号头是
|
|
157
|
+
* for/while/do,或直接是 `do {`,即认为在循环体内。
|
|
158
|
+
* @returns {boolean}
|
|
159
|
+
*/
|
|
160
|
+
export function insideLoopOver(tokens, idx) {
|
|
161
|
+
// 向左扫,遇到 `for`/`while` 关键字即认为在循环体内;
|
|
162
|
+
// 途中若先遇到语句边界(`;`)或块边界(`}`),说明已跨出当前语句,放弃。
|
|
163
|
+
// 不区分花括号体与单语句体 —— 两种都算。
|
|
164
|
+
let depth = 0;
|
|
165
|
+
for (let j = idx - 1; j >= 0; j--) {
|
|
166
|
+
const t = tokens[j];
|
|
167
|
+
if (t.type !== 'punct') {
|
|
168
|
+
// 未闭合的深度里遇到循环关键字:说明正处在它的头部/体内
|
|
169
|
+
if (t.type === 'ident' && LOOP_KEYWORDS.has(t.value)) return true;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (t.value === ')' || t.value === ']' || t.value === '}') { depth++; continue; }
|
|
173
|
+
if (t.value === '(' || t.value === '[') { depth--; if (depth < 0) return false; continue; }
|
|
174
|
+
if (t.value === '{') {
|
|
175
|
+
if (depth > 0) { depth--; continue; }
|
|
176
|
+
return isLoopBodyBrace(tokens, j);
|
|
177
|
+
}
|
|
178
|
+
if (t.value === ';' && depth === 0) return false;
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 闭合类括号:向右扫时用于累计深度。 */
|
|
184
|
+
|
|
185
|
+
/** 开启类括号:向左扫时用于消解深度。 */
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 该 `{` 是否开启一个循环体。
|
|
189
|
+
* 块的**开**括号可能是循环体(`for (...) {`)—— 回看它前面是不是循环头;
|
|
190
|
+
* 不是则说明已跨出到别的作用域。
|
|
191
|
+
* @returns {boolean}
|
|
192
|
+
*/
|
|
193
|
+
export function isLoopBodyBrace(tokens, braceIdx) {
|
|
194
|
+
const prev = tokens[braceIdx - 1];
|
|
195
|
+
if (prev?.value === 'do') return true; // do { ... }
|
|
196
|
+
if (prev?.value !== ')') return false;
|
|
197
|
+
const paren = matchPairBack(tokens, braceIdx - 1, '(', ')');
|
|
198
|
+
if (paren === -1) return false;
|
|
199
|
+
const kw = tokens[paren - 1]?.value;
|
|
200
|
+
return kw === 'for' || kw === 'while';
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** 数 `[ ... ]` 字面量数组的元素个数(顶层逗号 + 1;空数组为 0)。 */
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 形如 `for (const x of ['a', 'b', 'c'])`:元素数固定且 ≤ SMALL_FIXED_LOOP_MAX。
|
|
207
|
+
* 只认字面量数组字面形态;变量/函数调用一律视为规模未知(保守判高风险)。
|
|
208
|
+
* @returns {boolean}
|
|
209
|
+
*/
|
|
210
|
+
export function isSmallFixedLoop(tokens, i) {
|
|
211
|
+
// ① 数组方法形式(`['a','b'].some((n) => ...)`):受调用的数组若是字面量,
|
|
212
|
+
// 规模同样固定且极小。这一形态不走 for...of,早先完全未覆盖。
|
|
213
|
+
if (LOOP_METHODS.has(tokens[i].value) && isSmallLiteralCallee(tokens, i)) return true;
|
|
214
|
+
|
|
215
|
+
// ② for...of / for...in 形态:看被遍历的表达式
|
|
216
|
+
const ofIdx = findOfKeyword(tokens, i);
|
|
217
|
+
if (ofIdx === -1) return false;
|
|
218
|
+
|
|
219
|
+
const open = tokens[ofIdx + 1];
|
|
220
|
+
if (!open) return false;
|
|
221
|
+
|
|
222
|
+
// 变量数组(`for (const f of candidates)`):回看它的声明,若以字面量数组起手
|
|
223
|
+
// 且仅用少量 push 追加,则规模固定且极小 —— 与字面量数组同义,按小集合处理。
|
|
224
|
+
if (open.value !== '[') {
|
|
225
|
+
return open.type === 'ident' && isSmallPushedArray(tokens, open.value, ofIdx);
|
|
226
|
+
}
|
|
227
|
+
// ③ 字面量数组形态:数顶层元素个数
|
|
228
|
+
return countLiteralElements(tokens, ofIdx + 1) <= SMALL_FIXED_LOOP_MAX;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 在循环头内定位 `of` / `in` 关键字;找不到返回 -1。
|
|
233
|
+
* 扫到 `{` 或 `;` 即停 —— 说明已越过循环头。
|
|
234
|
+
* @returns {number} 关键字 token 下标
|
|
235
|
+
*/
|
|
236
|
+
export function findOfKeyword(tokens, i) {
|
|
237
|
+
const limit = Math.min(tokens.length, i + LOOP_BODY_SEARCH_TOKENS);
|
|
238
|
+
for (let j = i + 1; j < limit; j++) {
|
|
239
|
+
const tj = tokens[j];
|
|
240
|
+
if (tj.type === 'ident' && (tj.value === 'of' || tj.value === 'in')) return j;
|
|
241
|
+
if (tj.type === 'punct' && (tj.value === '{' || tj.value === ';')) break;
|
|
242
|
+
}
|
|
243
|
+
return -1;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 从 `[` 起数顶层元素个数,判断是否为「固定小集合」(存在元素且 ≤ 上限)。
|
|
248
|
+
*
|
|
249
|
+
* 计数规则:深度回到 0 时遇 `]` 收尾;深度 1 上的逗号即分隔符。
|
|
250
|
+
* 元素数超上限时早退,避免长数组上白扫。
|
|
251
|
+
* @param {number} fromIdx 数组 `[` 的下标
|
|
252
|
+
* @returns {boolean}
|
|
253
|
+
*/
|
|
254
|
+
export function countLiteralElements(tokens, fromIdx) {
|
|
255
|
+
let depth = 0;
|
|
256
|
+
let count = 0;
|
|
257
|
+
let hasElement = false;
|
|
258
|
+
for (let j = fromIdx; j < tokens.length; j++) {
|
|
259
|
+
const tj = tokens[j];
|
|
260
|
+
if (tj.type !== 'punct') { hasElement = true; continue; }
|
|
261
|
+
if (tj.value === '[' || tj.value === '(' || tj.value === '{') { depth++; continue; }
|
|
262
|
+
if (tj.value === ']') {
|
|
263
|
+
depth--;
|
|
264
|
+
if (depth === 0) return hasElement ? count + 1 : 0;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (tj.value === ')' || tj.value === '}') { depth--; continue; }
|
|
268
|
+
if (tj.value === ',' && depth === 1) { count++; hasElement = true; }
|
|
269
|
+
if (count + 1 > SMALL_FIXED_LOOP_MAX) return count + 1; // 超上限早退(已知大于上限)
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 收集所有函数体范围(含 async 标记)与循环体范围,用于上下文归属判断。
|
|
276
|
+
*
|
|
277
|
+
* 刻意不跳过函数体:函数内还嵌着循环与内层函数,跳过会漏收内层范围。
|
|
278
|
+
* @param {Array} tokens tokenize 结果
|
|
279
|
+
* @returns {{asyncFns: Array, loops: Array}}
|
|
280
|
+
*/
|
|
281
|
+
export function collectRanges(tokens) {
|
|
282
|
+
const asyncFns = [];
|
|
283
|
+
const allFns = [];
|
|
284
|
+
const loops = [];
|
|
285
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
286
|
+
const t = tokens[i];
|
|
287
|
+
if (t.type !== 'ident') continue;
|
|
288
|
+
const fnRange = classifyFnHead(tokens, i, t);
|
|
289
|
+
if (fnRange) { asyncFns.push(fnRange); allFns.push(fnRange); continue; }
|
|
290
|
+
// 同步函数/箭头函数/对象方法简写:不参与 inAsync,但要参与「请求路径」判定
|
|
291
|
+
// (否则同步 HTTP handler 里的 I/O 被误判成「启动路径一次性」而低估风险)。
|
|
292
|
+
const syncRange = classifySyncFnHead(tokens, i, t);
|
|
293
|
+
if (syncRange) { allFns.push(syncRange); continue; }
|
|
294
|
+
if (isLoopHead(t, tokens[i - 1])) {
|
|
295
|
+
const range = findLoopRange(tokens, i);
|
|
296
|
+
// 固定小字面量数组(`for (const n of ['a','b','c'])`)迭代次数确定且极少,
|
|
297
|
+
// 循环内同步 I/O 的开销 = 元素数个系统调用,不构成「逐次阻塞放大耗时」——
|
|
298
|
+
// 标出来供判定层降级(实测:3 个 SSH key 名的探测循环被误判为高风险)。
|
|
299
|
+
if (range) {
|
|
300
|
+
// 第 4 项 = 循环头 `(...)` 的 token 下标范围,用于剔除「迭代器表达式内的 I/O」:
|
|
301
|
+
// `for (const line of readFileSync(f).split('\n'))` 里 readFileSync 只在
|
|
302
|
+
// 进入循环前执行一次,把它算作循环内 I/O 是误报(实测 repos.js:81)。
|
|
303
|
+
loops.push([range[0], range[1], isSmallFixedLoop(tokens, i), loopHeadSpan(tokens, i)]);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return { asyncFns, allFns, loops };
|
|
308
|
+
}
|