dsh-git-push 2.20.19 → 3.1.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.
- package/README.md +76 -47
- package/cli-tool.mjs +2 -4
- package/cli.mjs +11 -4
- package/lib/app/apply.js +15 -13
- package/lib/app/command-registry.js +26 -19
- package/lib/app/handlers/http-constants.js +8 -0
- package/lib/app/handlers/meta.js +2 -2
- package/lib/app/handlers/preprocess.js +104 -0
- package/lib/app/handlers/repo-actions.js +2 -3
- package/lib/app/handlers/repos.js +137 -10
- package/lib/app/handlers/settings.js +34 -15
- package/lib/app/http-handlers.js +13 -42
- package/lib/app/index.js +3 -2
- package/lib/app/inject-text.js +6 -21
- package/lib/app/preprocess-client.js +136 -0
- package/lib/app/repo-ownership.js +1 -1
- package/lib/app/schema.js +20 -10
- package/lib/app/settings-bridge.js +60 -8
- package/lib/app/severity.js +14 -0
- package/lib/app/slash-commands.js +5 -164
- package/lib/app/tool-call.js +96 -163
- package/lib/cli/commands-preprocess.mjs +74 -0
- package/lib/cli/commands-tool.mjs +3 -2
- package/lib/cli/commands-vcs.mjs +39 -5
- package/lib/cli/shared.mjs +36 -10
- package/lib/client/index.js +11 -16
- package/lib/client-parts/git/account.js +21 -12
- package/lib/client-parts/git/actions.js +23 -77
- package/lib/client-parts/git/apply.js +8 -2
- package/lib/client-parts/git/browser.js +1 -1
- package/lib/client-parts/git/const.js +33 -1
- package/lib/client-parts/git/controller.js +175 -176
- package/lib/client-parts/git/icons.js +1 -1
- package/lib/client-parts/git/loader.js +1 -1
- package/lib/client-parts/git/npm.js +56 -37
- package/lib/client-parts/git/page.js +53 -1
- package/lib/client-parts/git/repo.js +8 -1
- package/lib/client-parts/git/settings.js +13 -1
- package/lib/client-parts/git/state.js +237 -0
- package/lib/client-parts/git/utils.js +1 -1
- package/lib/client.js +631 -312
- package/lib/commit-push/pre-commit-engine.js +629 -30
- package/lib/commit-push.js +127 -67
- package/lib/context/index.js +7 -5
- package/lib/core/config.js +1 -27
- package/lib/core/exec.js +2 -11
- package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
- package/lib/git/identity-rewrite.js +1 -1
- package/lib/git/ignore.js +1 -1
- package/lib/git/index.js +1 -1
- package/lib/git/push.js +2 -17
- package/lib/git/repo-index.js +8 -3
- package/lib/git/repos.js +2 -2
- package/lib/git/sensitive.js +1 -1
- package/lib/npm/install.js +1 -1
- package/lib/npm/publish.js +20 -5
- package/lib/npm/registry.js +2 -2
- package/lib/plugin/auto-push.js +5 -8
- package/lib/preprocess/providers.js +195 -0
- package/lib/self/index.js +1 -1
- package/lib/skip-dirs.js +14 -55
- package/package.json +31 -42
- package/skills/dev/dsh-git-push.md +9 -9
- package/skills/guide/guide-commit-push.md +11 -13
- package/skills/workflow/README.md +0 -1
- package/skills/workflow/task-completion-report.md +2 -2
- package/.auditignore +0 -30
- package/.gitignore +0 -43
- package/cli-audit.mjs +0 -141
- package/lib/BUGFIX-NOTES-2026-09-14.md +0 -278
- package/lib/app/audit-api.js +0 -173
- package/lib/app/handlers/audit.js +0 -150
- package/lib/app/host-doc-checks.js +0 -25
- package/lib/app/schema-audit.js +0 -42
- package/lib/app/scope-check.js +0 -69
- package/lib/ast/brace.js +0 -120
- package/lib/ast/callgraph.js +0 -135
- package/lib/ast/code-lines.js +0 -175
- package/lib/ast/consts.js +0 -12
- package/lib/ast/control-flow.js +0 -470
- package/lib/ast/credential.js +0 -315
- package/lib/ast/dataflow.js +0 -391
- package/lib/ast/dup-code.js +0 -165
- package/lib/ast/fs-hotspots.js +0 -154
- package/lib/ast/index.js +0 -38
- package/lib/ast/io-risk-const.js +0 -95
- package/lib/ast/io-risk-fn.js +0 -208
- package/lib/ast/io-risk-loop.js +0 -308
- package/lib/ast/io-risk.js +0 -314
- package/lib/ast/lang.js +0 -156
- package/lib/ast/line-count.js +0 -52
- package/lib/ast/magic-number.js +0 -272
- package/lib/ast/naming.js +0 -292
- package/lib/ast/perf.js +0 -488
- package/lib/ast/scope-collisions.js +0 -228
- package/lib/ast/scope.js +0 -241
- package/lib/ast/shell.js +0 -131
- package/lib/ast/size.js +0 -448
- package/lib/ast/symbol-index.js +0 -399
- package/lib/ast/tokenizer.js +0 -421
- package/lib/ast/treesitter.js +0 -413
- package/lib/audit/analysis-coverage.js +0 -98
- package/lib/audit/audit-file.js +0 -205
- package/lib/audit/auditignore-rules.js +0 -148
- package/lib/audit/checks.js +0 -17
- package/lib/audit/client-entry.js +0 -93
- package/lib/audit/collector.js +0 -610
- package/lib/audit/ext-runner.js +0 -80
- package/lib/audit/file-context.js +0 -391
- package/lib/audit/finding.js +0 -101
- package/lib/audit/gitignore-match.js +0 -113
- package/lib/audit/glob.js +0 -63
- package/lib/audit/history-report.js +0 -100
- package/lib/audit/history.js +0 -126
- package/lib/audit/ignore-blind.js +0 -102
- package/lib/audit/index.js +0 -33
- package/lib/audit/orchestrate.js +0 -672
- package/lib/audit/public.js +0 -24
- package/lib/audit/repo-level.js +0 -30
- package/lib/audit/report-yaml.js +0 -90
- package/lib/audit/scope-policy.js +0 -110
- package/lib/audit/slot.js +0 -66
- package/lib/audit-rules/audit-rules-comment.yml +0 -199
- package/lib/audit-rules/audit-rules-cpp.yml +0 -114
- package/lib/audit-rules/audit-rules-csharp.yml +0 -82
- package/lib/audit-rules/audit-rules-docs.yml +0 -32
- package/lib/audit-rules/audit-rules-dsh.yml +0 -177
- package/lib/audit-rules/audit-rules-filehealth.yml +0 -42
- package/lib/audit-rules/audit-rules-folder.yml +0 -99
- package/lib/audit-rules/audit-rules-frontend.yml +0 -335
- package/lib/audit-rules/audit-rules-go.yml +0 -103
- package/lib/audit-rules/audit-rules-i18n.yml +0 -45
- package/lib/audit-rules/audit-rules-java.yml +0 -105
- package/lib/audit-rules/audit-rules-nodejs.yml +0 -662
- package/lib/audit-rules/audit-rules-npm.yml +0 -187
- package/lib/audit-rules/audit-rules-paths.yml +0 -98
- package/lib/audit-rules/audit-rules-performance.yml +0 -245
- package/lib/audit-rules/audit-rules-php.yml +0 -101
- package/lib/audit-rules/audit-rules-private.yml +0 -24
- package/lib/audit-rules/audit-rules-python.yml +0 -106
- package/lib/audit-rules/audit-rules-robustness.yml +0 -31
- package/lib/audit-rules/audit-rules-rust.yml +0 -109
- package/lib/audit-rules/audit-rules-structure.yml +0 -158
- package/lib/audit-rules/audit-rules-swift.yml +0 -99
- package/lib/audit-rules/audit-rules-template.yml +0 -108
- package/lib/audit-ui-sdk/contract.js +0 -117
- package/lib/audit-ui-sdk/index.js +0 -17
- package/lib/checks/button-bind.js +0 -347
- package/lib/checks/common.js +0 -170
- package/lib/checks/credential-file.js +0 -43
- package/lib/checks/dataflow.js +0 -40
- package/lib/checks/dispatch.js +0 -117
- package/lib/checks/dup-code.js +0 -69
- package/lib/checks/dup-const.js +0 -232
- package/lib/checks/file-health.js +0 -150
- package/lib/checks/filter.js +0 -137
- package/lib/checks/folder.js +0 -173
- package/lib/checks/fs-scanner.js +0 -57
- package/lib/checks/hardcoded-path.js +0 -98
- package/lib/checks/index.js +0 -49
- package/lib/checks/io.js +0 -66
- package/lib/checks/magic-number.js +0 -42
- package/lib/checks/npm-json.js +0 -273
- package/lib/checks/private.js +0 -64
- package/lib/checks/regex.js +0 -298
- package/lib/checks/scope-collision.js +0 -68
- package/lib/checks/semantic.js +0 -134
- package/lib/checks/structural.js +0 -281
- package/lib/cli/commands-audit.mjs +0 -142
- package/lib/cli/commands-doc.mjs +0 -127
- package/lib/client-parts/audit/audit.js +0 -301
- package/lib/exempt/tool.js +0 -198
- package/lib/git/module-splitter.js +0 -47
- package/lib/link-check/index.js +0 -187
- package/lib/lint-adapter/detect.js +0 -161
- package/lib/lint-adapter/index.js +0 -50
- package/lib/lint-adapter/runner.js +0 -133
- package/lib/preprocess/audit.js +0 -98
- package/lib/preprocess/index.js +0 -78
- package/lib/rule/compilers/credential.js +0 -62
- package/lib/rule/compilers/dataflow.js +0 -38
- package/lib/rule/compilers/file-health.js +0 -75
- package/lib/rule/compilers/frontend.js +0 -86
- package/lib/rule/compilers/func.js +0 -35
- package/lib/rule/compilers/helpers.js +0 -71
- package/lib/rule/compilers/index.js +0 -29
- package/lib/rule/compilers/link-semantic.js +0 -45
- package/lib/rule/compilers/list-kinds.js +0 -12
- package/lib/rule/compilers/numeric-kinds.js +0 -73
- package/lib/rule/compilers/regex.js +0 -78
- package/lib/rule/compilers/structure.js +0 -107
- package/lib/rule/compilers.js +0 -17
- package/lib/rule/homoglyph.js +0 -45
- package/lib/rule/loader.js +0 -274
- package/lib/rule/registry.js +0 -122
- package/lib/rule/scope.js +0 -67
- package/lib/score/docs-score.js +0 -166
- package/lib/score/index.js +0 -234
- package/lib/user-requirements.json +0 -15
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +0 -34
- package/scripts/audit-ext/dsh-inject-declarations.mjs +0 -209
- package/scripts/audit-ext/variable-min-length.mjs +0 -102
- package/scripts/audit-gate.mjs +0 -235
- package/scripts/audit-runner.mjs +0 -35
- package/scripts/audit-runtime-check.mjs +0 -129
- package/scripts/browser-page-probe.mjs +0 -271
- package/scripts/build-client.mjs +0 -131
- package/scripts/bump-version.mjs +0 -178
- package/scripts/check-js-scope.mjs +0 -87
- package/scripts/check.mjs +0 -203
- package/scripts/clean-date-comments.py +0 -102
- package/scripts/cpu-sample.mjs +0 -188
- package/scripts/doc-drift.mjs +0 -200
- package/scripts/doc-func.mjs +0 -212
- package/scripts/doc-tree.mjs +0 -458
- package/scripts/doc-version.mjs +0 -149
- package/scripts/hot-path-probe-cjs.cjs +0 -71
- package/scripts/hot-path-probe-fs-shim.mjs +0 -28
- package/scripts/hot-path-probe-fsp-shim.mjs +0 -31
- package/scripts/hot-path-probe.mjs +0 -153
- package/scripts/local-registry.mjs +0 -37
- package/scripts/module-splitter.py +0 -800
- package/scripts/picker-repro.mjs +0 -115
- package/scripts/platform-scan.mjs +0 -168
- package/scripts/probe-recheck.mjs +0 -47
- package/scripts/rename-locator.mjs +0 -97
- package/scripts/rule-switch.mjs +0 -97
- package/scripts/rules-solo-audit.mjs +0 -173
- package/scripts/scan-file-io.mjs +0 -717
- package/scripts/scan-fs-hotspots.mjs +0 -107
- package/scripts/scan-version.mjs +0 -125
- package/scripts/symbol-index.mjs +0 -85
- package/scripts/verify-prestep.mjs +0 -100
- package/scripts/version-source.mjs +0 -173
- package/skills/dev/audit-rule-authoring.md +0 -58
- package/skills/guide/guide-audit.md +0 -270
- /package/lib/{audit → app}/ownership.js +0 -0
- /package/lib/{audit-defaults.js → git/defaults.js} +0 -0
package/lib/ast/naming.js
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 命名与读取检查
|
|
3
|
-
*
|
|
4
|
-
* 职责:标识符长度、函数名是否过短(含 i18n 缩写豁免)、受控小文件读取判定。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { tokenize } from './tokenizer.js';
|
|
8
|
-
import { shortFunctionNamesFromTree } from './treesitter.js';
|
|
9
|
-
|
|
10
|
-
/** 声明/控制流关键字(Java「类型 短名」分支须排除的常见语言关键字前缀)——
|
|
11
|
-
* prevT 是这些时不是「类型名」,`短名` 属各语言声明/流程语境而非 Java 变量声明。
|
|
12
|
-
* const/let/var=JS、val=Kotlin、return=流程、function/def/class=声明、import/from=导入、
|
|
13
|
-
* for/in/of=循环头、case=switch、new=构造、throw=抛错。 */
|
|
14
|
-
const DECL_KEYWORDS = new Set([
|
|
15
|
-
'const', 'let', 'var', 'val', 'return', 'function', 'def', 'class', 'import', 'from',
|
|
16
|
-
'for', 'in', 'of', 'case', 'new', 'throw', 'while', 'if', 'else', 'switch', 'catch',
|
|
17
|
-
'typeof', 'void', 'delete', 'yield', 'await', 'extends', 'super', 'this', 'static',
|
|
18
|
-
]);
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 检查函数/变量命名长度不足(min-length kind,AST 级)。
|
|
22
|
-
* 覆盖:function 声明名的长度、参数名单字母、变量声明名单字母。
|
|
23
|
-
* @param {string} text 文件全文
|
|
24
|
-
* @param {object} [opts] { min=2, allow=['i','j','k','x','y','z','e','_','$','a','b','n','p','s','v','f','t','r','id','ok','db','fs','os','el','cb','err','req','res','ctx','args' } }
|
|
25
|
-
* @returns {Array<{line:number, name:string, type:string}>}
|
|
26
|
-
*/
|
|
27
|
-
export function checkNameLengthAst(text = '', { min = 2, allow = [] } = {}) {
|
|
28
|
-
const tokens = tokenize(text);
|
|
29
|
-
const out = [];
|
|
30
|
-
const allowSet = new Set(['i', 'j', 'k', 'x', 'y', 'z', 'e', '_', '$', 'a', 'b', 'n', 'p', 's', 'v', 'f', 't', 'r',
|
|
31
|
-
'id', 'ok', 'db', 'fs', 'os', 'el', 'cb', 'err', 'req', 'res', 'ctx', 'args', 'key', 'val', 'fn', 'ns', ...allow,
|
|
32
|
-
// i18n/字典领域标准缩写——tr/L 是翻译与字典加载的通用命名(对标
|
|
33
|
-
// react-i18next 的 t、i18n 字典 dict),不是「无法猜出含义」的缩写。
|
|
34
|
-
// 小写 l 与大写 L 同语义(line/字典加载),此前只列了大写 → 同一写法两种待遇(自审实测命中)。
|
|
35
|
-
'tr', 'L', 'l', 'i18n', 'dict', 'locale']);
|
|
36
|
-
// 窄作用域阈值:短名所在大括号块的行跨度 ≤ 该值时视为「惯用局部别名」不报(见下方声明分支)。
|
|
37
|
-
// 取 8 的依据:实测 dsh-session-conductor 的 20 条业务层命中全部落在 3–6 行的紧邻块内,
|
|
38
|
-
// 而真正难读的是「跨几十行仍在用的短名」——8 行既放行惯用法、又不放过长作用域。
|
|
39
|
-
const SCOPE_LINES = 8;
|
|
40
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
41
|
-
const t = tokens[i];
|
|
42
|
-
// 函数声明:function 后的 ident
|
|
43
|
-
if (t.type === 'ident' && t.value === 'function') {
|
|
44
|
-
const nx = tokens[i + 1];
|
|
45
|
-
if (nx && nx.type === 'ident' && nx.value.length < min && !allowSet.has(nx.value)) {
|
|
46
|
-
out.push({ line: nx.line, name: nx.value, type: 'function' });
|
|
47
|
-
}
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
// 参数列表内的短名(function f(a, b) / (a, b) => 的形参或调用实参位置)——短函数参数可接受(作用域 P2)
|
|
51
|
-
if (t.type === 'ident' && t.value.length < min && !allowSet.has(t.value)) {
|
|
52
|
-
const prevT = tokens[i - 1];
|
|
53
|
-
const nextT = tokens[i + 1];
|
|
54
|
-
const inParams = prevT && ((prevT.type === 'punct' && (prevT.value === '(' || prevT.value === ',')))
|
|
55
|
-
&& nextT && (nextT.type === 'punct' && (nextT.value === ',' || nextT.value === ')'));
|
|
56
|
-
if (inParams) continue;
|
|
57
|
-
}
|
|
58
|
-
// 变量声明:const/let/var 后的 ident
|
|
59
|
-
if (t.type === 'ident' && ['const', 'let', 'var'].includes(t.value)) {
|
|
60
|
-
const nx = tokens[i + 1];
|
|
61
|
-
// 循环迭代变量豁免(for (const d of densityHits) / for (let i in obj) 的
|
|
62
|
-
// 单字母是强语义局部变量——作用域仅循环体、语义由被遍历集合决定,不属「看不懂含义」。
|
|
63
|
-
if (nx && nx.type === 'ident' && nx.value.length < min && !allowSet.has(nx.value)) {
|
|
64
|
-
// token 序列:for (const d of ... —— const 前一 token 是 '(',再前是 'for'
|
|
65
|
-
const prevP = tokens[i - 1];
|
|
66
|
-
const prev2 = tokens[i - 2];
|
|
67
|
-
const inForHead = prevP && prevP.type === 'punct' && prevP.value === '('
|
|
68
|
-
&& prev2 && prev2.type === 'ident' && prev2.value === 'for';
|
|
69
|
-
// 作用域分析(新增):短名若只落在**很窄**的作用域里(声明所在大括号块的行跨度
|
|
70
|
-
// ≤ SCOPE_LINES),属惯用局部别名(c=chunk / h=header / m=method / d=data / q=query),
|
|
71
|
-
// 含义由紧邻的上下文决定,不影响可读性 ⇒ 不报;只有跨越大段代码仍用短名才真的难看懂。
|
|
72
|
-
// 实测 dsh-session-conductor 的 20 条业务层命中全部落在 3–6 行的紧邻块内
|
|
73
|
-
// (声明与最后一次使用相距 ≤ 4 行),而它们此前全被报成「应可读命名」。
|
|
74
|
-
// 用**大括号栈**定位声明所在块(而不是「同名最后一次出现」——同文件里可能有另一个
|
|
75
|
-
// 同名的短别名,那样会把它误算成同一作用域、导致该报的不报/该放行的报)。
|
|
76
|
-
let scopeEndLine = nx.line;
|
|
77
|
-
{
|
|
78
|
-
// 向前找声明所在的最内层 '{'(跳过字符串/注释——tokenizer 已把它们单独分类)
|
|
79
|
-
let depth = 0;
|
|
80
|
-
let openIdx = -1;
|
|
81
|
-
for (let k = i - 1; k >= 0; k--) {
|
|
82
|
-
const tk = tokens[k];
|
|
83
|
-
if (tk.type !== 'punct') continue;
|
|
84
|
-
if (tk.value === '}') depth++;
|
|
85
|
-
else if (tk.value === '{') {
|
|
86
|
-
if (depth === 0) { openIdx = k; break; }
|
|
87
|
-
depth--;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (openIdx !== -1) {
|
|
91
|
-
// 从该 '{' 向后配对到它的 '}',其行号即作用域结束
|
|
92
|
-
let d2 = 0;
|
|
93
|
-
for (let k = openIdx; k < tokens.length; k++) {
|
|
94
|
-
const tk = tokens[k];
|
|
95
|
-
if (tk.type !== 'punct') continue;
|
|
96
|
-
if (tk.value === '{') d2++;
|
|
97
|
-
else if (tk.value === '}') {
|
|
98
|
-
d2--;
|
|
99
|
-
if (d2 === 0) { scopeEndLine = tk.line; break; }
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const narrowScope = (scopeEndLine - nx.line) <= SCOPE_LINES;
|
|
105
|
-
// 降级而非丢弃(按需求):窄作用域里的短别名**仍要报出来**(聚合结果里能查到位置、
|
|
106
|
-
// 便于人工核对),但带 narrowScope 标记 ⇒ 上游把它降为 notice(不计警告、不影响评分)。
|
|
107
|
-
// 宽作用域仍按 warning 报。
|
|
108
|
-
if (!inForHead) out.push({ line: nx.line, name: nx.value, type: 'variable', narrowScope });
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// ── Python 语法支持(python/variable-min-length 等规则生效)──
|
|
112
|
-
// Python 无 const/let/var/function:函数声明是 `def name(`,变量声明是 `name = 值`。
|
|
113
|
-
// a) def 后的函数名(ident 'def' 下一个非空白 ident 且后跟 '(')
|
|
114
|
-
if (t.type === 'ident' && t.value === 'def') {
|
|
115
|
-
const nx = tokens[i + 1];
|
|
116
|
-
const after = tokens[i + 2];
|
|
117
|
-
if (nx && nx.type === 'ident' && nx.value.length < min && !allowSet.has(nx.value)
|
|
118
|
-
&& after && after.type === 'punct' && after.value === '(') {
|
|
119
|
-
out.push({ line: nx.line, name: nx.value, type: 'function' });
|
|
120
|
-
}
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
// b) `name = ` 变量声明(等号前是短 ident;排除 import/from/def/class/return/
|
|
124
|
-
// for/if/elif/else/while 关键字语境——这些不是「变量声明」;
|
|
125
|
-
// 并排除 const/let/var 前缀——JS 声明已由上方 JS 分支处理,避免二次报)
|
|
126
|
-
if (t.type === 'ident' && t.value.length < min && !allowSet.has(t.value)) {
|
|
127
|
-
const prevT = tokens[i - 1];
|
|
128
|
-
const prev2 = tokens[i - 2];
|
|
129
|
-
const nextT = tokens[i + 1];
|
|
130
|
-
if (nextT && nextT.type === 'punct' && nextT.value === '=') {
|
|
131
|
-
// 排除:import x / from x / return x / for x in / class x / def x / const x =
|
|
132
|
-
// (const/let/var 前缀的 JS 声明由 JS 分支管;这里是 Python 无关键字声明)
|
|
133
|
-
const KW = new Set(['import', 'from', 'return', 'for', 'class', 'def', 'if', 'elif', 'else', 'while', 'in', 'as', 'with', 'lambda', 'del', 'pass', 'not', 'and', 'or', 'global', 'nonlocal', 'yield', 'assert', 'raise', 'is', 'None', 'True', 'False']);
|
|
134
|
-
const prevIsDecl = prevT && prevT.type === 'ident' && ['const', 'let', 'var'].includes(prevT.value);
|
|
135
|
-
const prev2IsDecl = prev2 && prev2.type === 'ident' && ['const', 'let', 'var'].includes(prev2.value);
|
|
136
|
-
if (!KW.has(t.value) && !prevIsDecl && !prev2IsDecl
|
|
137
|
-
&& prevT && !(prevT.type === 'punct' && prevT.value === '.')) {
|
|
138
|
-
out.push({ line: t.line, name: t.value, type: 'variable' });
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// ── Java/Kotlin 语法支持(java/variable-min-length 等规则生效)──
|
|
143
|
-
// Java/Kotlin 无 const/let/var/function/def:变量声明是「类型 短名」形态
|
|
144
|
-
// (int x / String s / int[] arr / List<String> list),方法参数、字段、局部变量
|
|
145
|
-
// 都如此。识别:短 ident 后跟 `=` 或 `;` 或 `,` 或 `)`,且前一是「类型段末尾」
|
|
146
|
-
// (ident / `>` 泛型尾 / `]` 数组尾)——即 `类型 短名` 声明形态。
|
|
147
|
-
if (t.type === 'ident' && t.value.length < min && !allowSet.has(t.value)) {
|
|
148
|
-
const prevT = tokens[i - 1];
|
|
149
|
-
const nextT0 = tokens[i + 1];
|
|
150
|
-
const nextT1 = tokens[i + 2];
|
|
151
|
-
// 声明判定:前一是类型段(ident/`>`/`]`),后一可能是 `=`(初始化)、
|
|
152
|
-
// `;`(纯声明)、`,`(多声明/参数)、`)`(参数尾)、`]`(数组长度)
|
|
153
|
-
const prevIsTypeEnd = prevT && (
|
|
154
|
-
prevT.type === 'ident'
|
|
155
|
-
|| (prevT.type === 'punct' && (prevT.value === '>' || prevT.value === ']'))
|
|
156
|
-
);
|
|
157
|
-
const nextIsDecl = nextT0 && (
|
|
158
|
-
(nextT0.type === 'punct' && ['=', ';', ',', ')', ']', ':'].includes(nextT0.value))
|
|
159
|
-
|| (nextT0.type === 'ident' && nextT1 && nextT1.type === 'punct' && nextT1.value === '=')
|
|
160
|
-
);
|
|
161
|
-
if (prevIsTypeEnd && nextIsDecl && !(prevT.type === 'ident' && prevT.value === 'fun')) {
|
|
162
|
-
// 排除声明/控制流关键字前缀(修复 Java 分支误伤 JS):
|
|
163
|
-
// `const c = 1`(JS)、`val/var x =`(Kotlin)、`return q`、`function`、
|
|
164
|
-
// Python `def`/`import` 后接短名等——prevT 是 ident 但非「类型名」,
|
|
165
|
-
// 前一是这些关键字时是各语言声明/流程语境,不是「类型 短名」声明。
|
|
166
|
-
// 实测误伤:Pawchive test/tools 大量 `const c/h/m = …` 被 Java 分支当
|
|
167
|
-
// Java 变量报短名(内置 min-length 27 条 + ext 部分全为此根因)。
|
|
168
|
-
if (prevT.type === 'ident' && DECL_KEYWORDS.has(prevT.value)) continue;
|
|
169
|
-
out.push({ line: t.line, name: t.value, type: 'variable' });
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
// Kotlin 函数声明:fun name( —— name 前的 `fun` 是声明关键字
|
|
173
|
-
if (t.type === 'ident' && t.value === 'fun') {
|
|
174
|
-
const nx = tokens[i + 1];
|
|
175
|
-
const after = tokens[i + 2];
|
|
176
|
-
if (nx && nx.type === 'ident' && nx.value.length < min && !allowSet.has(nx.value)
|
|
177
|
-
&& after && after.type === 'punct' && after.value === '(') {
|
|
178
|
-
out.push({ line: nx.line, name: nx.value, type: 'function' });
|
|
179
|
-
}
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
// 多语言分支可能对同一标识符重复命中(如 Java `int w = 5` 的 w 同时命中
|
|
184
|
-
// 「类型+短名」Java 分支与「短名=」Python 分支)——按行+名+类型去重
|
|
185
|
-
const seen = new Set();
|
|
186
|
-
return out.filter((x) => {
|
|
187
|
-
const k = `${x.line}:${x.name}:${x.type}`;
|
|
188
|
-
if (seen.has(k)) return false;
|
|
189
|
-
seen.add(k);
|
|
190
|
-
return true;
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* 检查函数名过短(function-name-too-short kind,AST 级,正则初筛后的精筛)。
|
|
196
|
-
*
|
|
197
|
-
* 与纯正则的差别:正则 `function\s+[A-Za-z_$][\w$]{0,1}\s*\(` 命中即报,会把
|
|
198
|
-
* **i18n 领域标准缩写**(tr = translate、t、L = locale dict)当「无法猜出含义的缩写」
|
|
199
|
-
* 误报。本实现按 token 定位 function 声明后的函数名,长度 ≤ max 且**不在公认缩写
|
|
200
|
-
* 白名单**里才报。
|
|
201
|
-
*
|
|
202
|
-
* @param {string} text 文件全文
|
|
203
|
-
* @param {{max?: number, allow?: string[]}} [opts] max 默认 2;allow 追加白名单
|
|
204
|
-
* @returns {Array<{line:number, name:string}>} 命中列表
|
|
205
|
-
*/
|
|
206
|
-
export function checkShortFunctionNameAst(text = '', { max = 2, allow = [], tree = null } = {}) {
|
|
207
|
-
const tokens = tokenize(text);
|
|
208
|
-
const out = [];
|
|
209
|
-
// 长度 ≤2 但有公认语义的缩写(不报):i18n 的 tr/t、字典 L、常见工具名
|
|
210
|
-
const ALLOW = new Set(['tr', 't', 'r', 'L', 'i18n', 'db', 'fs', 'os', 'el', 'cb', 'fn', 'ns',
|
|
211
|
-
'id', 'ok', 'kv', 'ui', 'js', 'ts', 'is', 'to', 'of', 'in', 'no', 'op', 'up', 'un',
|
|
212
|
-
'i', 'j', 'k', 'x', 'y', 'z', 'e', 'a', 'b', 'n', 'p', 's', 'v', 'f', ...allow]);
|
|
213
|
-
const isShortName = (name) => String(name).length <= max && !ALLOW.has(name);
|
|
214
|
-
// 「有树走树」:树版覆盖函数声明 + 类/对象方法(token 级查不到的那族),判定语汇仍是同一份
|
|
215
|
-
// isShortName(长度阈值 + 白名单只有一处定义)。树不可用则继续走下面的 token 路径(D5)。
|
|
216
|
-
if (tree && tree.rootNode) {
|
|
217
|
-
const fromTree = shortFunctionNamesFromTree(tree, isShortName);
|
|
218
|
-
if (fromTree) return fromTree;
|
|
219
|
-
}
|
|
220
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
221
|
-
const t = tokens[i];
|
|
222
|
-
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
223
|
-
const nx = tokens[i + 1];
|
|
224
|
-
if (!nx || nx.type !== 'ident') continue;
|
|
225
|
-
if (nx.value.length <= max && !ALLOW.has(nx.value)) out.push({ line: nx.line, name: nx.value });
|
|
226
|
-
}
|
|
227
|
-
return out;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* 判定「全量读入文件」是否属于**受控小文件**读取(memory-bomb 精筛,AST 级)。
|
|
232
|
-
*
|
|
233
|
-
* 规则语义:`fs.readFile` 调用命中即报对「读本地小 JSON 配置/数据」的项目恒定误报。
|
|
234
|
-
* 内存爆炸的真实风险在「读未知大小的大文件」;而读记分数据、标题缓存这类受控小文件
|
|
235
|
-
* (通常紧跟 JSON.parse、或路径变量名含 data/cache/config/locale/store)不构成风险。
|
|
236
|
-
*
|
|
237
|
-
* 判定(满足任一即视为受控小文件读取 → 不报):
|
|
238
|
-
* ① 同一行(或紧邻下一行)出现 JSON.parse —— 读 JSON 数据结构化的标准写法;
|
|
239
|
-
* ② 读取调用前后 6 个 token 内出现小文件语义标识符(data/cache/config/locale/settings/store/state)。
|
|
240
|
-
*
|
|
241
|
-
* @param {string} text 文件全文
|
|
242
|
-
* @returns {Set<number>} 受控小文件读取的行号集合(这些行应从 memory-bomb 命中中剔除)
|
|
243
|
-
*/
|
|
244
|
-
export function checkSmallFileReadAst(text = '') {
|
|
245
|
-
const tokens = tokenize(text);
|
|
246
|
-
const out = new Set();
|
|
247
|
-
const SMALL_HINTS = /(data|cache|config|locale|settings|store|state|manifest|package)/i;
|
|
248
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
249
|
-
const t = tokens[i];
|
|
250
|
-
// 找 readFile / readFileSync 调用
|
|
251
|
-
if (!(t.type === 'ident' && /^readFile(Sync)?$/.test(t.value))) continue;
|
|
252
|
-
// 邻近 token 文本(前后各 8 个)作为上下文
|
|
253
|
-
const before = tokens.slice(Math.max(0, i - 8), i).map((x) => x.value).join(' ');
|
|
254
|
-
const after = tokens.slice(i + 1, i + 9).map((x) => x.value).join(' ');
|
|
255
|
-
const ctx = `${before} ${after}`;
|
|
256
|
-
// 行级窗口:读入后常在下一行 JSON.parse(如读取文件后紧跟 migrate(JSON.parse(raw))),
|
|
257
|
-
// token 窗口不够,按源码行扩到 ±3 行。
|
|
258
|
-
const srcLines = String(text).split('\n');
|
|
259
|
-
const lineWindow = srcLines.slice(Math.max(0, t.line - 4), t.line + 3).join(' ');
|
|
260
|
-
// ① 同语句/邻近行里出现 JSON.parse(读 JSON 数据)
|
|
261
|
-
if (/JSON\s*\.\s*parse/.test(ctx) || /JSON\s*\.\s*parse/.test(lineWindow)) { out.add(t.line); continue; }
|
|
262
|
-
// ② 上下文含小文件语义标识符
|
|
263
|
-
if (SMALL_HINTS.test(before) || SMALL_HINTS.test(after) || SMALL_HINTS.test(lineWindow)) { out.add(t.line); continue; }
|
|
264
|
-
// ③ 变量名/参数名为 file/raw 且同语句有 JSON 语义 —— 保守起见不豁免
|
|
265
|
-
}
|
|
266
|
-
return out;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* memory-bomb 的 json-stringify-large 精筛:`console.log(JSON.stringify(大对象))`
|
|
271
|
-
* 是一次性输出模式——CLI `--json` 出口序列化一次立即打印并退出,字符串不会常驻内存,
|
|
272
|
-
* 与「长期运行进程里大对象序列化」的真实风险不同。此类行豁免。
|
|
273
|
-
* @param {string} text 文件全文
|
|
274
|
-
* @returns {Set<number>} 豁免行号集合
|
|
275
|
-
*/
|
|
276
|
-
export function checkConsoleLogJsonAst(text = '') {
|
|
277
|
-
const tokens = tokenize(text);
|
|
278
|
-
const out = new Set();
|
|
279
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
280
|
-
const t = tokens[i];
|
|
281
|
-
if (!(t.type === 'ident' && t.value === 'JSON')) continue;
|
|
282
|
-
// JSON 后应跟 .stringify
|
|
283
|
-
const nx = tokens[i + 1];
|
|
284
|
-
if (!nx || nx.type !== 'punct' || nx.value !== '.') continue;
|
|
285
|
-
const nx2 = tokens[i + 2];
|
|
286
|
-
if (!nx2 || nx2.type !== 'ident' || nx2.value !== 'stringify') continue;
|
|
287
|
-
// 行内窗口:JSON.stringify 之前 12 个 token 内是否有 console.log / console.error 输出调用
|
|
288
|
-
const before = tokens.slice(Math.max(0, i - 12), i).map((x) => x.value).join(' ');
|
|
289
|
-
if (/console\s*\.\s*(log|error|warn|info)\s*\(/.test(before)) out.add(t.line);
|
|
290
|
-
}
|
|
291
|
-
return out;
|
|
292
|
-
}
|