dsh-git-push 0.0.0-stage → 2.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.auditignore +30 -0
- package/.gitignore +39 -0
- package/LICENSE +21 -0
- package/README.md +1486 -2
- package/cli.mjs +360 -0
- package/cordis.patch.yml +14 -0
- package/lib/ARCHITECTURE.md +34 -0
- package/lib/BUGFIX-NOTES-2026-09-14.md +278 -0
- package/lib/app/apply.js +275 -0
- package/lib/app/audit-api.js +158 -0
- package/lib/app/command-registry.js +266 -0
- package/lib/app/constants.js +16 -0
- package/lib/app/handlers/account.js +166 -0
- package/lib/app/handlers/clone.js +168 -0
- package/lib/app/handlers/http-constants.js +9 -0
- package/lib/app/handlers/identity-rewrite.js +94 -0
- package/lib/app/handlers/meta.js +175 -0
- package/lib/app/handlers/npm.js +169 -0
- package/lib/app/handlers/repo-actions.js +143 -0
- package/lib/app/handlers/repos.js +231 -0
- package/lib/app/handlers/settings.js +62 -0
- package/lib/app/host-doc-checks.js +25 -0
- package/lib/app/http-handlers.js +131 -0
- package/lib/app/index.js +30 -0
- package/lib/app/inject-text.js +192 -0
- package/lib/app/scan-root.js +23 -0
- package/lib/app/schema.js +150 -0
- package/lib/app/settings-bridge.js +167 -0
- package/lib/app/slash-commands.js +395 -0
- package/lib/app/slot-stats.js +35 -0
- package/lib/app/tool-call.js +672 -0
- package/lib/app/tools.js +34 -0
- package/lib/ast/brace.js +105 -0
- package/lib/ast/callgraph.js +135 -0
- package/lib/ast/code-lines.js +114 -0
- package/lib/ast/consts.js +12 -0
- package/lib/ast/control-flow.js +461 -0
- package/lib/ast/credential.js +199 -0
- package/lib/ast/dataflow.js +274 -0
- package/lib/ast/dup-code.js +165 -0
- package/lib/ast/index.js +36 -0
- package/lib/ast/io-risk-const.js +95 -0
- package/lib/ast/io-risk-fn.js +208 -0
- package/lib/ast/io-risk-loop.js +308 -0
- package/lib/ast/io-risk.js +314 -0
- package/lib/ast/lang.js +156 -0
- package/lib/ast/line-count.js +52 -0
- package/lib/ast/magic-number.js +272 -0
- package/lib/ast/naming.js +284 -0
- package/lib/ast/perf.js +472 -0
- package/lib/ast/scope.js +241 -0
- package/lib/ast/shell.js +131 -0
- package/lib/ast/size.js +378 -0
- package/lib/ast/symbol-index.js +399 -0
- package/lib/ast/tokenizer.js +341 -0
- package/lib/audit/analysis-coverage.js +98 -0
- package/lib/audit/audit-file.js +182 -0
- package/lib/audit/checks.js +17 -0
- package/lib/audit/client-entry.js +93 -0
- package/lib/audit/collector.js +477 -0
- package/lib/audit/ext-runner.js +80 -0
- package/lib/audit/file-context.js +138 -0
- package/lib/audit/finding.js +50 -0
- package/lib/audit/gitignore-match.js +113 -0
- package/lib/audit/glob.js +63 -0
- package/lib/audit/history-report.js +100 -0
- package/lib/audit/history.js +126 -0
- package/lib/audit/ignore-blind.js +101 -0
- package/lib/audit/index.js +33 -0
- package/lib/audit/orchestrate.js +476 -0
- package/lib/audit/public.js +24 -0
- package/lib/audit/repo-level.js +30 -0
- package/lib/audit/report-yaml.js +86 -0
- package/lib/audit/slot.js +65 -0
- package/lib/audit-defaults.js +18 -0
- package/lib/audit-rules/audit-rules-comment.yml +247 -0
- package/lib/audit-rules/audit-rules-cpp.yml +22 -0
- package/lib/audit-rules/audit-rules-docs.yml +32 -0
- package/lib/audit-rules/audit-rules-dsh.yml +162 -0
- package/lib/audit-rules/audit-rules-filehealth.yml +42 -0
- package/lib/audit-rules/audit-rules-folder.yml +99 -0
- package/lib/audit-rules/audit-rules-frontend.yml +335 -0
- package/lib/audit-rules/audit-rules-go.yml +22 -0
- package/lib/audit-rules/audit-rules-i18n.yml +45 -0
- package/lib/audit-rules/audit-rules-java.yml +105 -0
- package/lib/audit-rules/audit-rules-nodejs.yml +647 -0
- package/lib/audit-rules/audit-rules-npm.yml +187 -0
- package/lib/audit-rules/audit-rules-paths.yml +92 -0
- package/lib/audit-rules/audit-rules-performance.yml +215 -0
- package/lib/audit-rules/audit-rules-php.yml +22 -0
- package/lib/audit-rules/audit-rules-private.yml +24 -0
- package/lib/audit-rules/audit-rules-python.yml +106 -0
- package/lib/audit-rules/audit-rules-robustness.yml +27 -0
- package/lib/audit-rules/audit-rules-rust.yml +22 -0
- package/lib/audit-rules/audit-rules-structure.yml +159 -0
- package/lib/audit-rules/audit-rules-swift.yml +22 -0
- package/lib/audit-rules/audit-rules-template.yml +108 -0
- package/lib/checks/button-bind.js +276 -0
- package/lib/checks/common.js +170 -0
- package/lib/checks/credential-file.js +43 -0
- package/lib/checks/dataflow.js +40 -0
- package/lib/checks/dispatch.js +112 -0
- package/lib/checks/dup-code.js +69 -0
- package/lib/checks/dup-const.js +232 -0
- package/lib/checks/file-health.js +150 -0
- package/lib/checks/filter.js +137 -0
- package/lib/checks/folder.js +173 -0
- package/lib/checks/hardcoded-path.js +98 -0
- package/lib/checks/index.js +46 -0
- package/lib/checks/io.js +66 -0
- package/lib/checks/magic-number.js +42 -0
- package/lib/checks/npm-json.js +273 -0
- package/lib/checks/private.js +63 -0
- package/lib/checks/regex.js +256 -0
- package/lib/checks/semantic.js +134 -0
- package/lib/checks/structural.js +274 -0
- package/lib/cli/commands-account.mjs +107 -0
- package/lib/cli/commands-audit.mjs +144 -0
- package/lib/cli/commands-doc.mjs +127 -0
- package/lib/cli/commands-vcs.mjs +175 -0
- package/lib/client/index.js +162 -0
- package/lib/client-parts/00-loader.js +33 -0
- package/lib/client-parts/01-const.js +207 -0
- package/lib/client-parts/02-utils.js +62 -0
- package/lib/client-parts/03-icons.js +12 -0
- package/lib/client-parts/10-account.js +83 -0
- package/lib/client-parts/11-browser.js +95 -0
- package/lib/client-parts/20-repo.js +374 -0
- package/lib/client-parts/21-audit.js +301 -0
- package/lib/client-parts/22-settings.js +146 -0
- package/lib/client-parts/23-npm.js +383 -0
- package/lib/client-parts/30-page.js +56 -0
- package/lib/client-parts/40-controller.js +1092 -0
- package/lib/client-parts/41-actions.js +279 -0
- package/lib/client-parts/99-apply.js +72 -0
- package/lib/client.js +3195 -0
- package/lib/commit-push.js +148 -0
- package/lib/context/index.js +363 -0
- package/lib/core/atomic-json.js +98 -0
- package/lib/core/config.js +40 -0
- package/lib/core/credentials.js +274 -0
- package/lib/core/endpoints.js +109 -0
- package/lib/core/exec.js +162 -0
- package/lib/exempt/index.js +292 -0
- package/lib/fs/edit-after-read.js +103 -0
- package/lib/fsx.js +87 -0
- package/lib/git/account-status.js +140 -0
- package/lib/git/account.js +233 -0
- package/lib/git/api.js +136 -0
- package/lib/git/atomic-json.js +6 -0
- package/lib/git/browse.js +38 -0
- package/lib/git/clone-download.js +397 -0
- package/lib/git/clone-history-mode.js +143 -0
- package/lib/git/clone-history.js +175 -0
- package/lib/git/clone-jobs.js +250 -0
- package/lib/git/clone-replay-io.js +172 -0
- package/lib/git/clone-replay.js +275 -0
- package/lib/git/clone.js +445 -0
- package/lib/git/cloud.js +46 -0
- package/lib/git/commit-message.js +141 -0
- package/lib/git/config.js +6 -0
- package/lib/git/cred-env.js +88 -0
- package/lib/git/credentials.js +6 -0
- package/lib/git/endpoints.js +6 -0
- package/lib/git/exec.js +6 -0
- package/lib/git/identity-rewrite.js +297 -0
- package/lib/git/identity.js +150 -0
- package/lib/git/ignore.js +69 -0
- package/lib/git/index.js +41 -0
- package/lib/git/module-splitter.js +47 -0
- package/lib/git/post-push.js +101 -0
- package/lib/git/push-diagnose.js +142 -0
- package/lib/git/push.js +476 -0
- package/lib/git/remote-resolve.js +146 -0
- package/lib/git/remote.js +128 -0
- package/lib/git/repo-index.js +512 -0
- package/lib/git/repos.js +235 -0
- package/lib/git/scan-runner.js +159 -0
- package/lib/git/sensitive.js +134 -0
- package/lib/git/transport.js +430 -0
- package/lib/git/visibility.js +35 -0
- package/lib/git/wrapped-git.js +68 -0
- package/lib/http/index.js +170 -0
- package/lib/index.js +24 -0
- package/lib/link-check/index.js +187 -0
- package/lib/npm/account.js +64 -0
- package/lib/npm/install.js +211 -0
- package/lib/npm/npmrc.js +56 -0
- package/lib/npm/publish.js +171 -0
- package/lib/npm/registry.js +168 -0
- package/lib/plugin/auto-detect.js +92 -0
- package/lib/plugin/auto-push.js +154 -0
- package/lib/plugin/index.js +250 -0
- package/lib/readme-templates/readme.yml +38 -0
- package/lib/rule/compilers/credential.js +62 -0
- package/lib/rule/compilers/dataflow.js +38 -0
- package/lib/rule/compilers/file-health.js +75 -0
- package/lib/rule/compilers/frontend.js +86 -0
- package/lib/rule/compilers/func.js +35 -0
- package/lib/rule/compilers/helpers.js +71 -0
- package/lib/rule/compilers/index.js +29 -0
- package/lib/rule/compilers/link-semantic.js +45 -0
- package/lib/rule/compilers/list-kinds.js +12 -0
- package/lib/rule/compilers/numeric-kinds.js +66 -0
- package/lib/rule/compilers/regex.js +78 -0
- package/lib/rule/compilers/structure.js +80 -0
- package/lib/rule/compilers.js +17 -0
- package/lib/rule/homoglyph.js +45 -0
- package/lib/rule/loader.js +270 -0
- package/lib/rule/registry.js +115 -0
- package/lib/rule/scope.js +67 -0
- package/lib/score/docs-score.js +166 -0
- package/lib/score/index.js +140 -0
- package/lib/self/index.js +196 -0
- package/lib/skip-dirs.js +64 -0
- package/lib/tool-probes.json +50 -0
- package/lib/user-requirements.json +14 -0
- package/lib/vendor/js-yaml/LICENSE +21 -0
- package/lib/vendor/js-yaml/js-yaml.mjs +3073 -0
- package/package.json +95 -4
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +34 -0
- package/scripts/audit-ext/variable-min-length.mjs +102 -0
- package/scripts/audit-runner.mjs +35 -0
- package/scripts/audit-runtime-check.mjs +129 -0
- package/scripts/browser-page-probe.mjs +271 -0
- package/scripts/build-client.mjs +119 -0
- package/scripts/check.mjs +88 -0
- package/scripts/clean-date-comments.py +102 -0
- package/scripts/cpu-sample.mjs +188 -0
- package/scripts/doc-drift.mjs +200 -0
- package/scripts/doc-func.mjs +212 -0
- package/scripts/doc-tree.mjs +456 -0
- package/scripts/doc-version.mjs +220 -0
- package/scripts/frontend-preview-check.mjs +124 -0
- package/scripts/hot-path-probe-cjs.cjs +71 -0
- package/scripts/hot-path-probe-fs-shim.mjs +28 -0
- package/scripts/hot-path-probe-fsp-shim.mjs +31 -0
- package/scripts/hot-path-probe.mjs +153 -0
- package/scripts/module-splitter.py +800 -0
- package/scripts/platform-scan.mjs +168 -0
- package/scripts/preview-gen.mjs +489 -0
- package/scripts/preview-local-runner.mjs +55 -0
- package/scripts/preview-server-direct.mjs +116 -0
- package/scripts/preview-server.mjs +305 -0
- package/scripts/preview-slot-data.mjs +123 -0
- package/scripts/probe-recheck.mjs +47 -0
- package/scripts/readme-gen.mjs +268 -0
- package/scripts/rename-locator.mjs +97 -0
- package/scripts/rule-switch.mjs +97 -0
- package/scripts/rules-solo-audit.mjs +173 -0
- package/scripts/scan-file-io.mjs +713 -0
- package/scripts/scan-hardcoded-paths.mjs +125 -0
- package/scripts/scan-repos.mjs +167 -0
- package/scripts/scan-version.mjs +122 -0
- package/scripts/scrub-user-wording.mjs +420 -0
- package/scripts/symbol-index.mjs +85 -0
- package/scripts/sync-plugin-install.mjs +223 -0
- package/scripts/sync-plugin.mjs +279 -0
- package/scripts/verify-prestep.mjs +100 -0
- package/skills/dev/audit-rule-authoring.md +58 -0
- package/skills/dev/dsh-git-push.md +323 -0
- package/skills/dev/git-push-live-fix.md +45 -0
- package/skills/dsh-repo-index.md +35 -0
- package/skills/guide/guide-audit.md +167 -0
- package/skills/guide/guide-cli-scripts.md +104 -0
- package/skills/guide/guide-clone-repos.md +173 -0
- package/skills/guide/guide-commit-push.md +156 -0
- package/skills/guide/guide-inject-ui.md +166 -0
- package/skills/guide/guide-slash-completion.md +118 -0
- package/skills/workflow/README.md +21 -0
- package/skills/workflow/git-commit-discipline.md +68 -0
- package/skills/workflow/git-project-startup.md +85 -0
- package/skills/workflow/git-rebuild-process.md +57 -0
- package/skills/workflow/github-operations.md +91 -0
- package/skills/workflow/readme-sync-git-md.md +28 -0
- package/skills/workflow/task-completion-report.md +73 -0
- package/skills/workflow/tool-json-add-ask.md +35 -0
- package/skills/workflow/versioning-rule.md +211 -0
package/lib/ast/scope.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 变量作用域分类器 · 最小版(方案 Step 1)
|
|
3
|
+
*
|
|
4
|
+
* 基于 token 流的行号区间法(零依赖,复用 dataflow 的函数体区间思路):
|
|
5
|
+
* 预扫描 tokens 得到「函数体区间」与「循环体区间」,再按声明所在行号归类:
|
|
6
|
+
* - 在函数体内 → 'function'
|
|
7
|
+
* - 在循环体内 → 'loop'(同时标注 inFunction)
|
|
8
|
+
* - 其余 → 'module'(顶层)
|
|
9
|
+
* 另提供「模块级常量赋值」判定(isModuleConstAssignment)——供 magic-number 等规则
|
|
10
|
+
* 豁免 `const MAX_RETRY = 3` 这类模块级命名常量(评审方案:模块/全局作用域豁免常量)。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { tokenize } from './tokenizer.js';
|
|
14
|
+
|
|
15
|
+
/** return 语句向后扫「暴露了哪些函数名」的最大 token 跨度(防遍历到语句外)。 */
|
|
16
|
+
const RETURN_SCAN_LOOKAHEAD = 40;
|
|
17
|
+
|
|
18
|
+
/** 收集函数体行号区间 [startLine, endLine](function 声明/表达式/箭头/方法)。 */
|
|
19
|
+
export function collectFnLineRanges(text) {
|
|
20
|
+
const tokens = tokenize(text);
|
|
21
|
+
const ranges = [];
|
|
22
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
23
|
+
const t = tokens[i];
|
|
24
|
+
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
25
|
+
let openIdx = -1;
|
|
26
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + 24); j++) {
|
|
27
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '{') { openIdx = j; break; }
|
|
28
|
+
if (tokens[j].type === 'punct' && (tokens[j].value === ';' || tokens[j].value === '}')) break;
|
|
29
|
+
}
|
|
30
|
+
if (openIdx === -1) continue;
|
|
31
|
+
const depth = braceCloseIdx(tokens, openIdx);
|
|
32
|
+
if (depth > openIdx) ranges.push([tokens[openIdx].line, tokens[depth].line]);
|
|
33
|
+
}
|
|
34
|
+
return ranges;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 收集循环体行号区间 [startLine, endLine](for/while/do/for-in/for-of)。 */
|
|
38
|
+
export function collectLoopLineRanges(text) {
|
|
39
|
+
const tokens = tokenize(text);
|
|
40
|
+
const ranges = [];
|
|
41
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
42
|
+
const t = tokens[i];
|
|
43
|
+
if (t.type !== 'ident' || !['for', 'while', 'do'].includes(t.value)) continue;
|
|
44
|
+
// for (...) { / while (...) { / do { —— 找循环后第一个 '{'
|
|
45
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + 24); j++) {
|
|
46
|
+
const tj = tokens[j];
|
|
47
|
+
if (tj.type === 'punct' && tj.value === '{') {
|
|
48
|
+
const depth = braceCloseIdx(tokens, j);
|
|
49
|
+
if (depth > j) ranges.push([tokens[j].line, tokens[depth].line]);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
// fix:for 头部分号(for (let i=0; ...))不能提前 break——只以 } 或超限终止
|
|
53
|
+
if (tj.type === 'punct' && tj.value === '}') break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return ranges;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 从 openIdx(应为 `{`)找配对闭括号下标(无配对返回 openIdx)。 */
|
|
60
|
+
function braceCloseIdx(tokens, openIdx) {
|
|
61
|
+
let depth = 0;
|
|
62
|
+
for (let j = openIdx; j < tokens.length; j++) {
|
|
63
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '{') depth++;
|
|
64
|
+
else if (tokens[j].type === 'punct' && tokens[j].value === '}') {
|
|
65
|
+
depth--;
|
|
66
|
+
if (depth === 0) return j;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return openIdx;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
/** 启动路径函数名模式(一次性初始化:apply/init/main/bootstrap/setup 等),豁免大函数/high 复杂度。 */
|
|
75
|
+
const STARTUP_PATH_PATTERNS = ['apply', 'init', 'main', 'bootstrap', 'setup', 'mount', 'start', 'boot', 'register', 'configure', 'sync', 'restore', 'migrate'];
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 序列化/反序列化职责函数名模式(评审规则③):
|
|
79
|
+
* 序列化/反序列化函数(to_json/from_json/dumps/loads/serialize/deserialize/parse/unpack 等)
|
|
80
|
+
* 在意义明确、变量生命周期仅当前会话时,代码常超百行——过度拆分反而让数据传递与
|
|
81
|
+
* 生命周期处理复杂化,得不偿失。故函数长度/复杂度规则对其放宽(非全豁免,见规则 yml)。
|
|
82
|
+
* 收紧面:仅「职责名」命中(含 serialize/deserial/serializ/to_json/from_json/dumps/loads/
|
|
83
|
+
* parse/unpack/pack/encode/decode 语义的函数名),普通业务函数不含这些词不受影响。
|
|
84
|
+
*/
|
|
85
|
+
const SERIALIZATION_PATH_PATTERNS = ['serial', 'to_json', 'from_json', 'dumps', 'loads', 'parse', 'unpack', 'pack', 'encode', 'decode'];
|
|
86
|
+
|
|
87
|
+
/** 按函数名判执行路径:'startup' | 'serialization' | 'unknown'(请求路径等复杂判定留给调用链分析)。 */
|
|
88
|
+
export function classifyFunctionPath(fnName = '') {
|
|
89
|
+
const name = String(fnName).toLowerCase();
|
|
90
|
+
if (STARTUP_PATH_PATTERNS.some((p) => name.includes(p))) return 'startup';
|
|
91
|
+
if (SERIALIZATION_PATH_PATTERNS.some((p) => name.includes(p))) return 'serialization';
|
|
92
|
+
return 'unknown';
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 声明行号归类:'module' | 'function' | 'loop'。
|
|
96
|
+
* @param {string} text 文件全文
|
|
97
|
+
* @param {number} line 声明所在行号(1-based)
|
|
98
|
+
* @returns {{scope: string, inFunction: boolean, inLoop: boolean}}
|
|
99
|
+
*/
|
|
100
|
+
export function classifyLineScope(text, line) {
|
|
101
|
+
const inLoop = collectLoopLineRanges(text).some(([s, e]) => line >= s && line <= e);
|
|
102
|
+
const inFunction = collectFnLineRanges(text).some(([s, e]) => line >= s && line <= e);
|
|
103
|
+
return { scope: inFunction ? 'function' : 'module', inFunction, inLoop };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 模块级常量赋值判定:该行形如 `const NAME = <数字>`(或 let/var + 大写常量名),
|
|
108
|
+
* 且声明在模块顶层(不在函数体内)——magic-number 等规则豁免这类命名常量。
|
|
109
|
+
* @param {string} text 文件全文
|
|
110
|
+
* @param {number} line 目标行号
|
|
111
|
+
* @returns {boolean}
|
|
112
|
+
*/
|
|
113
|
+
export function isModuleConstAssignment(text, line) { // dsh-skip-complexity: 模块级常量判定器(顶层过滤+常量名风格+行内=num 多条件链,职责单一)
|
|
114
|
+
// 仅模块顶层:函数体内 `const x = 3` 不算命名常量(评审方案:函数内魔数照报)
|
|
115
|
+
if (collectFnLineRanges(text).some(([sf, ef]) => line >= sf && line <= ef)) return false;
|
|
116
|
+
const tokens = tokenize(text);
|
|
117
|
+
// 行内找 const/let/var → ident → = 且右侧是 num(含大写常量名风格)
|
|
118
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
119
|
+
const t = tokens[i];
|
|
120
|
+
if (t.line !== line) continue;
|
|
121
|
+
if (t.type !== 'ident' || !['const', 'let', 'var'].includes(t.value)) continue;
|
|
122
|
+
const name = tokens[i + 1];
|
|
123
|
+
if (!name || name.type !== 'ident' || name.line !== line) continue;
|
|
124
|
+
// 常量名风格:全大写或 CamelCase(小写则不算命名常量)
|
|
125
|
+
const isConstStyle = /^[A-Z]/.test(name.value) || /^[a-z]+[A-Z]/.test(name.value);
|
|
126
|
+
if (!isConstStyle) continue;
|
|
127
|
+
// 跳过 = 后是函数/表达式(非纯数字)的情况——找行内 '=' 后的 num
|
|
128
|
+
for (let j = i + 2; j < tokens.length && tokens[j].line === line; j++) {
|
|
129
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '=') {
|
|
130
|
+
const rhs = tokens[j + 1];
|
|
131
|
+
if (rhs && rhs.type === 'num' && rhs.line === line) return true;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* 闭包双重作用域(方案 Step 6/P4)——词法作用域 vs 功能作用域。
|
|
140
|
+
*
|
|
141
|
+
* 词法上在函数内(局部),功能上可能被暴露(return / 挂 this / 挂 exports)→ 公共 API,
|
|
142
|
+
* 按模块级规则处理(不享受 startup 等豁免);作为参数传递 → 回调(unknown 路径)。
|
|
143
|
+
* @param {string} text 文件全文
|
|
144
|
+
* @returns {Map<string, string>} fnName → 'public' | 'callback'
|
|
145
|
+
*/
|
|
146
|
+
export function analyzeFunctionalScope(text = '') {
|
|
147
|
+
const tokens = tokenize(text);
|
|
148
|
+
const fnNames = collectFnNames(tokens);
|
|
149
|
+
const scope = new Map();
|
|
150
|
+
scanReturnExposed(tokens, fnNames, scope); // ① return 暴露
|
|
151
|
+
scanAssignmentExposed(tokens, fnNames, scope); // ② this/exports 暴露
|
|
152
|
+
scanCallbackPassed(tokens, fnNames, scope); // ③ 参数传递
|
|
153
|
+
return scope;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 收集全部具名函数名(function NAME / const NAME = function / const NAME = (...) =>)。 */
|
|
157
|
+
function collectFnNames(tokens) { // dsh-skip-complexity: 函数名收集器(function/const function/箭头三形态扫描,拆分后职责单一)
|
|
158
|
+
const fnNames = new Set();
|
|
159
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
160
|
+
const t = tokens[i];
|
|
161
|
+
if (t.type === 'ident' && t.value === 'function') {
|
|
162
|
+
const nx = tokens[i + 1];
|
|
163
|
+
if (nx && nx.type === 'ident') { fnNames.add(nx.value); continue; }
|
|
164
|
+
// function (xxx) { —— const f = function 场景
|
|
165
|
+
for (let k = i - 1; k >= Math.max(0, i - 5); k--) {
|
|
166
|
+
const tk = tokens[k];
|
|
167
|
+
if (tk && tk.type === 'ident' && ['const', 'let', 'var'].includes(tk.value)) {
|
|
168
|
+
const nm = tokens[k + 1];
|
|
169
|
+
if (nm && nm.type === 'ident') { fnNames.add(nm.value); break; }
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} else if (t.type === 'ident' && ['const', 'let', 'var'].includes(t.value)) {
|
|
173
|
+
const nm = tokens[i + 1];
|
|
174
|
+
// const NAME = (...) => { 箭头函数(后续有 =>)
|
|
175
|
+
if (nm && nm.type === 'ident') {
|
|
176
|
+
for (let j = i + 2; j < Math.min(tokens.length, i + 30); j++) {
|
|
177
|
+
if (tokens[j].type === 'punct' && tokens[j].value === '=>') { fnNames.add(nm.value); break; }
|
|
178
|
+
if (tokens[j].type === 'punct' && tokens[j].value === ';') break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return fnNames;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** ① return 暴露:return { fnA, fnB } / return fnA。 */
|
|
187
|
+
function scanReturnExposed(tokens, fnNames, scope) {
|
|
188
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
189
|
+
const t = tokens[i];
|
|
190
|
+
if (!(t.type === 'ident' && t.value === 'return')) continue;
|
|
191
|
+
for (let j = i + 1; j < Math.min(tokens.length, i + RETURN_SCAN_LOOKAHEAD); j++) {
|
|
192
|
+
const tj = tokens[j];
|
|
193
|
+
if (tj.type === 'punct' && (tj.value === ';' || tj.value === '}')) break;
|
|
194
|
+
if (tj.type === 'ident' && fnNames.has(tj.value) && !scope.has(tj.value)) scope.set(tj.value, 'public');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** ② this.xxx = fn / exports.xxx = fn / module.exports = { fn }。 */
|
|
200
|
+
function scanAssignmentExposed(tokens, fnNames, scope) { // dsh-skip-complexity: this/exports/module 赋值暴露扫描(三前缀分支为结构必然)
|
|
201
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
202
|
+
const t = tokens[i];
|
|
203
|
+
if (!(t.type === 'ident' && ['this', 'exports', 'module'].includes(t.value))) continue;
|
|
204
|
+
let j = i + 1;
|
|
205
|
+
if (t.value === 'module') {
|
|
206
|
+
if (!(tokens[j]?.type === 'punct' && tokens[j].value === '.')) continue;
|
|
207
|
+
j += 1;
|
|
208
|
+
if (!(tokens[j]?.type === 'ident' && tokens[j].value === 'exports')) continue;
|
|
209
|
+
j += 1;
|
|
210
|
+
}
|
|
211
|
+
if (tokens[j]?.type === 'punct' && tokens[j].value === '.') {
|
|
212
|
+
j += 1;
|
|
213
|
+
if (tokens[j]?.type === 'punct' && tokens[j].value === '=') { continue; } // 赋值场景下面处理
|
|
214
|
+
j += 1; // exports.xxx
|
|
215
|
+
}
|
|
216
|
+
// 找 = 后的 ident(function 名)
|
|
217
|
+
for (let k = j; k < Math.min(tokens.length, j + 12); k++) {
|
|
218
|
+
const tk = tokens[k];
|
|
219
|
+
if (tk.type === 'punct' && tk.value === '=') {
|
|
220
|
+
const rhs = tokens[k + 1];
|
|
221
|
+
if (rhs && rhs.type === 'ident' && fnNames.has(rhs.value)) scope.set(rhs.value, 'public');
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
if (tk.type === 'punct' && tk.value === ';') break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** ③ 作为参数传递(回调/未知路径):call(fnA) / arr.map(fnA)。 */
|
|
230
|
+
function scanCallbackPassed(tokens, fnNames, scope) {
|
|
231
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
232
|
+
const t = tokens[i];
|
|
233
|
+
if (!(t.type === 'ident' && fnNames.has(t.value))) continue;
|
|
234
|
+
if (scope.has(t.value)) continue;
|
|
235
|
+
const prevT = tokens[i - 1];
|
|
236
|
+
const nextT = tokens[i + 1];
|
|
237
|
+
const passedAsArg = prevT && (prevT.type === 'punct' && (prevT.value === '(' || prevT.value === ','))
|
|
238
|
+
&& nextT && (nextT.type === 'punct' && (nextT.value === ')' || nextT.value === ','));
|
|
239
|
+
if (passedAsArg) scope.set(t.value, 'callback');
|
|
240
|
+
}
|
|
241
|
+
}
|
package/lib/ast/shell.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 分层位置(三层审计架构)② 具体实现 lib/ast/*(token 级判定):
|
|
3
|
+
* shell 相关精筛——cd 到动态路径、写操作目标命中 .gitignore。
|
|
4
|
+
* 调用包装在 lib/checks/common.js(转成应报行集合后由 regex.js 消费)。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
8
|
+
import { join, resolve } from 'node:path';
|
|
9
|
+
import { makeCodeLineFilter } from './code-lines.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* astConfirmKind: "shell-cd-dynamic" 的精筛——cd 到动态路径($VAR / $(cmd))且无失败兜底的行。
|
|
13
|
+
* 报出条件(全部满足才报):
|
|
14
|
+
* ① 候选行在代码里(注释/字符串内不报)
|
|
15
|
+
* ② 含 cd 命令且目标含 $(变量/命令替换 → 目录运行时才知道,可能不存在)
|
|
16
|
+
* ③ 非脚本自我定位(`cd "$(dirname "$0")"` / `"${BASH_SOURCE[0]%/*}"`——目录必然存在,豁免)
|
|
17
|
+
* ④ 同行无 `||` 兜底(cd ... || exit/return/echo)
|
|
18
|
+
* ⑤ 非 `&&` 链结尾(cd x && ... 失败即断链)
|
|
19
|
+
* @param {string} text 文件全文
|
|
20
|
+
* @returns {Set<number>} 应报的行号集合
|
|
21
|
+
*/
|
|
22
|
+
export function shellCdDynamicLines(text) {
|
|
23
|
+
const t = String(text || '');
|
|
24
|
+
const lines = t.split('\n');
|
|
25
|
+
const candidates = [];
|
|
26
|
+
for (let i = 0; i < lines.length; i++) {
|
|
27
|
+
if (lines[i] && /(?:^|[;&])\s*cd\s+\S+/.test(lines[i])) candidates.push(i + 1);
|
|
28
|
+
}
|
|
29
|
+
if (!candidates.length) return new Set();
|
|
30
|
+
const codeFilter = makeCodeLineFilter(t, candidates);
|
|
31
|
+
const out = new Set();
|
|
32
|
+
for (const ln of candidates) {
|
|
33
|
+
const line = lines[ln - 1];
|
|
34
|
+
const match = /(?:^|[;&])\s*cd\s+(\S+)/.exec(line);
|
|
35
|
+
if (!match) continue;
|
|
36
|
+
const target = match[1];
|
|
37
|
+
if (!target.includes('$')) continue; // ② 字面量路径(仓库内存在性由人工/仓库级判定)
|
|
38
|
+
if (/dirname|BASH_SOURCE|%\/\*/.test(target)) continue; // ③ 自我定位豁免
|
|
39
|
+
if (!codeFilter.isCode(ln)) continue; // ① 注释/字符串内不报
|
|
40
|
+
if (/\|\|/.test(line.slice(match.index))) continue; // ④ 有兜底
|
|
41
|
+
if (/&&/.test(line.slice(match.index))) continue; // ⑤ && 链(cd 失败即断链)
|
|
42
|
+
out.add(ln);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* ────────────────── .gitignore 解析(轻量版,够规则判定用) ────────────────── */
|
|
48
|
+
|
|
49
|
+
/** 解析 .gitignore:逐行 → { negate, re }(! 取反 / 目录尾 / glob 通配)。 */
|
|
50
|
+
function parseGitignore(text) {
|
|
51
|
+
const patterns = [];
|
|
52
|
+
for (const raw of String(text || '').split('\n')) {
|
|
53
|
+
let line = raw.trim();
|
|
54
|
+
if (!line || line.startsWith('#')) continue;
|
|
55
|
+
let negate = false;
|
|
56
|
+
if (line.startsWith('!')) { negate = true; line = line.slice(1).trim(); }
|
|
57
|
+
if (!line) continue;
|
|
58
|
+
const dirOnly = line.endsWith('/');
|
|
59
|
+
const anchored = line.startsWith('/');
|
|
60
|
+
line = line.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
61
|
+
if (!line) continue;
|
|
62
|
+
let re = '';
|
|
63
|
+
for (const ch of line) {
|
|
64
|
+
if (ch === '*') re += '[^/]*';
|
|
65
|
+
else if (ch === '?') re += '[^/]';
|
|
66
|
+
else if (ch === '.') re = re + '\\.';
|
|
67
|
+
else if ('/\\^$+{}()|'.includes(ch)) re += '\\' + ch;
|
|
68
|
+
else re += ch;
|
|
69
|
+
}
|
|
70
|
+
const tail = dirOnly ? '(?:/|$)' : '(?:/|$)';
|
|
71
|
+
// 含 / 或前导 / 的模式相对仓库根匹配;否则匹配任意层级 basename
|
|
72
|
+
const body = (anchored || line.includes('/')) ? `(?:^|.*/)${re}${tail}` : `(?:^|.*/)${re}${tail}`;
|
|
73
|
+
patterns.push({ negate, re: new RegExp(body) });
|
|
74
|
+
}
|
|
75
|
+
return patterns;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 目标是否被规则忽略(最后一个匹配生效;! 取反恢复)。 */
|
|
79
|
+
function isIgnored(target, patterns) {
|
|
80
|
+
let ignored = false;
|
|
81
|
+
for (const { negate, re } of patterns) {
|
|
82
|
+
if (re.test(target)) ignored = !negate;
|
|
83
|
+
}
|
|
84
|
+
return ignored;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 从行里提取写操作的目标路径(node 字面量 / shell 命令参数)。 */
|
|
88
|
+
function extractWriteTarget(line) {
|
|
89
|
+
const match = /\b(writeFile|writeFileSync|appendFile|appendFileSync|createWriteStream|mkdir|mkdirSync)\s*\(\s*['"]([^'"]+)['"]/.exec(line);
|
|
90
|
+
if (match && match[2]) return match[2];
|
|
91
|
+
const sh = /(?:^|[;&])\s*mkdir\s+(?:-p\s+)?['"]?([^'"\s]+)['"]?/.exec(line);
|
|
92
|
+
if (sh) return sh[1];
|
|
93
|
+
return '';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* astConfirmKind: "write-into-gitignored" 的精筛——写文件/目录的目标路径命中仓库
|
|
98
|
+
* .gitignore(产出目录被忽略、可能不存在)的行。
|
|
99
|
+
* 依赖仓库上下文 repoPath(读 .gitignore);无 repoPath(单文件审计)或 .gitignore
|
|
100
|
+
* 不存在时返回空集合(不报——没有仓库上下文不臆测)。
|
|
101
|
+
* 豁免:node_modules 内写入(安装产物)、/tmp 前缀(临时目录)、.gitignore 中 ! 取反。
|
|
102
|
+
* @param {string} text 文件全文
|
|
103
|
+
* @param {string} [repoPath] 仓库根目录(auditFile 传入的 opts.repoPath)
|
|
104
|
+
* @returns {Set<number>} 应报的行号集合
|
|
105
|
+
*/
|
|
106
|
+
export function writeIntoGitignoredLines(text, repoPath = '') {
|
|
107
|
+
const t = String(text || '');
|
|
108
|
+
if (!t || !repoPath) return new Set();
|
|
109
|
+
const gi = resolve(join(String(repoPath), '.gitignore'));
|
|
110
|
+
if (!existsSync(gi)) return new Set();
|
|
111
|
+
let giText = '';
|
|
112
|
+
try { giText = readFileSync(gi, 'utf8'); } catch { return new Set(); }
|
|
113
|
+
const patterns = parseGitignore(giText);
|
|
114
|
+
if (!patterns.length) return new Set();
|
|
115
|
+
const lines = t.split('\n');
|
|
116
|
+
const candidates = [];
|
|
117
|
+
for (let i = 0; i < lines.length; i++) {
|
|
118
|
+
if (lines[i] && extractWriteTarget(lines[i])) candidates.push(i + 1);
|
|
119
|
+
}
|
|
120
|
+
if (!candidates.length) return new Set();
|
|
121
|
+
const codeFilter = makeCodeLineFilter(t, candidates);
|
|
122
|
+
const out = new Set();
|
|
123
|
+
for (const ln of candidates) {
|
|
124
|
+
const line = lines[ln - 1];
|
|
125
|
+
const target = extractWriteTarget(line);
|
|
126
|
+
if (!target || !codeFilter.isCode(ln)) continue;
|
|
127
|
+
if (/node_modules/.test(target) || /^\/?tmp\b/.test(target)) continue; // 豁免
|
|
128
|
+
if (isIgnored(target, patterns)) out.add(ln);
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|