dsh-git-push 2.20.18 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/README.md +3 -2
  2. package/cli-tool.mjs +2 -4
  3. package/cli.mjs +36 -2
  4. package/lib/app/apply.js +9 -9
  5. package/lib/app/command-registry.js +44 -20
  6. package/lib/app/handlers/meta.js +7 -142
  7. package/lib/app/handlers/npm.js +18 -1
  8. package/lib/app/handlers/preprocess.js +107 -0
  9. package/lib/app/handlers/repo-actions.js +2 -2
  10. package/lib/app/http-handlers.js +25 -12
  11. package/lib/app/index.js +2 -1
  12. package/lib/app/inject-text.js +3 -2
  13. package/lib/app/preprocess-client.js +136 -0
  14. package/lib/app/repo-ownership.js +1 -1
  15. package/lib/app/schema.js +3 -3
  16. package/lib/app/severity.js +14 -0
  17. package/lib/app/slash-commands.js +5 -164
  18. package/lib/app/tool-call.js +75 -140
  19. package/lib/app/tools.js +13 -6
  20. package/lib/cli/commands-preprocess.mjs +74 -0
  21. package/lib/cli/commands-tool.mjs +3 -2
  22. package/lib/cli/commands-vcs.mjs +4 -4
  23. package/lib/cli/shared.mjs +42 -13
  24. package/lib/client-parts/git/controller.js +2 -2
  25. package/lib/client.js +2 -2
  26. package/lib/commit-push/pre-commit-engine.js +57 -10
  27. package/lib/commit-push.js +125 -36
  28. package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
  29. package/lib/git/ignore.js +1 -1
  30. package/lib/git/repos.js +2 -2
  31. package/lib/git/sensitive.js +1 -1
  32. package/lib/npm/publish.js +20 -5
  33. package/lib/plugin/auto-push.js +2 -2
  34. package/lib/preprocess/providers.js +191 -0
  35. package/lib/self/index.js +1 -1
  36. package/lib/skip-dirs.js +14 -55
  37. package/package.json +29 -37
  38. package/scripts/build-client.mjs +2 -0
  39. package/scripts/check.mjs +4 -2
  40. package/scripts/local-registry.mjs +2 -1
  41. package/skills/dev/dsh-git-push.md +5 -0
  42. package/cli-audit.mjs +0 -141
  43. package/lib/app/audit-api.js +0 -173
  44. package/lib/app/host-doc-checks.js +0 -25
  45. package/lib/app/scope-check.js +0 -69
  46. package/lib/ast/brace.js +0 -120
  47. package/lib/ast/callgraph.js +0 -135
  48. package/lib/ast/code-lines.js +0 -175
  49. package/lib/ast/consts.js +0 -12
  50. package/lib/ast/control-flow.js +0 -470
  51. package/lib/ast/credential.js +0 -315
  52. package/lib/ast/dataflow.js +0 -391
  53. package/lib/ast/dup-code.js +0 -165
  54. package/lib/ast/fs-hotspots.js +0 -154
  55. package/lib/ast/index.js +0 -38
  56. package/lib/ast/io-risk-const.js +0 -95
  57. package/lib/ast/io-risk-fn.js +0 -208
  58. package/lib/ast/io-risk-loop.js +0 -308
  59. package/lib/ast/io-risk.js +0 -314
  60. package/lib/ast/lang.js +0 -156
  61. package/lib/ast/line-count.js +0 -52
  62. package/lib/ast/magic-number.js +0 -272
  63. package/lib/ast/naming.js +0 -292
  64. package/lib/ast/perf.js +0 -488
  65. package/lib/ast/scope-collisions.js +0 -228
  66. package/lib/ast/scope.js +0 -241
  67. package/lib/ast/shell.js +0 -131
  68. package/lib/ast/size.js +0 -448
  69. package/lib/ast/symbol-index.js +0 -399
  70. package/lib/ast/tokenizer.js +0 -421
  71. package/lib/ast/treesitter.js +0 -413
  72. package/lib/audit/analysis-coverage.js +0 -98
  73. package/lib/audit/audit-file.js +0 -205
  74. package/lib/audit/auditignore-rules.js +0 -148
  75. package/lib/audit/checks.js +0 -17
  76. package/lib/audit/client-entry.js +0 -93
  77. package/lib/audit/collector.js +0 -610
  78. package/lib/audit/ext-runner.js +0 -80
  79. package/lib/audit/file-context.js +0 -391
  80. package/lib/audit/finding.js +0 -101
  81. package/lib/audit/gitignore-match.js +0 -113
  82. package/lib/audit/glob.js +0 -63
  83. package/lib/audit/history-report.js +0 -100
  84. package/lib/audit/history.js +0 -126
  85. package/lib/audit/ignore-blind.js +0 -102
  86. package/lib/audit/index.js +0 -33
  87. package/lib/audit/orchestrate.js +0 -672
  88. package/lib/audit/public.js +0 -24
  89. package/lib/audit/repo-level.js +0 -30
  90. package/lib/audit/report-yaml.js +0 -90
  91. package/lib/audit/scope-policy.js +0 -110
  92. package/lib/audit/slot.js +0 -66
  93. package/lib/audit-rules/audit-rules-comment.yml +0 -199
  94. package/lib/audit-rules/audit-rules-cpp.yml +0 -114
  95. package/lib/audit-rules/audit-rules-csharp.yml +0 -82
  96. package/lib/audit-rules/audit-rules-docs.yml +0 -32
  97. package/lib/audit-rules/audit-rules-dsh.yml +0 -177
  98. package/lib/audit-rules/audit-rules-filehealth.yml +0 -42
  99. package/lib/audit-rules/audit-rules-folder.yml +0 -99
  100. package/lib/audit-rules/audit-rules-frontend.yml +0 -335
  101. package/lib/audit-rules/audit-rules-go.yml +0 -103
  102. package/lib/audit-rules/audit-rules-i18n.yml +0 -45
  103. package/lib/audit-rules/audit-rules-java.yml +0 -105
  104. package/lib/audit-rules/audit-rules-nodejs.yml +0 -662
  105. package/lib/audit-rules/audit-rules-npm.yml +0 -187
  106. package/lib/audit-rules/audit-rules-paths.yml +0 -98
  107. package/lib/audit-rules/audit-rules-performance.yml +0 -245
  108. package/lib/audit-rules/audit-rules-php.yml +0 -101
  109. package/lib/audit-rules/audit-rules-private.yml +0 -24
  110. package/lib/audit-rules/audit-rules-python.yml +0 -106
  111. package/lib/audit-rules/audit-rules-robustness.yml +0 -31
  112. package/lib/audit-rules/audit-rules-rust.yml +0 -109
  113. package/lib/audit-rules/audit-rules-structure.yml +0 -158
  114. package/lib/audit-rules/audit-rules-swift.yml +0 -99
  115. package/lib/audit-rules/audit-rules-template.yml +0 -108
  116. package/lib/audit-ui-sdk/contract.js +0 -117
  117. package/lib/audit-ui-sdk/index.js +0 -17
  118. package/lib/checks/button-bind.js +0 -347
  119. package/lib/checks/common.js +0 -170
  120. package/lib/checks/credential-file.js +0 -43
  121. package/lib/checks/dataflow.js +0 -40
  122. package/lib/checks/dispatch.js +0 -117
  123. package/lib/checks/dup-code.js +0 -69
  124. package/lib/checks/dup-const.js +0 -232
  125. package/lib/checks/file-health.js +0 -150
  126. package/lib/checks/filter.js +0 -137
  127. package/lib/checks/folder.js +0 -173
  128. package/lib/checks/fs-scanner.js +0 -57
  129. package/lib/checks/hardcoded-path.js +0 -98
  130. package/lib/checks/index.js +0 -49
  131. package/lib/checks/io.js +0 -66
  132. package/lib/checks/magic-number.js +0 -42
  133. package/lib/checks/npm-json.js +0 -273
  134. package/lib/checks/private.js +0 -64
  135. package/lib/checks/regex.js +0 -298
  136. package/lib/checks/scope-collision.js +0 -68
  137. package/lib/checks/semantic.js +0 -134
  138. package/lib/checks/structural.js +0 -281
  139. package/lib/cli/commands-audit.mjs +0 -142
  140. package/lib/cli/commands-doc.mjs +0 -127
  141. package/lib/client-parts/audit/audit.js +0 -301
  142. package/lib/exempt/tool.js +0 -198
  143. package/lib/git/module-splitter.js +0 -47
  144. package/lib/link-check/index.js +0 -187
  145. package/lib/lint-adapter/detect.js +0 -161
  146. package/lib/lint-adapter/index.js +0 -50
  147. package/lib/lint-adapter/runner.js +0 -133
  148. package/lib/rule/compilers/credential.js +0 -62
  149. package/lib/rule/compilers/dataflow.js +0 -38
  150. package/lib/rule/compilers/file-health.js +0 -75
  151. package/lib/rule/compilers/frontend.js +0 -86
  152. package/lib/rule/compilers/func.js +0 -35
  153. package/lib/rule/compilers/helpers.js +0 -71
  154. package/lib/rule/compilers/index.js +0 -29
  155. package/lib/rule/compilers/link-semantic.js +0 -45
  156. package/lib/rule/compilers/list-kinds.js +0 -12
  157. package/lib/rule/compilers/numeric-kinds.js +0 -73
  158. package/lib/rule/compilers/regex.js +0 -78
  159. package/lib/rule/compilers/structure.js +0 -107
  160. package/lib/rule/compilers.js +0 -17
  161. package/lib/rule/homoglyph.js +0 -45
  162. package/lib/rule/loader.js +0 -274
  163. package/lib/rule/registry.js +0 -122
  164. package/lib/rule/scope.js +0 -67
  165. package/lib/score/docs-score.js +0 -166
  166. package/lib/score/index.js +0 -234
  167. package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
  168. package/scripts/audit-ext/_example-readme-present.mjs +0 -34
  169. package/scripts/audit-ext/dsh-inject-declarations.mjs +0 -209
  170. package/scripts/audit-ext/variable-min-length.mjs +0 -102
  171. package/scripts/audit-gate.mjs +0 -235
  172. package/scripts/audit-runner.mjs +0 -35
  173. package/scripts/audit-runtime-check.mjs +0 -129
  174. package/scripts/browser-page-probe.mjs +0 -271
  175. package/scripts/check-js-scope.mjs +0 -87
  176. package/scripts/clean-date-comments.py +0 -102
  177. package/scripts/cpu-sample.mjs +0 -188
  178. package/scripts/doc-drift.mjs +0 -200
  179. package/scripts/hot-path-probe-cjs.cjs +0 -71
  180. package/scripts/hot-path-probe-fs-shim.mjs +0 -28
  181. package/scripts/hot-path-probe-fsp-shim.mjs +0 -31
  182. package/scripts/hot-path-probe.mjs +0 -153
  183. package/scripts/module-splitter.py +0 -800
  184. package/scripts/picker-repro.mjs +0 -115
  185. package/scripts/platform-scan.mjs +0 -168
  186. package/scripts/probe-recheck.mjs +0 -47
  187. package/scripts/rename-locator.mjs +0 -97
  188. package/scripts/rule-switch.mjs +0 -97
  189. package/scripts/rules-solo-audit.mjs +0 -173
  190. package/scripts/scan-file-io.mjs +0 -717
  191. package/scripts/scan-fs-hotspots.mjs +0 -107
  192. package/scripts/symbol-index.mjs +0 -85
  193. package/scripts/verify-prestep.mjs +0 -100
  194. package/skills/dev/audit-rule-authoring.md +0 -58
  195. package/skills/guide/guide-audit.md +0 -270
  196. /package/lib/{audit → app}/ownership.js +0 -0
  197. /package/lib/app/{schema-audit.js → schema-preprocess.js} +0 -0
  198. /package/lib/{audit-defaults.js → git/defaults.js} +0 -0
@@ -1,421 +0,0 @@
1
- // dsh-skip-func-length: tokenizeUncached 为纯线性解析主体(逐字符状态机),拆成多个函数会引入跨函数
2
- // 共享的可变状态(行号/块注释状态/输出数组),反而更易出错、更难读;故整文件豁免函数长度检查。
3
- /**
4
- * AST 实现层 · 分词器
5
- *
6
- * 职责:把源码切成 token 流(字符串/模板串/注释感知),并提供按文本内容的 LRU 缓存。
7
- * 本模块是全部 token 级检查的公共底座——其余 ast/ 模块都通过 tokenize() 取流。
8
- *
9
- * 自研 tokenizer 的原因:零第三方依赖;Node 无内置 JS AST,token 级已足够覆盖
10
- * 「注释/字符串里的内容不算代码」这类判定(逐行正则做不到)。
11
- */
12
-
13
- /**
14
- * tokenize 结果缓存(性能优化)。
15
- *
16
- * 动机:审计一个文件时,多条 AST 精筛规则(short-func-name / magic-number /
17
- * credential-value / small-file-read)会**各自独立**调用 tokenize,同一个文件
18
- * 被重复切词 5+ 次。实测 1255 行文件单次 tokenize 16ms,重复调用白浪费约 64ms/文件,
19
- * 在弱 CPU 机器 + 大仓库上直接表现为「提交推送卡住」。
20
- *
21
- * tokenize 是纯函数(同输入必得同输出)且调用方只读返回数组(无 push/splice/sort),
22
- * 因此可按文本内容缓存。用 Map(插入序 LRU)+ 上限淘汰,避免长驻进程内存无界增长。
23
- */
24
- const TOKEN_CACHE = new Map();
25
-
26
- /** 缓存条目数上限(防「海量小文件」把 Map 撑爆)。可用 env 覆盖(不硬编码)。 */
27
- const TOKEN_CACHE_MAX = Number(process.env.DSH_GIT_PUSH_TOKEN_CACHE_MAX || 512) || 512;
28
- /**
29
- * 缓存**字节预算**(按被缓存文本的字符数计)。为什么要有:旧口径只看条数(512 条),
30
- * 在大文件仓库上命中率低——512 条小文件就能把大文件全挤出去;而大文件恰恰最贵
31
- * (单次 tokenize 耗时与其长度成正比,且审计里同一文件会被多条规则反复切词)。
32
- * 改为「条数 + 字节」双上限后,大文件能留在缓存里。
33
- * 可用 env 覆盖(不硬编码):DSH_GIT_PUSH_TOKEN_CACHE_BYTES(默认 8MiB 文本)。
34
- */
35
- const TOKEN_CACHE_MAX_BYTES = Number(process.env.DSH_GIT_PUSH_TOKEN_CACHE_BYTES || 8 * 1024 * 1024) || 8 * 1024 * 1024;
36
- /** 当前缓存占用(字符数近似字节;仅用于预算判断,不参与任何判定结果)。 */
37
- let TOKEN_CACHE_BYTES = 0;
38
-
39
- /**
40
- * 轻量 tokenizer:把文本切成 token 流,字符串/模板串/注释原样保留但标记类型。
41
- *
42
- * 结果按文本内容缓存(见 TOKEN_CACHE):同一文件被多条规则重复切词时只算一次。
43
- * 返回的是**共享数组**——调用方只读,禁止原地修改(push/splice/sort)。
44
- *
45
- * @param {string} text 文件全文
46
- * @returns {Array<{type:string, value:string, line:number, col:number}>}
47
- * type: ident / punct / str / tmpl / comment / num / ws / other
48
- */
49
- export function tokenize(text = '') {
50
- const key = String(text);
51
- const cached = TOKEN_CACHE.get(key);
52
- if (cached) {
53
- // LRU 触达:删后重插使其移到队尾(Map 保持插入序,队首即最久未用)
54
- TOKEN_CACHE.delete(key);
55
- TOKEN_CACHE.set(key, cached);
56
- return cached;
57
- }
58
- const tokens = tokenizeUncached(text);
59
- TOKEN_CACHE.set(key, tokens);
60
- TOKEN_CACHE_BYTES += key.length;
61
- // 双上限(条数 + 字节):任一超限即淘汰最久未用(Map 首个键),直到都回到限内。
62
- // 保留至少 1 条:单个超大文件也不至于「刚缓存就被自己挤出」。
63
- while (TOKEN_CACHE.size > TOKEN_CACHE_MAX || (TOKEN_CACHE_BYTES > TOKEN_CACHE_MAX_BYTES && TOKEN_CACHE.size > 1)) {
64
- const oldest = TOKEN_CACHE.keys().next();
65
- if (oldest.done) break;
66
- TOKEN_CACHE_BYTES -= String(oldest.value).length;
67
- TOKEN_CACHE.delete(oldest.value);
68
- }
69
- return tokens;
70
- }
71
-
72
- /** 清空 tokenize 缓存(测试/长驻进程内存回收用)。 */
73
- export function clearTokenCache() {
74
- TOKEN_CACHE.clear();
75
- TOKEN_CACHE_BYTES = 0;
76
- }
77
-
78
- /**
79
- * tokenize 的实际实现(不做缓存)。外部一律走 tokenize()。
80
- * @param {string} text 文件全文
81
- * @returns {Array<{type:string, value:string, line:number}>} token 流
82
- */
83
- function tokenizeUncached(text = '') {
84
- const tokens = [];
85
- const lines = String(text).split('\n');
86
- let line = 1;
87
- // 块注释跨行状态(修):旧实现在**单行内**找 `*/`,多行块注释
88
- // (JSDoc `/** ... */`)的续行 ` * 说明 +1` 会被当成代码 → ident/num token 泄漏,
89
- // 导致注释里的版本号/数字被魔数等规则误报。改为跨行携带 inBlock 状态。
90
- let inBlock = false;
91
- // 模板字符串跨行状态:主循环按行 split,多行模板(如 cli.mjs 的 HELP 帮助文本)
92
- // 此前只把「开引号那一行」当模板,后续行整行被当代码 tokenize ⇒ 正文里的
93
- // 占位符 `[--depth N]` 产出 type:"ident" 的 `N`,被 readability/variable-min-length
94
- // 当短变量名误报(实测 3 条)。与三引号/块注释同模式:跨行携带状态直到闭合反引号。
95
- let inTemplate = false;
96
- // Python 三引号字符串跨行状态:主循环按行 split,
97
- // 多行 docstring("""…\n…""")若按行独立 tokenize 会在换行处被切断成两个
98
- // str + 中间内容被拆碎(数字/文本误报魔数/重复串)。跨行携带 tripleQuote
99
- // 状态:进入三引号后,后续行整行并入同一 str token,直到闭合三引号。
100
- let tripleQuote = '';
101
- let tripleBuf = '';
102
- for (const raw of lines) {
103
- const src = raw;
104
- let i = 0;
105
- const n = src.length;
106
- // col = token 起始列(1-based,行内)。用途:**列级精筛** —— 行级判定只能回答
107
- // 「这一行有没有代码」,回答不了「正则命中的那一处是不是落在字符串/注释里」
108
- // (实测遗留误报:`{ script: 'while (true) {}' }` 里的 while(true) 被当真实死循环)。
109
- // ⚠️ 跨行 token(三引号 / 多行块注释)的 col 是**续行内位置**、不是 token 真实起点 ⇒
110
- // 消费方必须跳过 value 含换行的 token(见 lib/ast/code-lines.js 的区间构造)。
111
- const push = (type, value) => tokens.push({ type, value, line, col: i + 1 });
112
- while (i < n) {
113
- const ch = src[i];
114
- const next = src[i + 1];
115
- // 三引号续行:整行剩余内容并入缓冲,直到找到闭合
116
- if (tripleQuote) {
117
- const end = src.indexOf(tripleQuote.repeat(3), i);
118
- if (end === -1) { tripleBuf += src.slice(i) + '\n'; i = n; break; }
119
- tripleBuf += src.slice(i, end);
120
- push('str', tripleQuote.repeat(3) + tripleBuf + tripleQuote.repeat(3));
121
- tripleQuote = ''; tripleBuf = '';
122
- i = end + 3;
123
- continue;
124
- }
125
- // 模板字符串续行:整行并入 tmpl,直到找到闭合反引号(多行模板不再泄漏为代码)
126
- if (inTemplate) {
127
- const end = src.indexOf('`', i);
128
- if (end === -1) { push('tmpl', src.slice(i)); i = n; break; }
129
- push('tmpl', src.slice(i, end + 1));
130
- inTemplate = false;
131
- i = end + 1;
132
- continue;
133
- }
134
- // 块注释续行:整行剩余内容都是注释(含 ` * xxx` 的星号续行)
135
- if (inBlock) {
136
- const end = src.indexOf('*/', i);
137
- if (end === -1) { push('comment', src.slice(i)); i = n; break; }
138
- push('comment', src.slice(i, end + 2));
139
- i = end + 2;
140
- inBlock = false;
141
- continue;
142
- }
143
- // 空白 / 行注释 / 块注释(跨行:本行找不到 `*/` 则整行剩余为注释并置 inBlock)
144
- if (/\s/.test(ch)) { i++; continue; }
145
- if (ch === '/' && next === '/') { push('comment', src.slice(i)); break; }
146
- // Python `#` 行注释:仅在**行首**(前面只可能有空白)识别——
147
- // JS 类私有字段 `#foo` 不在行首,CSS 选择器不在此 tokenizer;行首 `#` 是
148
- // Python 注释的确定形态(shebang `#!/usr/bin/env` 也算注释)。
149
- if (ch === '#' && /^\s*$/.test(src.slice(0, i))) { push('comment', src.slice(i)); break; }
150
- if (ch === '/' && next === '*') {
151
- const end = src.indexOf('*/', i + 2);
152
- if (end === -1) { push('comment', src.slice(i)); inBlock = true; i = n; break; }
153
- const seg = src.slice(i, end + 2);
154
- push('comment', seg);
155
- i += seg.length;
156
- continue;
157
- }
158
- // 三引号开始(""" 或 ''',):单行内闭合 → 整体消费为 str;
159
- // 跨行未闭合 → 设 tripleQuote 状态,整行及后续行并入缓冲
160
- if ((src.startsWith('"""', i) || src.startsWith("'''", i))) {
161
- const q = ch;
162
- const q3 = q.repeat(3);
163
- const end = src.indexOf(q3, i + 3);
164
- if (end === -1) {
165
- tripleQuote = q; tripleBuf = src.slice(i + 3) + '\n';
166
- i = n; break;
167
- }
168
- push('str', src.slice(i, end + 3));
169
- i = end + 3;
170
- continue;
171
- }
172
- // 字符串 / 模板字符串 / 正则 / 数字 / 标识符 / 运算符 —— 抽成独立消费函数
173
- const res = consumeLexeme(src, i, n, ch, next, tokens, push);
174
- if (res) {
175
- // 多行模板字符串:consumeTemplate 遇未闭合反引号只归并到行尾,这里补跨行状态,
176
- // 否则后续行会被当代码 tokenize(帮助文本里的 `[--depth N]` 曾因此报短变量名误报)
177
- const lastTok = tokens[tokens.length - 1];
178
- // 判据:首个反引号之后再无反引号 = 本行内未闭合。不能只看 endsWith——
179
- // `const css = ` 后面紧跟换行(反引号正好在行尾)时,token 值就是**单个反引号**,
180
- // endsWith('`') 为真 ⇒ 旧判据漏置跨行状态 ⇒ 模板正文(CSS/帮助文本)整段被当代码,
181
- // CSS 里的 #141518 / #888 / 1.5 被 readability/magic-number-smart 当魔数误报。
182
- // 判据补 startsWith('`'):C# 插值串产物同为 tmpl 但不含反引号,
183
- // 不收紧会把其后所有行并进模板(实测整段被吞成一个 tmpl token)。
184
- if (lastTok && lastTok.type === 'tmpl' && String(lastTok.value).startsWith('`')
185
- && String(lastTok.value).indexOf('`', 1) === -1) inTemplate = true;
186
- i = res; continue;
187
- }
188
- // 标点 / 其他(单字符)
189
- push('punct', ch);
190
- i++;
191
- }
192
- line++;
193
- }
194
- return tokens;
195
- }
196
-
197
- /** 多字符运算符(最长匹配优先, 修):旧实现逐字符推送标点,导致
198
- * `?.`(可选链)被切成 `?` + `.`、`??` 切成两个 `?`、`||` 切成两个 `|`
199
- * → 圈复杂度把每个可选链当三元运算符(严重高估),而 `||`/`&&`/`??` 反而
200
- * 识别不到(漏报)。这里按长度降序匹配;**可选链 `?.` 与空值合并 `??`
201
- * 必须切成不同 token**——前者不产生独立执行路径(不是分支),后者是分支。 */
202
- const MULTI_OPS = ['===', '!==', '**=', '<<=', '>>=', '...', '??=', '&&=', '||=', '?.', '??',
203
- '=>', '==', '!=', '<=', '>=', '&&', '||', '++', '--', '+=', '-=', '*=', '/=', '%=', '**', '<<', '>>', '::'];
204
-
205
- /** 多字符运算符**按首字符分桶**(性能,2026-10-09):原实现对每个标点都线性扫 26 个候选(startsWith),
206
- * 分桶后只扫同首字符的少数几个;**桶内保持 MULTI_OPS 原顺序** ⇒ 最长匹配语义完全不变。 */
207
- const MULTI_OPS_BY_FIRST = (() => {
208
- const m = new Map();
209
- for (const op of MULTI_OPS) {
210
- const k = op[0];
211
- if (!m.has(k)) m.set(k, []);
212
- m.get(k).push(op);
213
- }
214
- return m;
215
- })();
216
-
217
- /* 词素字符判定:**字符码查表**替代逐字符正则(性能,2026-10-09)。
218
- * 原实现对每个字符调用正则(/[0-9]/、/[0-9._a-zA-Z]/、/[A-Za-z_$\u4e00-\u9fa5]/ 等),
219
- * 逐字符建正则/跑正则的开销占 tokenizer 自耗时的大头(温跑 profile:consumeLexeme 9.8%)。
220
- * 三张 128 项 ASCII 表由**同样的正则**在模块加载时生成一次 ⇒ 判定集合逐字符等价;
221
- * 非 ASCII 只有 CJK 区间 \u4e00-\u9fa5 属于标识符(数字体集合里没有非 ASCII)。 */
222
- const ID_START_ASCII = new Uint8Array(128);
223
- const ID_PART_ASCII = new Uint8Array(128);
224
- const NUM_PART_ASCII = new Uint8Array(128);
225
- for (let c = 0; c < 128; c++) {
226
- const s = String.fromCharCode(c);
227
- ID_START_ASCII[c] = /[A-Za-z_$]/.test(s) ? 1 : 0;
228
- ID_PART_ASCII[c] = /[A-Za-z0-9_$]/.test(s) ? 1 : 0;
229
- NUM_PART_ASCII[c] = /[0-9._a-zA-Z]/.test(s) ? 1 : 0;
230
- }
231
- /** CJK 标识符区间(与原正则 \u4e00-\u9fa5 一致)。 */
232
- const isCjkChar = (c) => c >= 0x4e00 && c <= 0x9fa5;
233
- /** 是否标识符起始字符(等价于 /[A-Za-z_$\u4e00-\u9fa5]/)。 */
234
- const isIdentStartCode = (c) => (c < 128 ? ID_START_ASCII[c] === 1 : isCjkChar(c));
235
- /** 是否标识符后续字符(等价于 /[A-Za-z0-9_$\u4e00-\u9fa5]/)。 */
236
- const isIdentPartCode = (c) => (c < 128 ? ID_PART_ASCII[c] === 1 : isCjkChar(c));
237
- /** 是否数字体字符(等价于 /[0-9._a-zA-Z]/;该集合无非 ASCII 成员)。 */
238
- const isNumPartCode = (c) => c < 128 && NUM_PART_ASCII[c] === 1;
239
- /** 是否 ASCII 数字(等价于 /[0-9]/)。 */
240
- const isAsciiDigit = (c) => c >= 48 && c <= 57;
241
-
242
- /** 正则可接在哪些关键字后(return/typeof/case/in/of/new/delete/void/do/else/yield/await)。 */
243
- const REGEX_AFTER_KEYWORD = /^(?:return|typeof|case|in|of|new|delete|void|do|else|yield|await|instanceof)$/;
244
-
245
- /**
246
- * 消费一个词素(字符串/模板/正则/数字/标识符/运算符)。由 tokenizeUncached 主循环分派。
247
- * 返回消费后的新 i;不匹配(纯标点)返回 0。
248
- * 拆出的原因:tokenizeUncached 圈复杂度 61(阈值 15)——逐字符状态机
249
- * 分支密集,全部内联在 while 里无法维护;按词素类型抽独立函数,主循环只做分派。
250
- */
251
- function consumeLexeme(src, i, n, ch, next, tokens, push) { // dsh-skip-complexity: 词素分派器(六类词素分派分支为结构必然,拆分后职责单一)
252
- // C# 插值串($"…" / $@"…" / @$"…")→ tmpl,须先于普通字符串与标识符分支判定
253
- if (ch === '$' || ch === '@') {
254
- const cs = consumeCsharpInterpolated(src, i, n, push);
255
- if (cs) return cs;
256
- }
257
- // 字符串(' ")——普通单引号字符串;三引号(""" ''')由主循环专门处理
258
- if (ch === '"' || ch === "'") return consumeString(src, i, n, ch, push);
259
- // 模板字符串(`)——嵌套 ${} 简化处理:遇未闭合 ` 归并到行尾
260
- if (ch === '`') return consumeTemplate(src, i, n, push);
261
- // 正则字面量(/…/flags)——修:旧实现不识别正则,把 `/^\s*(?:x|y)/` 拆成
262
- // `/` `^` `\` `s` `*` `(` `?` `:` `|` `)` 等标点,其中的 `(` `)` `{` `}` 直接参与
263
- // 括号平衡,导致 matchBrace 跑飞:单个 12 行函数被算成 242 行(func-lines 误报 blocker)。
264
- // 判别依据:`/` 出现在「表达式起始位置」才是正则——即上一个有意义 token 不是
265
- // 标识符/数字/字符串/`)`/`]`/`}`(那些后面跟的 `/` 是除号)。
266
- if (ch === '/' && next !== '/' && next !== '*') {
267
- const consumed = consumeRegex(src, i, n, tokens, push);
268
- if (consumed) return consumed;
269
- }
270
- // 数字(字符码判定,等价于原先的 /[0-9]/.test(ch) 与 /[0-9._a-zA-Z]/.test(src[j]))
271
- const c0 = src.charCodeAt(i);
272
- if (isAsciiDigit(c0) || (ch === '.' && isAsciiDigit(next ? next.charCodeAt(0) : -1))) {
273
- let j = i;
274
- while (j < n && isNumPartCode(src.charCodeAt(j))) j++;
275
- push('num', src.slice(i, j));
276
- return j;
277
- }
278
- // 标识符(含 $ _ 中文;字符码判定,等价于 /[A-Za-z_$\u4e00-\u9fa5]/ 与 /[A-Za-z0-9_$\u4e00-\u9fa5]/)
279
- if (isIdentStartCode(c0)) {
280
- let j = i;
281
- while (j < n && isIdentPartCode(src.charCodeAt(j))) j++;
282
- push('ident', src.slice(i, j));
283
- return j;
284
- }
285
- // 多字符运算符(最长匹配优先;按首字符分桶,桶内顺序与原数组一致)
286
- const cands = MULTI_OPS_BY_FIRST.get(ch);
287
- if (cands) {
288
- for (const cand of cands) {
289
- if (src.startsWith(cand, i)) { push('punct', cand); return i + cand.length; }
290
- }
291
- }
292
- return 0;
293
- }
294
-
295
- /**
296
- * C# 插值串(`$"…"` / `$@"…"` / `@$"…"`)—— 产出 `tmpl` 类型。
297
- *
298
- * 为什么:插值串里的 `{expr}` 是**表达式**而不是文本。若按普通字符串(`str`)处理,
299
- * 「重复硬编码文本」统计会把表达式当成硬编码值参与计数(C# 语料实测 10/41 由此误报,
300
- * 如 `$"<error: {ex.Message}>"` 被提成 `<error: {ex.Message}>`)。它在语义上与 JS 模板串
301
- * 同类,故复用既有 `tmpl` 口径(该规则的提取器已刻意排除 `tmpl`)。
302
- * 边界:
303
- * · `@"…"`(verbatim 但**不插值**)是纯字面量,仍按 `str`,不在此处理;
304
- * · verbatim 串内连续两个 `""` 表示一个转义引号,不构成闭合;
305
- * · 插值洞 `{…}` 里可以再出现字符串(`$"{(c ? a : "null")}, "`),洞内的 `"` **不**结束
306
- * 整个插值串,故必须按花括号深度判定——否则会在洞内第一个引号处截断,残留 `)}, "` 片段
307
- * 又被当成普通字符串参与统计(实测该形态仍误报);
308
- * · 转义的大括号 `{{` / `}}` 是字面量括号,不改变深度。
309
- * @returns {number} 消费后的新下标;非插值串返回 0(交其它词素处理)
310
- */
311
- function consumeCsharpInterpolated(src, i, n, push) { // dsh-skip-complexity: 三前缀组合 + 洞内嵌套串 + verbatim 转义
312
- const three = src.slice(i, i + 3);
313
- const two = src.slice(i, i + 2);
314
- let quoteAt = -1;
315
- if (three === '$@"' || three === '@$"') quoteAt = i + 2;
316
- else if (two === '$"') quoteAt = i + 1;
317
- if (quoteAt < 0) return 0;
318
- const verbatim = src.slice(i, quoteAt).includes('@');
319
- let j = quoteAt + 1;
320
- let str = src.slice(i, quoteAt + 1);
321
- let depth = 0;
322
- const pushChar = () => { str += src[j]; j++; };
323
- while (j < n) {
324
- const ch = src[j];
325
- if (depth > 0) {
326
- // 洞内:花括号调节深度;字符串整段跳过(不参与闭合判定)
327
- if (ch === '"') {
328
- pushChar();
329
- while (j < n) {
330
- if (src[j] === '\\') { pushChar(); if (j < n) pushChar(); continue; }
331
- const c2 = src[j];
332
- pushChar();
333
- if (c2 === '"') break;
334
- }
335
- continue;
336
- }
337
- if (ch === '{') { depth++; pushChar(); continue; }
338
- if (ch === '}') { depth--; pushChar(); continue; }
339
- pushChar();
340
- continue;
341
- }
342
- if (!verbatim && ch === '\\') { pushChar(); if (j < n) pushChar(); continue; }
343
- if (ch === '{') {
344
- if (src[j + 1] === '{') { pushChar(); pushChar(); continue; } // {{ 转义,不计深度
345
- depth++; pushChar(); continue;
346
- }
347
- if (ch === '}') {
348
- if (src[j + 1] === '}') { pushChar(); pushChar(); continue; } // }} 转义,不计深度
349
- pushChar(); continue;
350
- }
351
- if (ch === '"') {
352
- if (verbatim && src[j + 1] === '"') { pushChar(); pushChar(); continue; }
353
- pushChar();
354
- break;
355
- }
356
- pushChar();
357
- }
358
- push('tmpl', str);
359
- return j;
360
- }
361
-
362
- /** 字符串字面量(' ")——遇转义 \ 跳两字符;未闭合归并到行尾。 */
363
- function consumeString(src, i, n, ch, push) {
364
- let j = i + 1;
365
- let str = ch;
366
- while (j < n) {
367
- if (src[j] === '\\') { str += src[j] + (src[j + 1] || ''); j += 2; continue; }
368
- str += src[j];
369
- if (src[j] === ch) { j++; break; }
370
- j++;
371
- }
372
- push('str', str);
373
- return j;
374
- }
375
-
376
- /** 模板字符串(`)——遇 \ 跳两字符;未闭合归并到行尾(简单 ${} 处理)。 */
377
- function consumeTemplate(src, i, n, push) {
378
- let j = i + 1;
379
- let str = '`';
380
- while (j < n && src[j] !== '`') {
381
- if (src[j] === '\\') { str += src[j] + (src[j + 1] || ''); j += 2; continue; }
382
- str += src[j];
383
- j++;
384
- }
385
- if (j < n) { str += '`'; j++; }
386
- push('tmpl', str);
387
- return j;
388
- }
389
-
390
- /** 正则字面量(/…/flags);`/` 前一 token 是值表达式时为除号,返回 0 交给标点处理。 */
391
- function consumeRegex(src, i, n, tokens, push) { // dsh-skip-complexity: 正则状态机(转义/字符组/闭包三态,天然多分支)
392
- const prevTok = tokens.length ? tokens[tokens.length - 1] : null;
393
- const prevIsValue = prevTok && (
394
- prevTok.type === 'ident' || prevTok.type === 'num' || prevTok.type === 'str'
395
- || prevTok.type === 'tmpl' || prevTok.type === 'regex'
396
- || (prevTok.type === 'punct' && [')', ']', '}'].includes(prevTok.value))
397
- );
398
- // 关键字后(return/typeof/case/in/of/new/delete/void/do/else/yield/await)可接正则
399
- const afterKeyword = prevTok && prevTok.type === 'ident' && REGEX_AFTER_KEYWORD.test(prevTok.value);
400
- if (prevIsValue && !afterKeyword) return 0;
401
- let j = i + 1;
402
- let re = '/';
403
- let inClass = false; // 字符组 [ ] 内的 `/` 不结束正则
404
- let closed = false;
405
- while (j < n) {
406
- const cj = src[j];
407
- if (cj === '\\') { re += cj + (src[j + 1] || ''); j += 2; continue; }
408
- if (cj === '\n') break; // 正则不能跨行,未闭合则放弃
409
- if (cj === '[') inClass = true;
410
- else if (cj === ']') inClass = false;
411
- re += cj;
412
- if (cj === '/' && !inClass) { closed = true; j++; break; }
413
- j++;
414
- }
415
- if (!closed) return 0;
416
- // 跟随的 flags(g/i/m/s/u/y/d)
417
- while (j < n && /[gimsuy]/.test(src[j])) { re += src[j]; j++; }
418
- push('regex', re);
419
- return j;
420
- }
421
-