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,800 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""module-splitter —— 把巨型单文件按职责切成多模块 + 纯引用 index(零依赖,只用 python3 标准库)。
|
|
3
|
+
|
|
4
|
+
用途:一个 .js 文件涨到几百上千行(路由/实现/调度/工具全塞在一起)时,
|
|
5
|
+
按「顶层块」机械切到 <outdir>/ 的各模块,并生成只做再导出的 index.js,
|
|
6
|
+
使调用方导入路径不变、对外导出面不变。
|
|
7
|
+
|
|
8
|
+
三个子命令:
|
|
9
|
+
python3 module-splitter.py analyze <file.js>
|
|
10
|
+
只读分析:列出顶层块(行号/行数)、块间依赖图、模块循环依赖风险、顶部 import。
|
|
11
|
+
先跑这个,再据此写 plan.json —— 不要凭空猜哪块依赖哪块。
|
|
12
|
+
|
|
13
|
+
python3 module-splitter.py split <plan.json> [--dry-run]
|
|
14
|
+
按 plan 执行切分。--dry-run 只报告将要写入什么,不落盘。
|
|
15
|
+
|
|
16
|
+
python3 module-splitter.py verify <plan.json>
|
|
17
|
+
静态校验:index 再导出的名字集合是否与原文件「本来就 export」的名字集合完全一致
|
|
18
|
+
(拆完必跑;少一个名字就是调用方会 undefined 的隐性破坏)。
|
|
19
|
+
|
|
20
|
+
plan.json 结构(示例见本文件末尾「PLAN 示例」):
|
|
21
|
+
{
|
|
22
|
+
"file": "lib/git/index.js", # 待拆文件
|
|
23
|
+
"outdir": "lib/git", # 输出目录
|
|
24
|
+
"index": "index.js", # 出口文件名
|
|
25
|
+
"index_header": "/** 统一出口 ... */", # 出口文件头注释
|
|
26
|
+
"plan": { "exec.js": ["runGit", "gitRaw"], ... }, # 模块 → 顶层块名清单
|
|
27
|
+
"headers": { "exec.js": "/** ... */", ... }, # 各模块头注释
|
|
28
|
+
"external":{ "exec.js": ["import x from './y.js';"], ... } # 需额外注入的 import
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
设计要点(都是实战踩出来的,勿随意简化):
|
|
32
|
+
1. 块的起始行**向上吞掉紧邻的注释/空行**——否则文档注释留在旧文件里丢失。
|
|
33
|
+
2. 原有 import 按**符号**分发:解析原文件每条 import 的符号,哪个模块用到就带上。
|
|
34
|
+
不靠猜 builtin 清单(曾因靠猜导致 makeCodeLineFilter / CODE_EXTS 漏 import)。
|
|
35
|
+
3. 符号引用检测前**先剥注释**:文档注释里常提到别的函数名,会造成假依赖甚至假循环
|
|
36
|
+
(实测 gitRaw 注释提到 pushViaApi → 误报 exec.js ↔ transport.js 循环)。
|
|
37
|
+
4. 跨模块引用的**内部**符号自动补 export,但**不进 index 再导出**——
|
|
38
|
+
保证对外公开面与拆分前完全一致。
|
|
39
|
+
5. index 只再导出「原文件本来就 export 的符号」,顺序保持原样。
|
|
40
|
+
6. 模块间引用做**环检测**,发现环直接报错(ESM 循环会让符号静默变 undefined,极难查)。
|
|
41
|
+
7. 全部块必须被分配,漏一个就报错——防止「拆完发现少了个函数」这种静默丢失。
|
|
42
|
+
|
|
43
|
+
PLAN 示例:
|
|
44
|
+
{
|
|
45
|
+
"file": "lib/git/index.js",
|
|
46
|
+
"outdir": "lib/git",
|
|
47
|
+
"index": "index.js",
|
|
48
|
+
"index_header": "/**\\n * Git 操作层 · 统一出口(只做再导出)\\n */",
|
|
49
|
+
"plan": {
|
|
50
|
+
"exec.js": ["runGit", "gitRaw"],
|
|
51
|
+
"credentials.js": ["credentialsDir", "resolveToken"],
|
|
52
|
+
"push.js": ["commitAndPush"]
|
|
53
|
+
},
|
|
54
|
+
"headers": {
|
|
55
|
+
"exec.js": "/**\\n * Git 执行层 · 进程调用\\n */"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
"""
|
|
59
|
+
import json
|
|
60
|
+
import os
|
|
61
|
+
import re
|
|
62
|
+
import subprocess
|
|
63
|
+
import sys
|
|
64
|
+
from pathlib import Path
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# ───────────────────────── 解析 ─────────────────────────
|
|
68
|
+
|
|
69
|
+
def strip_comments(code: str) -> str:
|
|
70
|
+
"""去掉 // 行注释与 /* */ 块注释,仅供「符号引用检测」使用。
|
|
71
|
+
|
|
72
|
+
为什么必须剥:文档注释里常在散文里提到别的函数名(实测 gitRaw 的注释提到
|
|
73
|
+
pushViaApi → 被误判为 exec.js 依赖 transport.js,进而报出并不存在的模块循环)。
|
|
74
|
+
检测用剥注释文本,产出仍用原文。
|
|
75
|
+
"""
|
|
76
|
+
out = []
|
|
77
|
+
i, n = 0, len(code)
|
|
78
|
+
while i < n:
|
|
79
|
+
two = code[i:i + 2]
|
|
80
|
+
if two == '//':
|
|
81
|
+
j = code.find('\n', i)
|
|
82
|
+
i = n if j < 0 else j
|
|
83
|
+
continue
|
|
84
|
+
if two == '/*':
|
|
85
|
+
j = code.find('*/', i + 2)
|
|
86
|
+
i = n if j < 0 else j + 2
|
|
87
|
+
continue
|
|
88
|
+
out.append(code[i])
|
|
89
|
+
i += 1
|
|
90
|
+
return ''.join(out)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
PUNCT_ALLOWS_REGEX = ('', '(', ',', '=', ':', '[', '!', '&', '|', '?', '{', '}',
|
|
94
|
+
';', '+', '-', '*', '%', '<', '>', '~', '^')
|
|
95
|
+
# 这些关键字后面出现的 `/` 一定是正则字面量(它们都期待一个表达式)
|
|
96
|
+
REGEX_KEYWORDS = frozenset((
|
|
97
|
+
'return', 'typeof', 'case', 'in', 'of', 'new', 'delete', 'void',
|
|
98
|
+
'instanceof', 'do', 'else', 'yield', 'await', 'throw',
|
|
99
|
+
))
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def regex_allowed(prev_sig: str, prev_word: str) -> bool:
|
|
103
|
+
"""判断当前位置的 `/` 是否为正则字面量起始(而非除号)。
|
|
104
|
+
|
|
105
|
+
为什么需要:正则常含**不成对**的括号/方括号(如 `/[/\-._A-Za-z0-9]/` 里的 `[`、`]`)。
|
|
106
|
+
若不识别正则字面量,深度计数会被算乱,后续所有顶层语句边界全错——实测
|
|
107
|
+
lib/audit/index.js 的 isVersionInPathContext 因此把后面 5 个 export function
|
|
108
|
+
全吞掉(auditFile/auditFull/auditChanged/auditWithScope 一个都识别不出来)。
|
|
109
|
+
|
|
110
|
+
判定必须**看词法单元而非单个字符**:`return /re/` 与 `a / b` 里 `/` 的前一个字符
|
|
111
|
+
都是字母,只按字符判断会把 `return` 后面的正则误判成除号——误判后 `[` 加深度、
|
|
112
|
+
紧随的 `/` 又被当成正则起始,把配对的 `]` 一起吞掉,深度从此永久残留。
|
|
113
|
+
故:前一字符是标点(不能作为表达式结尾)→ 正则;或前一个词是
|
|
114
|
+
return/typeof/case 等期待表达式的关键字 → 正则。
|
|
115
|
+
"""
|
|
116
|
+
if prev_sig in PUNCT_ALLOWS_REGEX:
|
|
117
|
+
return True
|
|
118
|
+
return prev_word in REGEX_KEYWORDS
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def parse_export_clauses(src: str):
|
|
122
|
+
"""收集顶层 `export { ... };` / `export { ... } from '...';` 子句。
|
|
123
|
+
|
|
124
|
+
为什么必须收集:公开面不止「function/const 声明」——文件末尾常有一句
|
|
125
|
+
`export { helperA, helperB };` 把别处 import 进来的符号再导出。只按声明统计导出
|
|
126
|
+
会漏掉它们,切分后出口少这一句 → **公开面静默缩小**(实测 lib/index.js 的
|
|
127
|
+
`export { routeRequest, readJsonBody };` 就是这种)。
|
|
128
|
+
返回 [{names: [...], from: 路径或 None, raw: 原语句}]。
|
|
129
|
+
"""
|
|
130
|
+
out = []
|
|
131
|
+
for m in re.finditer(r"^export\s*\{([^}]*)\}\s*(?:from\s*(['\"])([^'\"]+)\2\s*)?;", src, re.M):
|
|
132
|
+
names = [x.strip() for x in m.group(1).split(',') if x.strip()]
|
|
133
|
+
out.append({'names': names, 'from': m.group(3), 'raw': m.group(0)})
|
|
134
|
+
return out
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def prune_import_for(syms, imports):
|
|
138
|
+
"""生成「只含指定符号」的 import 语句(按原语句分组后裁剪)。"""
|
|
139
|
+
need = []
|
|
140
|
+
seen = set()
|
|
141
|
+
for sym in syms:
|
|
142
|
+
stmt = imports.get(sym)
|
|
143
|
+
if stmt and stmt not in seen:
|
|
144
|
+
seen.add(stmt)
|
|
145
|
+
others = [x for x, st in imports.items() if st == stmt]
|
|
146
|
+
if all(x in syms for x in others):
|
|
147
|
+
need.append(stmt)
|
|
148
|
+
else:
|
|
149
|
+
need.append(re.sub(r'\{.*\}', '{ ' + ', '.join(x for x in others if x in syms) + ' }', stmt))
|
|
150
|
+
return need
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def rewrite_rel_imports(stmt: str, orig_dir: str, mod_dir: str) -> str:
|
|
154
|
+
"""把 import 里的相对路径按「模块新位置」重算(含裸副作用 import)。
|
|
155
|
+
|
|
156
|
+
为什么必须做:切分常把模块放进**子目录**(lib/rule/compilers.js →
|
|
157
|
+
lib/rule/compilers/regex.js)。原文件里的 `./registry.js` 指的是 lib/rule/registry.js,
|
|
158
|
+
照抄进子目录模块会解析成 lib/rule/compilers/registry.js → ERR_MODULE_NOT_FOUND。
|
|
159
|
+
做法:把原相对路径按「原文件所在目录」解析,再相对「新模块所在目录」重算。
|
|
160
|
+
绝对路径与裸包名(node:*、@scope/pkg)原样保留。
|
|
161
|
+
"""
|
|
162
|
+
m = re.match(r"^import\s+.+?\s+from\s+(['\"])(\.[^'\"]*)\1;", stmt) \
|
|
163
|
+
or re.match(r"^import\s+(['\"])(\.[^'\"]*)\1;", stmt)
|
|
164
|
+
if not m:
|
|
165
|
+
return stmt
|
|
166
|
+
src = m.group(2)
|
|
167
|
+
target = os.path.normpath(os.path.join(orig_dir, src))
|
|
168
|
+
rel = os.path.relpath(target, mod_dir)
|
|
169
|
+
if not rel.startswith('.'):
|
|
170
|
+
rel = './' + rel
|
|
171
|
+
return stmt[:m.start(2)] + rel + stmt[m.end(2):]
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def parse_side_effect_imports(src: str):
|
|
175
|
+
"""收集**裸副作用 import**:`import './x.js';`(无绑定符号)。
|
|
176
|
+
|
|
177
|
+
为什么必须单独收集:parse_imports 按「符号 → 语句」建表,裸 import 没有任何符号,
|
|
178
|
+
不会被任何模块「用到」→ 整条在切分时被静默丢弃。而这类 import 恰恰常是关键的
|
|
179
|
+
初始化开关——实测 lib/audit/index.js 的 `import '../rule/compilers.js';`
|
|
180
|
+
就是「注册全部规则编译器」的唯一触发点,丢掉后审计拿到空编译器表,
|
|
181
|
+
结果静默变化(不报错,只是检查项全不出)。
|
|
182
|
+
做法:这类语句统一保留到出口文件(入口 import 即完成初始化),并同步重算相对路径。
|
|
183
|
+
"""
|
|
184
|
+
return re.findall(r"^import\s+['\"][^'\"]+['\"];.*$", src, re.M)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def rewrite_dynamic_imports(text: str, orig_dir: str, mod_dir: str) -> str:
|
|
188
|
+
"""重写正文里的**动态** import 路径:`import('./x.js')` / `require('./x.js')`。
|
|
189
|
+
|
|
190
|
+
为什么单独处理:上面的 rewrite_rel_imports 只认顶层 import 语句,而代码里常有
|
|
191
|
+
函数体内的延迟 import(用来破循环依赖)。模块挪进子目录后这些路径同样失效——
|
|
192
|
+
实测 listRegisteredKinds 的 `import('./registry.js')` 未重写,加载时直接
|
|
193
|
+
ERR_MODULE_NOT_FOUND(找 lib/rule/compilers/registry.js)。
|
|
194
|
+
"""
|
|
195
|
+
def fix(m):
|
|
196
|
+
quote, src = m.group(1), m.group(2)
|
|
197
|
+
target = os.path.normpath(os.path.join(orig_dir, src))
|
|
198
|
+
rel = os.path.relpath(target, mod_dir)
|
|
199
|
+
if not rel.startswith('.'):
|
|
200
|
+
rel = './' + rel
|
|
201
|
+
return f'import({quote}{rel}{quote})'
|
|
202
|
+
|
|
203
|
+
return re.sub(r"import\(\s*(['\"])(\.[^'\"]*)\1\s*\)", fix, text)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def top_level_statements(src: str):
|
|
207
|
+
"""按**括号深度**切出顶层语句 → [(start, end)](0-based,end 不含)。
|
|
208
|
+
|
|
209
|
+
为什么不能用「下一个顶层声明」当块尾:那假定了文件里只有函数/常量声明。注册表类
|
|
210
|
+
文件里大量顶层**语句**(registerCompiler('kind', ...))不是声明,于是上一个声明的
|
|
211
|
+
块尾会一路吞到下一个声明为止——实测 numericKinds(数组声明,真实到 139 行)的块尾
|
|
212
|
+
被撑到 264 行,把后面 6 个 registerCompiler 语句全算成它的正文。
|
|
213
|
+
|
|
214
|
+
正确做法:维护 () [] {} 深度 + 块注释/字符串/正则字面量状态,深度为 0 时遇到新的
|
|
215
|
+
行首非空行即视为新语句开始。
|
|
216
|
+
"""
|
|
217
|
+
lines = src.split('\n')
|
|
218
|
+
starts = []
|
|
219
|
+
depth = 0
|
|
220
|
+
in_block_comment = False
|
|
221
|
+
prev_sig = '' # 上一个非空白字符(含字母)
|
|
222
|
+
prev_word = '' # 上一个完整的词法单元(标识符/关键字)
|
|
223
|
+
cur_word = '' # 正在累积的词法单元
|
|
224
|
+
for i, line in enumerate(lines):
|
|
225
|
+
if depth == 0 and not in_block_comment and line.strip() \
|
|
226
|
+
and re.match(r'^\S', line) and not re.match(r'^[)\]}.,;]', line):
|
|
227
|
+
starts.append(i)
|
|
228
|
+
j, n = 0, len(line)
|
|
229
|
+
while j < n:
|
|
230
|
+
two = line[j:j + 2]
|
|
231
|
+
if in_block_comment:
|
|
232
|
+
if two == '*/':
|
|
233
|
+
in_block_comment = False
|
|
234
|
+
j += 2
|
|
235
|
+
continue
|
|
236
|
+
j += 1
|
|
237
|
+
continue
|
|
238
|
+
if two == '/*':
|
|
239
|
+
in_block_comment = True
|
|
240
|
+
j += 2
|
|
241
|
+
continue
|
|
242
|
+
if two == '//':
|
|
243
|
+
break
|
|
244
|
+
ch = line[j]
|
|
245
|
+
if ch in '\'"`':
|
|
246
|
+
quote = ch
|
|
247
|
+
j += 1
|
|
248
|
+
while j < n:
|
|
249
|
+
if line[j] == '\\':
|
|
250
|
+
j += 2
|
|
251
|
+
continue
|
|
252
|
+
if line[j] == quote:
|
|
253
|
+
break
|
|
254
|
+
j += 1
|
|
255
|
+
j += 1
|
|
256
|
+
prev_word, cur_word = (cur_word or prev_word), ''
|
|
257
|
+
prev_sig = quote
|
|
258
|
+
continue
|
|
259
|
+
if ch == '/' and regex_allowed(prev_sig, cur_word or prev_word):
|
|
260
|
+
j += 1
|
|
261
|
+
in_class = False
|
|
262
|
+
while j < n:
|
|
263
|
+
c2 = line[j]
|
|
264
|
+
if c2 == '\\':
|
|
265
|
+
j += 2
|
|
266
|
+
continue
|
|
267
|
+
if c2 == '[':
|
|
268
|
+
in_class = True
|
|
269
|
+
elif c2 == ']':
|
|
270
|
+
in_class = False
|
|
271
|
+
elif c2 == '/' and not in_class:
|
|
272
|
+
break
|
|
273
|
+
j += 1
|
|
274
|
+
j += 1
|
|
275
|
+
prev_word, cur_word = (cur_word or prev_word), ''
|
|
276
|
+
prev_sig = '/'
|
|
277
|
+
continue
|
|
278
|
+
if ch.isalnum() or ch in '_$':
|
|
279
|
+
cur_word += ch
|
|
280
|
+
prev_sig = ch
|
|
281
|
+
else:
|
|
282
|
+
if cur_word:
|
|
283
|
+
prev_word, cur_word = cur_word, ''
|
|
284
|
+
if not ch.isspace():
|
|
285
|
+
prev_sig = ch
|
|
286
|
+
if ch in '([{':
|
|
287
|
+
depth += 1
|
|
288
|
+
elif ch in ')]}':
|
|
289
|
+
depth = max(0, depth - 1)
|
|
290
|
+
j += 1
|
|
291
|
+
out = []
|
|
292
|
+
for k, st in enumerate(starts):
|
|
293
|
+
en = starts[k + 1] if k + 1 < len(starts) else len(lines)
|
|
294
|
+
while en > st and not lines[en - 1].strip():
|
|
295
|
+
en -= 1
|
|
296
|
+
out.append((st, en))
|
|
297
|
+
return out
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def parse_blocks(src: str):
|
|
301
|
+
"""解析顶层块 → [{name, exported, start, end, body}],start 含其前置注释。
|
|
302
|
+
|
|
303
|
+
只有「命名声明」算块;顶层匿名语句(registerCompiler 调用等)不是块,需按行范围分配。
|
|
304
|
+
"""
|
|
305
|
+
lines = src.split('\n')
|
|
306
|
+
out = []
|
|
307
|
+
for st, en in top_level_statements(src):
|
|
308
|
+
m = re.match(r'^(export )?(?:async )?(function|const|let) (\w+)', lines[st])
|
|
309
|
+
if not m:
|
|
310
|
+
continue
|
|
311
|
+
start = st
|
|
312
|
+
while start > 0 and re.match(r'^\s*(//|/\*|\*|\*/|$)', lines[start - 1]):
|
|
313
|
+
start -= 1
|
|
314
|
+
out.append({'name': m.group(3), 'exported': bool(m.group(1)), 'start': start, 'end': en,
|
|
315
|
+
'body': '\n'.join(lines[start:en]).strip('\n')})
|
|
316
|
+
return out
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def parse_imports(src: str):
|
|
320
|
+
"""{符号: 原样 import 语句}——用于按符号把原 import 分发到各模块。
|
|
321
|
+
|
|
322
|
+
多行 import 支持:`import {\\n a,\\n b,\\n} from "./x.js";` 是常见写法,
|
|
323
|
+
原先用 `.+?`(`.` 不跨行)⇒ 这类 import **匹配不到**,其符号分发不到任何模块,
|
|
324
|
+
续行又被覆盖审计判为「未承载」而拦下正确的 plan(dsh-session-conductor 的
|
|
325
|
+
lib/index.js 有 8 个多行 import,实测 32 行被误判)。改用 [\\s\\S] 跨行匹配。
|
|
326
|
+
"""
|
|
327
|
+
sym2stmt = {}
|
|
328
|
+
for m in re.finditer(r'^import\s+([\s\S]+?)\s+from\s+[\'"]([^\'"]+)[\'"];', src, re.M):
|
|
329
|
+
clause, stmt = m.group(1), m.group(0)
|
|
330
|
+
# `.*` 不跨行 ⇒ 多行 import 的符号列表取不到(clause 形如 "{\n a,\n b,\n}"),
|
|
331
|
+
# 会退化成只取第一段、把符号全丢(实测 dsh-session-conductor:81 个符号里
|
|
332
|
+
# analyzeSession / cancelSessionTimers / patchSwitch 等全缺,导致 index 里
|
|
333
|
+
# 裸再导出 `export { x };` 因绑定不存在直接 SyntaxError)。改用 [\s\S]。
|
|
334
|
+
inner = re.search(r'\{([\s\S]*)\}', clause)
|
|
335
|
+
if inner:
|
|
336
|
+
for sym in inner.group(1).split(','):
|
|
337
|
+
sym = sym.strip()
|
|
338
|
+
if sym:
|
|
339
|
+
sym2stmt[sym.split(' as ')[-1].strip()] = stmt
|
|
340
|
+
else:
|
|
341
|
+
sym2stmt[clause.split(',')[0].strip()] = stmt
|
|
342
|
+
return sym2stmt
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def collect_ownership(plan, blocks, ranges_of, lines):
|
|
346
|
+
"""符号 → 模块 的归属表,额外纳入「行范围内的顶层声明」。
|
|
347
|
+
|
|
348
|
+
为什么需要:按行范围切的模块(注册表类文件的语句段)里也可能新增顶层声明,
|
|
349
|
+
若只按命名块归属,别的模块引用到它就检测不出依赖 → 漏 import(运行期
|
|
350
|
+
ReferenceError)。故把范围内声明的符号也登记进来。
|
|
351
|
+
"""
|
|
352
|
+
owner = {}
|
|
353
|
+
for mod, value in plan.items():
|
|
354
|
+
names, ranges = split_entry(value)
|
|
355
|
+
for n in names:
|
|
356
|
+
owner[n] = mod
|
|
357
|
+
for a, z in ranges:
|
|
358
|
+
chunk = '\n'.join(lines[a - 1:z])
|
|
359
|
+
for m in re.finditer(r'^(?:export\s+)?(?:async\s+)?(?:function|const|let)\s+(\w+)', chunk, re.M):
|
|
360
|
+
owner.setdefault(m.group(1), mod)
|
|
361
|
+
return owner
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def audit_coverage(plan, blocks, ranges_of, lines, source_lines):
|
|
365
|
+
"""行级覆盖审计:任何一行原文都必须被**恰好一个**模块承载。
|
|
366
|
+
|
|
367
|
+
为什么必须做行级(而不是只查「块是否分配」):块级检查存在漏洞——块被按名字
|
|
368
|
+
分配后,其正文又因行范围被截断,剩余部分就无人承载而**静默丢失**。
|
|
369
|
+
实测 numericKinds(126-264)按名分配、又被范围 142-160 截断,
|
|
370
|
+
导致 161-264 整段数组内容蒸发,而块级检查完全看不出来。
|
|
371
|
+
|
|
372
|
+
返回 (未覆盖行号列表, 重复覆盖行号列表);重复覆盖视为硬错误(同一段代码进了
|
|
373
|
+
两个模块),未覆盖仅剔除空行/纯注释/import 后报告。
|
|
374
|
+
"""
|
|
375
|
+
count = {}
|
|
376
|
+
for mod, value in plan.items():
|
|
377
|
+
names, ranges = split_entry(value)
|
|
378
|
+
spans = [(blocks[n]['start'], blocks[n]['end']) for n in names]
|
|
379
|
+
spans += [(a - 1, z) for a, z in ranges]
|
|
380
|
+
for a, z in spans:
|
|
381
|
+
for i in range(a, z):
|
|
382
|
+
count[i] = count.get(i, 0) + 1
|
|
383
|
+
dup = sorted(i + 1 for i, c in count.items() if c > 1)
|
|
384
|
+
# 「被出口文件保留」的语句不算丢失:裸副作用 import 与 export 子句都会原样
|
|
385
|
+
# 写进 index(见 parse_side_effect_imports / parse_export_clauses),
|
|
386
|
+
# 故审计里视为已覆盖,否则会误报「未承载」而拦下正确的 plan。
|
|
387
|
+
trivial = re.compile(r'^\s*(//|/\*|\*|\*/|import\s|export\s*\{|$)')
|
|
388
|
+
# 多行 import / export 子句的**续行**也要算已覆盖:整条语句由 index 原样保留
|
|
389
|
+
# (见 parse_imports 的 [\s\S] 跨行匹配与 parse_export_clauses 的 [^}]*),
|
|
390
|
+
# 但续行本身不以 import/export 开头 ⇒ 单靠 trivial 正则会误报「未承载」而拦下正确的
|
|
391
|
+
# plan(实测 dsh-session-conductor:8 个多行 import + 3 个多行 export 子句共 41 行)。
|
|
392
|
+
kept_lines = set()
|
|
393
|
+
src_text = '\n'.join(source_lines)
|
|
394
|
+
for pat in (r'^import\s+[\s\S]*?\s+from\s+[\'"][^\'"]+[\'"];',
|
|
395
|
+
r"^export\s*\{[^}]*\}\s*(?:from\s*(['\"])[^'\"]+\1\s*)?;"):
|
|
396
|
+
for m in re.finditer(pat, src_text, re.M):
|
|
397
|
+
start = src_text.count('\n', 0, m.start()) + 1
|
|
398
|
+
end = src_text.count('\n', 0, m.end()) + 1
|
|
399
|
+
kept_lines.update(range(start, end + 1))
|
|
400
|
+
missing = [i + 1 for i in range(len(source_lines))
|
|
401
|
+
if i not in count and not trivial.match(source_lines[i]) and (i + 1) not in kept_lines]
|
|
402
|
+
return missing, dup
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def module_parts(plan, blocks, ranges_of, lines):
|
|
406
|
+
"""{模块: [(块名或 None, 文本)]}——保留分段信息,便于逐块补 export。
|
|
407
|
+
|
|
408
|
+
为什么不能只给拼接好的整段文本:跨模块内部符号需要补 `export` 前缀,而补注入是
|
|
409
|
+
按块定位的(`^(async )?(function|const|let)`)。若作用于整段正文,模块开头的
|
|
410
|
+
注释/import 会让锚点 `^` 匹配不到,补 export 就**静默失效**——实测 helpers.js 的
|
|
411
|
+
ruleOut/severityLevel 因此没被 export,别的模块 import 时报
|
|
412
|
+
「does not provide an export named 'ruleOut'」。
|
|
413
|
+
"""
|
|
414
|
+
out = {}
|
|
415
|
+
for mod, value in plan.items():
|
|
416
|
+
# **必须按 plan 里的原顺序**逐项组装:命名块与行范围常需交错
|
|
417
|
+
# (如先 `let Schema = null` → 再 try 赋值语句 → 再兜底函数 → 再 `if (!Schema)` 兜底调用),
|
|
418
|
+
# 若「命名块全排前、行范围全追加到末尾」,语句会被搬到最后——
|
|
419
|
+
# 实测 lib/app/schema.js 里 `Schema = makeFallbackSchema()` 被排到 Config 之后,
|
|
420
|
+
# 于是 Config 求值时 Schema 仍是 null,直接 `Cannot read properties of null`。
|
|
421
|
+
parts = []
|
|
422
|
+
for item in value:
|
|
423
|
+
if isinstance(item, str):
|
|
424
|
+
parts.append((item, blocks[item]['body']))
|
|
425
|
+
else:
|
|
426
|
+
a, z = item
|
|
427
|
+
parts.append((None, range_text(lines, a, z)))
|
|
428
|
+
out[mod] = parts
|
|
429
|
+
return out
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def module_texts(plan, blocks, ranges_of, lines):
|
|
433
|
+
"""{模块: 该模块的正文文本}——命名块取块文本,行范围取区间文本,按 plan 顺序拼接。"""
|
|
434
|
+
parts = module_parts(plan, blocks, ranges_of, lines)
|
|
435
|
+
return {mod: '\n\n'.join(t for _n, t in ps) for mod, ps in parts.items()}
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def dep_edges(blocks, owner, plan, texts):
|
|
439
|
+
"""{模块: {源模块: [符号]}},按「剥注释后的真实引用」计算。"""
|
|
440
|
+
edges = {}
|
|
441
|
+
for mod in plan:
|
|
442
|
+
body = strip_comments(texts[mod])
|
|
443
|
+
refs = set()
|
|
444
|
+
for other, omod in owner.items():
|
|
445
|
+
if omod != mod and re.search(r'\b' + re.escape(other) + r'\b', body):
|
|
446
|
+
refs.add(other)
|
|
447
|
+
by_mod = {}
|
|
448
|
+
for sym in sorted(refs):
|
|
449
|
+
by_mod.setdefault(owner[sym], []).append(sym)
|
|
450
|
+
edges[mod] = by_mod
|
|
451
|
+
return edges
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def detect_cycles(plan, edges):
|
|
455
|
+
"""返回循环路径(无环返回 None)。ESM 循环会让符号静默 undefined,必须拦。"""
|
|
456
|
+
WHITE, GRAY = 0, 1
|
|
457
|
+
color = {m: WHITE for m in plan}
|
|
458
|
+
|
|
459
|
+
def dfs(u, stack):
|
|
460
|
+
color[u] = GRAY
|
|
461
|
+
for v in edges.get(u, {}):
|
|
462
|
+
if color[v] == GRAY:
|
|
463
|
+
return stack + [u, v]
|
|
464
|
+
if color[v] == WHITE:
|
|
465
|
+
found = dfs(v, stack + [u])
|
|
466
|
+
if found:
|
|
467
|
+
return found
|
|
468
|
+
color[u] = 2
|
|
469
|
+
return None
|
|
470
|
+
|
|
471
|
+
for m in plan:
|
|
472
|
+
if color[m] == WHITE:
|
|
473
|
+
found = dfs(m, [])
|
|
474
|
+
if found:
|
|
475
|
+
return found
|
|
476
|
+
return None
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def split_entry(value):
|
|
480
|
+
"""plan 的一个条目 → (块名清单, 行范围清单)。
|
|
481
|
+
|
|
482
|
+
条目值可以是字符串(顶层块名)或 [起, 止](1-based 闭区间行号)的混合列表。
|
|
483
|
+
为什么需要行范围:并非所有文件都是「顶层命名块」结构——注册表类文件
|
|
484
|
+
(一串 registerCompiler('kind', ...) 语句 + 共享助手)没有名字可指,
|
|
485
|
+
只能按语句行范围切;只支持命名块的工具在这类文件上完全用不了。
|
|
486
|
+
"""
|
|
487
|
+
names, ranges = [], []
|
|
488
|
+
for item in value:
|
|
489
|
+
if isinstance(item, str):
|
|
490
|
+
names.append(item)
|
|
491
|
+
elif isinstance(item, (list, tuple)) and len(item) == 2 and all(isinstance(x, int) for x in item):
|
|
492
|
+
ranges.append((item[0], item[1]))
|
|
493
|
+
else:
|
|
494
|
+
sys.exit(f'❌ plan 条目格式不对(应为块名或 [起,止]):{item!r}')
|
|
495
|
+
return names, ranges
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def absorb_up(lines, i):
|
|
499
|
+
"""从 0-based 行 i 向上吞掉紧邻的注释/空行,返回新的起始行。"""
|
|
500
|
+
while i > 0 and re.match(r'^\s*(//|/\*|\*|\*/|$)', lines[i - 1]):
|
|
501
|
+
i -= 1
|
|
502
|
+
return i
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
def range_text(lines, start, end):
|
|
506
|
+
"""取 1-based 闭区间 [start, end] 的文本,并向上吞掉紧邻注释。"""
|
|
507
|
+
if not (1 <= start <= end <= len(lines)):
|
|
508
|
+
sys.exit(f'❌ 行范围越界:{start}-{end}(文件共 {len(lines)} 行)')
|
|
509
|
+
a = absorb_up(lines, start - 1)
|
|
510
|
+
return '\n'.join(lines[a:end]).strip('\n')
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
def reconcile_blocks_with_ranges(blocks, lines, ranges):
|
|
514
|
+
"""把「跨到行范围内的命名块」按范围起点截断,返回新的块字典。
|
|
515
|
+
|
|
516
|
+
为什么需要:块尾边界 = 下一个「顶层声明」起点;当文件末尾是一串非声明的顶层
|
|
517
|
+
语句(如 registerCompiler('kind', ...))时,最后一个命名块的边界会一路延伸到
|
|
518
|
+
EOF,把这些语句也吞进去——若这些语句同时又按行范围分配给了别的模块,同一段
|
|
519
|
+
代码就会**同时出现在两个模块里**(实测 listRegisteredKinds 吞掉 16 个
|
|
520
|
+
registerCompiler 语句,与 structure/frontend/file-health 的正文重复)。
|
|
521
|
+
|
|
522
|
+
规则(行范围是显式意图,优先级高于自动推断的块边界):
|
|
523
|
+
· 范围起点落在块内部 → 块尾截断到该范围起点之前
|
|
524
|
+
· 范围完全覆盖块起点 → 直接报错(该块应改为按范围分配,不该出现在 names 里)
|
|
525
|
+
"""
|
|
526
|
+
out = {}
|
|
527
|
+
for name, b in blocks.items():
|
|
528
|
+
b = dict(b)
|
|
529
|
+
for a, _z in ranges:
|
|
530
|
+
a0 = a - 1 # 1-based → 0-based
|
|
531
|
+
if b['start'] < a0 < b['end']:
|
|
532
|
+
# 必须**可见**:静默截断会产出语法坏文件(实测把块尾的闭合 `}` 划给了
|
|
533
|
+
# 另一个模块的范围,写出 `function f() { …` 不带 `}`),而截断本身是
|
|
534
|
+
# 合法设计(块尾延伸到后续匿名语句时正需截断),所以只能提示不能报错。
|
|
535
|
+
print(f" ⚠ 块 {name} 的尾部被行范围 {a}-{_z} 截断:{b['start'] + 1}-{b['end']}"
|
|
536
|
+
f" → {b['start'] + 1}-{a0}(若该范围起点其实是本块的闭合括号,说明 plan 写错了:"
|
|
537
|
+
f"范围应从块的后一行开始)")
|
|
538
|
+
b['end'] = a0
|
|
539
|
+
for a, z in ranges:
|
|
540
|
+
a0, z0 = a - 1, z - 1
|
|
541
|
+
if a0 <= b['start'] <= z0:
|
|
542
|
+
sys.exit(f'❌ 块 {name}(行 {b["start"] + 1})落在行范围 {a}-{z} 内:'
|
|
543
|
+
f'该块应从 names 移除,直接由该范围承载。')
|
|
544
|
+
body = '\n'.join(lines[b['start']:b['end']]).strip('\n')
|
|
545
|
+
if not body:
|
|
546
|
+
sys.exit(f'❌ 块 {name} 截断后为空(行范围与块边界冲突,请检查 plan)')
|
|
547
|
+
b['body'] = body
|
|
548
|
+
out[name] = b
|
|
549
|
+
return out
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def load_plan(path):
|
|
553
|
+
cfg = json.loads(Path(path).read_text(encoding='utf-8'))
|
|
554
|
+
target = Path(cfg['file'])
|
|
555
|
+
src = target.read_text(encoding='utf-8')
|
|
556
|
+
lines = src.split('\n')
|
|
557
|
+
plan = cfg['plan']
|
|
558
|
+
owner, ranges_of = {}, {}
|
|
559
|
+
for mod, value in plan.items():
|
|
560
|
+
names, ranges = split_entry(value)
|
|
561
|
+
for n in names:
|
|
562
|
+
owner[n] = mod
|
|
563
|
+
ranges_of[mod] = ranges
|
|
564
|
+
all_ranges = [r for rs in ranges_of.values() for r in rs]
|
|
565
|
+
blocks = reconcile_blocks_with_ranges({b['name']: b for b in parse_blocks(src)}, lines, all_ranges)
|
|
566
|
+
for n in owner:
|
|
567
|
+
if n not in blocks:
|
|
568
|
+
sys.exit(f'❌ plan 里的块不存在: {n}({target})')
|
|
569
|
+
owner = collect_ownership(plan, blocks, ranges_of, lines)
|
|
570
|
+
missing, dup = audit_coverage(plan, blocks, ranges_of, lines, lines)
|
|
571
|
+
if dup:
|
|
572
|
+
shown = dup[:12]
|
|
573
|
+
sys.exit(f'❌ 以下行被多个模块重复承载(同一段代码进两个模块):{shown}'
|
|
574
|
+
f'{" …共 " + str(len(dup)) + " 行" if len(dup) > 12 else ""}\n'
|
|
575
|
+
f' 常见原因:命名块与行范围重叠(一个块按名分配、其正文又被范围截断/覆盖)。')
|
|
576
|
+
if missing:
|
|
577
|
+
shown = missing[:12]
|
|
578
|
+
sys.exit(f'❌ 以下原文行没有任何模块承载(拆完会静默丢失):{shown}'
|
|
579
|
+
f'{" …共 " + str(len(missing)) + " 行" if len(missing) > 12 else ""}\n'
|
|
580
|
+
f' 常见原因:只把块按名分配、却没覆盖该块被截断后剩下的正文。')
|
|
581
|
+
return cfg, target, src, blocks, plan, owner, ranges_of, lines
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
# ───────────────────────── 子命令 ─────────────────────────
|
|
585
|
+
|
|
586
|
+
def cmd_analyze(argv):
|
|
587
|
+
path = Path(argv[0])
|
|
588
|
+
src = path.read_text(encoding='utf-8')
|
|
589
|
+
blocks = parse_blocks(src)
|
|
590
|
+
total = src.count('\n') + 1
|
|
591
|
+
print(f'═══ {path} ═══')
|
|
592
|
+
print(f'总行数 {total}|顶层块 {len(blocks)}|公开导出 {sum(1 for b in blocks if b["exported"])}\n')
|
|
593
|
+
print('顶层块(行号 / 行数 / 是否导出):')
|
|
594
|
+
for b in blocks:
|
|
595
|
+
n = b['body'].count('\n') + 1
|
|
596
|
+
flag = 'export' if b['exported'] else ' '
|
|
597
|
+
print(f' {b["start"] + 1:>5} {n:>4} 行 {flag} {b["name"]}')
|
|
598
|
+
print('\n块间依赖(供划分模块用;已剥注释,不含散文提及):')
|
|
599
|
+
by_name = {b['name']: b['body'] for b in blocks}
|
|
600
|
+
for b in blocks:
|
|
601
|
+
refs = sorted(n for n in by_name
|
|
602
|
+
if n != b['name'] and re.search(r'\b' + re.escape(n) + r'\b', strip_comments(b['body'])))
|
|
603
|
+
if refs:
|
|
604
|
+
print(f' {b["name"]:<34} → {", ".join(refs)}')
|
|
605
|
+
imps = [l for l in src.split('\n') if l.startswith('import ')]
|
|
606
|
+
if imps:
|
|
607
|
+
print('\n顶部 import(按符号自动分发到各模块):')
|
|
608
|
+
for i in imps:
|
|
609
|
+
print(' ', i)
|
|
610
|
+
print('\n下一步:据上表把块分到 plan.json 的 plan 里,再跑 split。')
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
def check_split_output(written, outdir):
|
|
614
|
+
"""写盘后逐文件语法校验(node --check)——抓「块边界划错导致产出非法 JS」的静默坏文件。
|
|
615
|
+
|
|
616
|
+
为什么单独成函数:这是 cmd_split 里唯一与「拼装/写盘」无关的独立阶段(自审
|
|
617
|
+
python/max-function-length 报 cmd_split 过长),提出来既降函数长度,也让这段
|
|
618
|
+
「产物合法性」判定能被单独阅读与复用。
|
|
619
|
+
|
|
620
|
+
失败即 sys.exit(不返回):模块文本已落盘但不是合法 JS,继续跑只会让坏文件进入
|
|
621
|
+
后续提交;提示里带上「修好 plan 后重跑」与文件位置。
|
|
622
|
+
"""
|
|
623
|
+
for mod, content in written.items():
|
|
624
|
+
if not mod.endswith(('.js', '.mjs', '.cjs')):
|
|
625
|
+
continue
|
|
626
|
+
probe = subprocess.run(['node', '--check', '--input-type=module'],
|
|
627
|
+
input=content, capture_output=True, text=True)
|
|
628
|
+
if probe.returncode != 0:
|
|
629
|
+
err = (probe.stderr or '').strip().splitlines()
|
|
630
|
+
detail = next((l for l in err if 'Error' in l), err[0] if err else '')
|
|
631
|
+
sys.exit(f'❌ {mod} 语法校验失败——模块文本已写出但**不是合法 JS**,'
|
|
632
|
+
f'通常是块边界/行范围把闭合括号划走了:{detail}\n'
|
|
633
|
+
f' 该文件已留在 {outdir}/{mod},修好 plan 后重跑。')
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
def cmd_split(argv):
|
|
637
|
+
dry = '--dry-run' in argv
|
|
638
|
+
argv = [a for a in argv if a != '--dry-run']
|
|
639
|
+
cfg, target, src, blocks, plan, owner, ranges_of, lines = load_plan(argv[0])
|
|
640
|
+
outdir = Path(cfg['outdir'])
|
|
641
|
+
imports = parse_imports(src)
|
|
642
|
+
parts_by_mod = module_parts(plan, blocks, ranges_of, lines)
|
|
643
|
+
texts = {m: '\n\n'.join(t for _n, t in ps) for m, ps in parts_by_mod.items()}
|
|
644
|
+
edges = dep_edges(blocks, owner, plan, texts)
|
|
645
|
+
# 「被别的模块引用、需要补 export」的符号集合。
|
|
646
|
+
# 注意不能写成 `name in refs`——edges 是 {模块: {源模块: [符号]}} 的**三层**结构,
|
|
647
|
+
# 那样等于拿符号名去查模块名,永远为假 → 内部符号静默不 export → 别的模块 import
|
|
648
|
+
# 时报「does not provide an export named 'ruleOut'」。必须展平到符号层再判断。
|
|
649
|
+
cross_needed = {sym for by_mod in edges.values() for syms in by_mod.values() for sym in syms}
|
|
650
|
+
cyc = detect_cycles(plan, edges)
|
|
651
|
+
if cyc:
|
|
652
|
+
sys.exit(f'❌ 模块循环依赖:{" → ".join(cyc)}\n'
|
|
653
|
+
f' ESM 循环会让符号静默变 undefined,请调整 plan 让依赖成 DAG。')
|
|
654
|
+
|
|
655
|
+
orig_dir = str(target.parent)
|
|
656
|
+
print(f'✓ 依赖图无环,开始{"预演" if dry else "切分"}:{target} → {outdir}/')
|
|
657
|
+
written = {}
|
|
658
|
+
for mod in plan:
|
|
659
|
+
# 内部符号若被跨模块引用 → 逐块补 export(行范围内的语句本身不需 export)
|
|
660
|
+
pieces = []
|
|
661
|
+
for name, text in parts_by_mod[mod]:
|
|
662
|
+
if name and not blocks[name]['exported'] and name in cross_needed:
|
|
663
|
+
# 用多行锚点:块文本以「前置注释」开头(块起始行向上吞了注释),
|
|
664
|
+
# 单行模式 `^` 只会匹配到注释首行,永远补不上 export。
|
|
665
|
+
# 加了 re.M 后锚点落在第一条真正的声明行上(注释行以 // 或 * 开头,不会误匹配)。
|
|
666
|
+
text = re.sub(r'(?m)^((?:async )?(?:function|const|let) )', r'export \1', text, count=1)
|
|
667
|
+
pieces.append(text)
|
|
668
|
+
body = '\n\n'.join(pieces)
|
|
669
|
+
body_all = strip_comments(body)
|
|
670
|
+
used = {sym for sym in imports if re.search(r'\b' + re.escape(sym) + r'\b', body_all)}
|
|
671
|
+
mod_dir = str(outdir)
|
|
672
|
+
final, seen = [], set()
|
|
673
|
+
for sym, stmt in imports.items():
|
|
674
|
+
if sym in used and stmt not in seen:
|
|
675
|
+
seen.add(stmt)
|
|
676
|
+
syms = [s for s, st in imports.items() if st == stmt]
|
|
677
|
+
if all(s in used for s in syms):
|
|
678
|
+
picked = stmt
|
|
679
|
+
else:
|
|
680
|
+
kept = [s for s in syms if s in used]
|
|
681
|
+
picked = re.sub(r'\{.*\}', '{ ' + ', '.join(kept) + ' }', stmt)
|
|
682
|
+
final.append(rewrite_rel_imports(picked, orig_dir, mod_dir))
|
|
683
|
+
for m2, syms in sorted(edges[mod].items()):
|
|
684
|
+
final.append(f"import {{ {', '.join(syms)} }} from './{m2}';")
|
|
685
|
+
final += cfg.get('external', {}).get(mod, [])
|
|
686
|
+
body = rewrite_dynamic_imports(body, orig_dir, str(outdir))
|
|
687
|
+
content = '\n\n'.join(x for x in [cfg.get('headers', {}).get(mod, ''),
|
|
688
|
+
'\n'.join(final), body] if x)
|
|
689
|
+
content = re.sub(r'\n{3,}', '\n\n', content).strip('\n') + '\n'
|
|
690
|
+
written[mod] = content
|
|
691
|
+
if not dry:
|
|
692
|
+
outdir.mkdir(parents=True, exist_ok=True)
|
|
693
|
+
(outdir / mod).write_text(content, encoding='utf-8')
|
|
694
|
+
# 写盘后语法校验:这是唯一能抓住「块边界被划错导致少了个闭合括号」这类
|
|
695
|
+
# 静默产坏文件的手段——上面所有行级覆盖审计都只看「行有没有被承载」,
|
|
696
|
+
# 看不出承载之后拼出来的文本是否仍是合法程序(实测漏过:块尾 `}` 被划给别的模块,
|
|
697
|
+
# 审计全过、写出的文件 node 直接 SyntaxError)。实现见 check_split_output。
|
|
698
|
+
if not dry and not cfg.get('skip_syntax_check'):
|
|
699
|
+
check_split_output(written, outdir)
|
|
700
|
+
|
|
701
|
+
orig_exports = [b['name'] for b in parse_blocks(src) if b['exported']]
|
|
702
|
+
clauses = parse_export_clauses(src)
|
|
703
|
+
by_mod = {}
|
|
704
|
+
for n in orig_exports:
|
|
705
|
+
by_mod.setdefault(owner[n], []).append(n)
|
|
706
|
+
idx_lines = [cfg.get('index_header', f'/**\n * {target.name} · 统一出口(只做再导出,不含实现)\n */'), '']
|
|
707
|
+
# 裸副作用 import 原样保留到出口(路径按新位置重算)——它们是初始化开关,丢了会静默改变行为
|
|
708
|
+
bare = [rewrite_rel_imports(st, orig_dir, str(outdir)) for st in parse_side_effect_imports(src)]
|
|
709
|
+
for st in bare:
|
|
710
|
+
idx_lines.append(st)
|
|
711
|
+
if bare:
|
|
712
|
+
idx_lines.append('')
|
|
713
|
+
# 副作用导入:注册表类文件靠 import 触发注册(顺序 = plan 顺序,保持原注册次序)
|
|
714
|
+
for m in cfg.get('index_imports', []):
|
|
715
|
+
idx_lines.append(f"import './{m}';")
|
|
716
|
+
if cfg.get('index_imports'):
|
|
717
|
+
idx_lines.append('')
|
|
718
|
+
for mod in plan:
|
|
719
|
+
if mod in by_mod:
|
|
720
|
+
idx_lines.append(f"export {{ {', '.join(by_mod[mod])} }} from './{mod}';")
|
|
721
|
+
elif mod not in set(cfg.get('index_imports', [])):
|
|
722
|
+
# (已由 index_imports 显式声明的跳过,避免重复 import 行)
|
|
723
|
+
# 没有任何导出的模块 = 只有顶层语句(注册表类文件:一串 registerCompiler(...))。
|
|
724
|
+
# 它不会出现在任何再导出行里,若不显式 import,**整个模块永远不会被加载**,
|
|
725
|
+
# 里面的注册/初始化全部不执行——不报错、不崩,只是「功能静默消失」
|
|
726
|
+
# (实测:registry.js 的三条 registerCompiler 一条都没跑)。
|
|
727
|
+
# 与裸副作用 import 是同一类失效,故同样必须保证被 import。
|
|
728
|
+
idx_lines.append(f"import './{mod}';")
|
|
729
|
+
# 原有的 export 子句必须原样保留(公开面的一部分)
|
|
730
|
+
for cl in clauses:
|
|
731
|
+
if cl['from']:
|
|
732
|
+
idx_lines.append(rewrite_rel_imports(cl['raw'], orig_dir, str(outdir)))
|
|
733
|
+
else:
|
|
734
|
+
# 无 from:再导出的是本文件 import 进来的符号,出口必须先把它们 import 进来,
|
|
735
|
+
# 否则 `export { x };` 会因绑定不存在直接 SyntaxError。
|
|
736
|
+
for stmt in prune_import_for(cl['names'], imports):
|
|
737
|
+
idx_lines.append(rewrite_rel_imports(stmt, orig_dir, str(outdir)))
|
|
738
|
+
idx_lines.append(cl['raw'])
|
|
739
|
+
index_name = cfg.get('index', 'index.js')
|
|
740
|
+
written[index_name] = '\n'.join(idx_lines) + '\n'
|
|
741
|
+
if not dry:
|
|
742
|
+
(outdir / index_name).write_text(written[index_name], encoding='utf-8')
|
|
743
|
+
|
|
744
|
+
for m, c in sorted(written.items(), key=lambda x: -x[1].count('\n')):
|
|
745
|
+
print(f' {m:<24} {c.count(chr(10)) + 1:>5} 行')
|
|
746
|
+
print(f'\n 原文件 {src.count(chr(10)) + 1} 行 → {len(plan)} 模块 + {index_name}')
|
|
747
|
+
print(f' 原导出 {len(orig_exports)} 个(index 全部保留,顺序不变)')
|
|
748
|
+
if dry:
|
|
749
|
+
print('\n(--dry-run:未写盘)')
|
|
750
|
+
else:
|
|
751
|
+
print('\n下一步(必做,缺一不可):')
|
|
752
|
+
print(' 1. node --check 每个新模块')
|
|
753
|
+
print(' 2. python3 module-splitter.py verify <plan.json> # 导出名集合比对')
|
|
754
|
+
print(' 3. 跑全部测试')
|
|
755
|
+
print(' 4. 与拆分前做审计/输出产出比对(未受影响文件的结果应逐条一致)')
|
|
756
|
+
print(' 5. 确认旧文件已删除、无残留重复实现')
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
def cmd_verify(argv):
|
|
760
|
+
cfg, target, src, blocks, plan, owner, ranges_of, lines = load_plan(argv[0])
|
|
761
|
+
outdir = Path(cfg['outdir'])
|
|
762
|
+
index_name = cfg.get('index', 'index.js')
|
|
763
|
+
idx = (outdir / index_name)
|
|
764
|
+
if not idx.exists():
|
|
765
|
+
sys.exit(f'❌ 出口文件不存在: {idx}(先跑 split)')
|
|
766
|
+
idx_src = idx.read_text(encoding='utf-8')
|
|
767
|
+
reexported = set()
|
|
768
|
+
# 两种形态都要认:`export { a } from './m.js';`(模块再导出)与
|
|
769
|
+
# `export { a, b };`(把出口自己 import 进来的符号再导出,原文件末尾常见)。
|
|
770
|
+
# 只匹配带 from 的那种会把后者判成「丢失导出」,误报 verify 失败。
|
|
771
|
+
for m in re.finditer(r"^export\s*\{([^}]*)\}\s*(?:from\s*['\"][^'\"]*['\"]\s*)?;", idx_src, re.M):
|
|
772
|
+
reexported |= {s.strip() for s in m.group(1).split(',') if s.strip()}
|
|
773
|
+
reexported |= set(re.findall(r'^export\s+(?:async )?(?:function|const|let)\s+(\w+)', idx_src, re.M))
|
|
774
|
+
original = {b['name'] for b in parse_blocks(src) if b['exported']}
|
|
775
|
+
for cl in parse_export_clauses(src):
|
|
776
|
+
original |= set(cl['names'])
|
|
777
|
+
missing = sorted(original - reexported)
|
|
778
|
+
extra = sorted(reexported - original)
|
|
779
|
+
print(f'原文件导出 {len(original)} 个|{index_name} 再导出 {len(reexported)} 个')
|
|
780
|
+
if missing:
|
|
781
|
+
print(f'❌ 丢失导出(调用方会拿到 undefined): {missing}')
|
|
782
|
+
if extra:
|
|
783
|
+
print(f'⚠ 新增导出(扩大了公开面,确认是否有意): {extra}')
|
|
784
|
+
if not missing and not extra:
|
|
785
|
+
print('✅ 导出面与拆分前完全一致')
|
|
786
|
+
# 附带:检查残留文件
|
|
787
|
+
print(f'\n原文件 {target} ' + ('仍存在——确认是否要删除(保留会形成两份实现)'
|
|
788
|
+
if target.exists() else '已删除(正确)'))
|
|
789
|
+
sys.exit(1 if missing else 0)
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
def main():
|
|
793
|
+
if len(sys.argv) < 3 or sys.argv[1] not in ('analyze', 'split', 'verify'):
|
|
794
|
+
print(__doc__)
|
|
795
|
+
sys.exit(0 if len(sys.argv) == 1 or sys.argv[1] in ('-h', '--help') else 1)
|
|
796
|
+
{'analyze': cmd_analyze, 'split': cmd_split, 'verify': cmd_verify}[sys.argv[1]](sys.argv[2:])
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
if __name__ == '__main__':
|
|
800
|
+
main()
|