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/callgraph.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 调用链追踪(方案 Step 5/P3)——单文件调用图
|
|
3
|
-
*
|
|
4
|
-
* 用途:io-risk 等规则的「请求路径」判定升级——不只靠函数体内特征(detectRequestContext),
|
|
5
|
-
* 还看**调用链**:被请求路径函数调用的函数,也在请求路径上。
|
|
6
|
-
*
|
|
7
|
-
* 局限(按方案标记「需人工复核」不强行判断):跨文件调用、动态调用 obj[method]()、
|
|
8
|
-
* 回调传递断链。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { tokenize } from './tokenizer.js';
|
|
12
|
-
|
|
13
|
-
/** 请求路径函数名模式(子串匹配,与 io-risk 的 REQUEST_PATTERNS 语义互补)。 */
|
|
14
|
-
const REQUEST_PATH_NAMES = ['handle', 'request', 'route', 'onrequest', 'middleware', 'api', 'controller', 'service'];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 构建单文件调用图:calleeName → Set(callerName)(调用者列表)。
|
|
18
|
-
* 只统计「具名函数体内」的具名调用(ident(...) 且被调名是 ident),
|
|
19
|
-
* 动态调用(obj[method](...) / 匿名回调)天然落不进图——按盲区处理。
|
|
20
|
-
* @param {string} text 文件全文
|
|
21
|
-
* @returns {Map<string, Set<string>>} callee → callers
|
|
22
|
-
*/
|
|
23
|
-
export function buildCallGraph(text) {
|
|
24
|
-
const tokens = tokenize(text);
|
|
25
|
-
const graph = new Map();
|
|
26
|
-
// 函数边界(名称 → [startIdx, endIdx]):function NAME / const NAME = function / NAME = (...) => / NAME(...) { }
|
|
27
|
-
const fnRanges = collectNamedFnRanges(tokens);
|
|
28
|
-
for (const [fnName, [startIdx, endIdx]] of fnRanges) {
|
|
29
|
-
for (let i = startIdx; i < endIdx; i++) {
|
|
30
|
-
const t = tokens[i];
|
|
31
|
-
if (t.type !== 'ident') continue;
|
|
32
|
-
const nx = tokens[i + 1];
|
|
33
|
-
if (!(nx && nx.type === 'punct' && nx.value === '(')) continue;
|
|
34
|
-
// 排除函数自身声明(function NAME(处的 NAME 后是 '(' 但那是声明)——fnName 的声明 token 不在本函数体前
|
|
35
|
-
if (t.value === fnName && tokens[i - 1] && tokens[i - 1].type === 'ident' && ['function', 'const', 'let', 'var'].includes(tokens[i - 1].value)) continue;
|
|
36
|
-
const callee = t.value;
|
|
37
|
-
if (!graph.has(callee)) graph.set(callee, new Set());
|
|
38
|
-
graph.get(callee).add(fnName);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return graph;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 判定函数是否在请求路径上(调用链向上追溯:本函数被某请求路径函数调用,或
|
|
46
|
-
* 该调用者又被请求路径函数调用……)。向上遍历防环。
|
|
47
|
-
* @param {string} fnName 目标函数名
|
|
48
|
-
* @param {Map<string, Set<string>>} graph buildCallGraph 结果
|
|
49
|
-
* @param {(name:string)=>boolean} [isRequestName] 请求路径函数名判定(缺省 REQUEST_PATH_NAMES)
|
|
50
|
-
* @returns {boolean}
|
|
51
|
-
*/
|
|
52
|
-
export function isInRequestPath(fnName, graph, isRequestName) {
|
|
53
|
-
const isReq = isRequestName || ((n) => REQUEST_PATH_NAMES.some((p) => String(n).toLowerCase().includes(p)));
|
|
54
|
-
const visited = new Set();
|
|
55
|
-
const stack = [fnName];
|
|
56
|
-
while (stack.length) {
|
|
57
|
-
const cur = stack.pop();
|
|
58
|
-
if (visited.has(cur)) continue;
|
|
59
|
-
visited.add(cur);
|
|
60
|
-
const callers = graph.get(cur);
|
|
61
|
-
if (!callers) continue;
|
|
62
|
-
for (const caller of callers) {
|
|
63
|
-
if (isReq(caller)) return true;
|
|
64
|
-
stack.push(caller);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** ① function NAME(...) { ... } 声明收集(找函数体 `{` 限 24 token)。 */
|
|
71
|
-
function collectFunctionDecls(tokens) {
|
|
72
|
-
const out = [];
|
|
73
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
74
|
-
const t = tokens[i];
|
|
75
|
-
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
76
|
-
let j = i + 1;
|
|
77
|
-
let name = '(匿名)';
|
|
78
|
-
if (tokens[j] && tokens[j].type === 'ident') { name = tokens[j].value; j += 1; }
|
|
79
|
-
// 找函数体 {(限 24 token)
|
|
80
|
-
let openIdx = -1;
|
|
81
|
-
for (let k = j; k < Math.min(tokens.length, j + 24); k++) {
|
|
82
|
-
if (tokens[k].type === 'punct' && tokens[k].value === '{') { openIdx = k; break; }
|
|
83
|
-
if (tokens[k].type === 'punct' && tokens[k].value === ';') break;
|
|
84
|
-
}
|
|
85
|
-
if (openIdx === -1) continue;
|
|
86
|
-
const closeIdx = matchBraceIdx(tokens, openIdx);
|
|
87
|
-
if (closeIdx > openIdx) { if (name !== '(匿名)') out.push([name, [openIdx, closeIdx]]); i = closeIdx; }
|
|
88
|
-
}
|
|
89
|
-
return out;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** ② const NAME = (...) => { } 箭头函数收集(找 => 后第一个 { 限 30 token)。 */
|
|
93
|
-
function collectArrowFns(tokens) {
|
|
94
|
-
const out = [];
|
|
95
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
96
|
-
const t = tokens[i];
|
|
97
|
-
if (!(t.type === 'ident' && ['const', 'let', 'var'].includes(t.value))) continue;
|
|
98
|
-
const name = tokens[i + 1];
|
|
99
|
-
if (!(name && name.type === 'ident')) continue;
|
|
100
|
-
// 向后找 => 后第一个 {(限 30 token)
|
|
101
|
-
for (let j = i + 2; j < Math.min(tokens.length, i + 30); j++) {
|
|
102
|
-
if (tokens[j].type === 'punct' && tokens[j].value === '=>') {
|
|
103
|
-
let openIdx = -1;
|
|
104
|
-
for (let k = j + 1; k < Math.min(tokens.length, j + 24); k++) {
|
|
105
|
-
if (tokens[k].type === 'punct' && tokens[k].value === '{') { openIdx = k; break; }
|
|
106
|
-
}
|
|
107
|
-
if (openIdx !== -1) {
|
|
108
|
-
const closeIdx = matchBraceIdx(tokens, openIdx);
|
|
109
|
-
if (closeIdx > openIdx) out.push([name.value, [openIdx, closeIdx]]);
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
if (tokens[j].type === 'punct' && tokens[j].value === ';') break;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return out;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** 收集具名函数体 token 区间:[name, [startIdx, endIdx]](function NAME / const NAME = (...) => {} / NAME = function)。 */
|
|
120
|
-
export function collectNamedFnRanges(tokens) {
|
|
121
|
-
return [...collectFunctionDecls(tokens), ...collectArrowFns(tokens)];
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** 从 openIdx(应为 `{`)找配对闭括号下标(无配对返回 openIdx)。 */
|
|
125
|
-
function matchBraceIdx(tokens, openIdx) {
|
|
126
|
-
let depth = 0;
|
|
127
|
-
for (let j = openIdx; j < tokens.length; j++) {
|
|
128
|
-
if (tokens[j].type === 'punct' && tokens[j].value === '{') depth++;
|
|
129
|
-
else if (tokens[j].type === 'punct' && tokens[j].value === '}') {
|
|
130
|
-
depth--;
|
|
131
|
-
if (depth === 0) return j;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return openIdx;
|
|
135
|
-
}
|
package/lib/ast/code-lines.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 代码行判定
|
|
3
|
-
*
|
|
4
|
-
* 职责:判断某行是否「真在代码里」(而非注释/字符串内),并取出代码里的字符串字面量。
|
|
5
|
-
* 供 regex 类规则做二次确认,避免注释中的示例被当真实代码报错。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { tokenize } from './tokenizer.js';
|
|
9
|
-
|
|
10
|
-
/* ───────────────────────── 正则初筛 → AST 精筛(通用设施) ───────────────────────── */
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 正则初筛 → token 级精筛(所有文本型检查的通用两段式)。
|
|
14
|
-
*
|
|
15
|
-
* 为什么两段式:正则快、能一次筛出候选行;但正则无法区分「代码」与「注释/字符串」,
|
|
16
|
-
* 单用必然误报(注释版本号、CSS 字号、i18n 字典值)。tokenizer 能精确分类,但全量
|
|
17
|
-
* token 化成本高于正则。两段式 = 正则初筛候选行 → 只对候选行做 token 判定。
|
|
18
|
-
*
|
|
19
|
-
* @param {string} text 文件全文
|
|
20
|
-
* @param {number[]} candidateLines 正则初筛命中的行号(1-based)
|
|
21
|
-
* @returns {{isCode: (line:number)=>boolean}} isCode(line) 判定该行是否含「代码 token」
|
|
22
|
-
* (注释/字符串/模板串/纯空白行 → false;含 ident/num/punct 的行 → true)
|
|
23
|
-
*/
|
|
24
|
-
export function makeCodeLineFilter(text = '', candidateLines = [], opts = {}) {
|
|
25
|
-
// 散文体文档(markdown/txt):tokenizer 会把自然语言词判成 ident
|
|
26
|
-
// (「CIFS」「挂载适配」都是 ident),于是整段散文被当成「含代码 token 的行」,
|
|
27
|
-
// astConfirm 形同虚设——实测 README 版本记录里说明「token 被误报」的那句话
|
|
28
|
-
// 本身被 security/no-hardcoded-credentials 报成 blocker。
|
|
29
|
-
// 故对这类文件改用文档语义:只有**围栏代码块内**与**行内代码**才算代码。
|
|
30
|
-
if (isProseDoc(text, opts.file)) return makeProseCodeFilter(text, candidateLines);
|
|
31
|
-
|
|
32
|
-
const want = new Set(candidateLines);
|
|
33
|
-
const codeLines = new Set();
|
|
34
|
-
const suspect = new Set();
|
|
35
|
-
for (const t of tokenize(text)) {
|
|
36
|
-
if (!want.has(t.line)) continue;
|
|
37
|
-
if (t.type === 'ident' || t.type === 'num') codeLines.add(t.line);
|
|
38
|
-
if (t.type === 'punct') suspect.add(t.line);
|
|
39
|
-
}
|
|
40
|
-
for (const lineNo of suspect) {
|
|
41
|
-
// 只有标点(如 `}` `);`)不算代码行,需同时有 ident/num
|
|
42
|
-
if (!codeLines.has(lineNo)) codeLines.delete(lineNo);
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
isCode: (line) => codeLines.has(line),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/* ───────────────────────── 列级精筛(行级判定的加强版) ───────────────────────── */
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 正则初筛 → **列级** token 精筛。
|
|
53
|
-
*
|
|
54
|
-
* 为什么需要:行级判定只能回答「这一行有没有代码 token」,回答不了
|
|
55
|
-
* 「正则命中的**那一处**是不是落在字符串/注释里」。实测遗留误报:
|
|
56
|
-
*
|
|
57
|
-
* const hang = await provider.execute(session, { script: 'while (true) {}' });
|
|
58
|
-
*
|
|
59
|
-
* 该行确实含 ident(script / provider 等)⇒ 行级判定放行 ⇒
|
|
60
|
-
* performance/busy-wait-loop 把字符串字面量里的 while(true) 当成真实死循环报出 ✗。
|
|
61
|
-
*
|
|
62
|
-
* 做法:在行级判定之上,用 tokenizer 的 str/tmpl/comment token 构造「不透明区间」,
|
|
63
|
-
* 命中列落在区间内 ⇒ 判为「不是代码」。
|
|
64
|
-
*
|
|
65
|
-
* @param {string} text 文件全文
|
|
66
|
-
* @param {number[]} candidateLines 候选行(1-based)
|
|
67
|
-
* @param {{file?:string}} [opts]
|
|
68
|
-
* @returns {{isCode:(line:number)=>boolean, isCodeHit:(line:number, col:number)=>boolean}}
|
|
69
|
-
* isCodeHit 的 col 为 **1-based 列**(正则 exec 的 index + 1);
|
|
70
|
-
* 列缺失/非法时**退化为行级判定**(宁可放行,不因拿不到列而误杀)。
|
|
71
|
-
*/
|
|
72
|
-
export function makeCodeSpanFilter(text = '', candidateLines = [], opts = {}) {
|
|
73
|
-
const base = makeCodeLineFilter(text, candidateLines, opts);
|
|
74
|
-
// 不透明区间只取**单行 token**:跨行 token(三引号/多行块注释)的 col 是续行内位置,
|
|
75
|
-
// 用它构造区间会算错。跨行场景本就由行级判定覆盖——续行不产 ident/num ⇒ 不算代码行。
|
|
76
|
-
const opaque = new Map(); // line → [[startCol, endColExclusive), ...]
|
|
77
|
-
// onlyComment:只把**注释**当不透明区间,字符串仍算代码。
|
|
78
|
-
// 为什么需要这一档:paths/凭据类规则的命中本来就写在字符串里(路径字面量),
|
|
79
|
-
// 若把字符串也一并当「非代码」会把这些规则整体废掉(实测 hardcoded-absolute-path 5→0);
|
|
80
|
-
// 而「注释里的示例代码被当真」是纯误报,两者必须分开处理。
|
|
81
|
-
const opaqueTypes = opts.onlyComment ? new Set(['comment']) : new Set(['str', 'tmpl', 'comment']);
|
|
82
|
-
for (const t of tokenize(text)) {
|
|
83
|
-
if (!opaqueTypes.has(t.type)) continue;
|
|
84
|
-
const v = String(t.value);
|
|
85
|
-
if (v.includes('\n')) continue;
|
|
86
|
-
const col = Number(t.col);
|
|
87
|
-
if (!Number.isFinite(col) || col < 1) continue;
|
|
88
|
-
const arr = opaque.get(t.line) || [];
|
|
89
|
-
arr.push([col, col + v.length]);
|
|
90
|
-
opaque.set(t.line, arr);
|
|
91
|
-
}
|
|
92
|
-
return {
|
|
93
|
-
isCode: base.isCode,
|
|
94
|
-
isCodeHit: (line, col) => {
|
|
95
|
-
// onlyComment(只判注释)**不走行级判定**:行级判定里编码了「散文体文档只有围栏代码算代码」
|
|
96
|
-
// 的语义,若沿用它,`contact: x@y.com` 这种普通文档行会被判非代码 ⇒ 所有正则规则
|
|
97
|
-
// 在 .md/.txt 上整体失效(实测:隐私规则端到端用例直接转红)。
|
|
98
|
-
// 这里只回答「这一列是否落在注释 token 区间内」,与文件体裁无关。
|
|
99
|
-
if (!opts.onlyComment && !base.isCode(line)) return false;
|
|
100
|
-
const ranges = opaque.get(line);
|
|
101
|
-
if (!ranges || !ranges.length) return true;
|
|
102
|
-
const c = Number(col);
|
|
103
|
-
if (!Number.isFinite(c) || c < 1) return true; // 拿不到列 ⇒ 退化为行级判定
|
|
104
|
-
for (const [s, e] of ranges) if (c >= s && c < e) return false;
|
|
105
|
-
return true;
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 是否散文体文档 —— **只按扩展名判定**。
|
|
112
|
-
*
|
|
113
|
-
* 刻意不做内容特征兜底:shell 注释 `# cd "$X"` 与 markdown 标题 `# 标题`
|
|
114
|
-
* 在文本层面完全同形,任何基于「以 # 开头」的启发式都会把 shell 脚本
|
|
115
|
-
* 误判为文档,进而让所有行按「非围栏、非行内代码」被剔除——实测使
|
|
116
|
-
* folder/cd-to-maybe-missing 规则在 .sh 上整体失效(回归测试 test 542 失败)。
|
|
117
|
-
* 调用方(lib/ast/shell.js)不传文件名,此时按代码处理是正确的默认。
|
|
118
|
-
* @param {string} text 文件全文(当前未使用,保留签名以备扩展)
|
|
119
|
-
* @param {string} file 文件路径(用于取扩展名)
|
|
120
|
-
* @returns {boolean}
|
|
121
|
-
*/
|
|
122
|
-
function isProseDoc(text, file) {
|
|
123
|
-
return /\.(md|markdown|txt|rst)$/i.test(String(file || ''));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 文档语义的代码行判定:只有围栏代码块内、以及**行内代码片段本身**算「代码」。
|
|
128
|
-
*
|
|
129
|
-
* 关键区别在行内代码:不能「该行有行内代码 → 整行放行」。文档里一行往往既有
|
|
130
|
-
* 散文又有多个行内片段(如版本记录一行写「...`chmod`/`utimes` 因 SMB 无 POSIX
|
|
131
|
-
* Extensions...`lib/fsx.js` 统一收口...」),整行放行等于把散文也当代码。
|
|
132
|
-
* 实测 README 说明「token 被误报」的那句话因此被报成 blocker。
|
|
133
|
-
* 做法:把该行的行内代码片段拼成「代码视图」,仅当代码视图里含 ident/num
|
|
134
|
-
* (即片段本身像代码)时才标为候选,供后续 token 级精筛继续判定。
|
|
135
|
-
*
|
|
136
|
-
* @returns {{isCode: (line:number)=>boolean}}
|
|
137
|
-
*/
|
|
138
|
-
function makeProseCodeFilter(text, candidateLines) {
|
|
139
|
-
const want = new Set(candidateLines);
|
|
140
|
-
const lines = String(text).split('\n');
|
|
141
|
-
const codeLines = new Set();
|
|
142
|
-
let inFence = false;
|
|
143
|
-
for (let i = 0; i < lines.length; i++) {
|
|
144
|
-
const line = lines[i];
|
|
145
|
-
if (/^\s*(```|~~~)/.test(line)) {
|
|
146
|
-
// 围栏标记行本身不算代码(它是文档语法)
|
|
147
|
-
inFence = !inFence;
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (inFence) { codeLines.add(i + 1); continue; }
|
|
151
|
-
if (!want.has(i + 1)) continue; // 只判候选行,省 token 化成本
|
|
152
|
-
// 取该行全部行内代码片段,判断「片段自身」是否像代码
|
|
153
|
-
const snippets = String(line).match(/`[^`]+`/g) || [];
|
|
154
|
-
if (!snippets.length) continue;
|
|
155
|
-
const snippetText = snippets.map((x) => x.slice(1, -1)).join('\n');
|
|
156
|
-
for (const t of tokenize(snippetText)) {
|
|
157
|
-
if (t.type === 'ident' || t.type === 'num' || t.type === 'str') { codeLines.add(i + 1); break; }
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return { isCode: (line) => codeLines.has(line) };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* 判定一组字符串字面量值是否出现在代码里(供「重复硬编码串」等规则精筛)。
|
|
165
|
-
* @param {string} text 文件全文
|
|
166
|
-
* @returns {Set<string>} 代码中出现的字符串字面量裸值集合
|
|
167
|
-
*/
|
|
168
|
-
export function codeStringLiterals(text = '') {
|
|
169
|
-
const out = new Set();
|
|
170
|
-
for (const t of tokenize(text)) {
|
|
171
|
-
if (t.type !== 'str' && t.type !== 'tmpl') continue;
|
|
172
|
-
out.add(String(t.value).replace(/^(['"`])([\s\S]*)\1$/, '$2'));
|
|
173
|
-
}
|
|
174
|
-
return out;
|
|
175
|
-
}
|
package/lib/ast/consts.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// AST 判定层公共常量(单一来源)。
|
|
2
|
-
//
|
|
3
|
-
// 为什么单独成模块:同一常量此前在 lib/ast 的多个文件各写一份(如 FN_BODY_LOOKAHEAD 在
|
|
4
|
-
// brace.js 与 control-flow.js),两处必须永远相等——漏改一处会出现「括号匹配按 20 回看、
|
|
5
|
-
// 控制流按别的值回看」这类静默不一致(自审 maintainability/duplicate-constant-def 报的真重复)。
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 找函数体起始 `{` 时向后回看的 token 数。
|
|
9
|
-
* 函数签名可能很长(多行形参/默认值/泛型/装饰器),回看窗口太小会找不到 `{` 而漏判函数体,
|
|
10
|
-
* 太大则可能越过边界把下一个块的 `{` 当成函数体——20 是实测能覆盖本仓库全部形态的值。
|
|
11
|
-
*/
|
|
12
|
-
export const FN_BODY_LOOKAHEAD = 20;
|