refactor: 全面代码质量提升 — StringList→string[], strict 模式, 死代码清理

架构重构:
- StringList 类替换为不可变 string[](消除 dataVersion hack,Zustand 自然检测变化)
- UndoRedoManager.undo/redo 返回新数组而非原地修改
- 删除 dataVersion 字段和 _bumpVersion()
- 启用 TypeScript strict 模式

死代码清理:
- 删除 string-list.ts, string-list.test.ts, use-path-validation.ts
- Rust AppError 保留供未来使用

功能修复:
- importFromJson 添加 try/catch
- handleClean 使用真实格式验证替代 () => true
- savePaths 保存前调用 backup_registry,处理部分保存失败
- importFromJson 校验非 object 类型输入

i18n 完善:
- MergePreview/StatusBar 硬编码中文 → t() 调用
- 新增 merge.* 和 status.* 翻译键

Rust 改进:
- registry.rs 抽取 load_paths/save_paths 通用函数,消除重复
- registry 新增 6 个单元测试(split/join/roundtrip)
- backup.rs 时间戳加毫秒防覆盖,回退路径改为 home_dir

元数据:
- package.json 名称→patheditor, 版本→4.0.0
- 新增 CHANGELOG.md
- 移除 UndoRedoButtons 废弃注释
- tsconfig 添加 strict:true

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 00:26:27 +08:00
parent 2ceec54790
commit bfd114d80f
21 changed files with 410 additions and 836 deletions
+14 -59
View File
@@ -1,92 +1,47 @@
/**
* 路径管理器 — 对应 C 版 path_manager.c
* 提供路径增删移清理等 CRUD 操作的纯逻辑
* 路径管理器 — 不可变的 string[] 操作
*/
import { StringList } from './string-list';
/** 删除指定索引的路径 */
export function pathRemoveAt(list: StringList, index: number): void {
list.removeAt(index);
}
/** 上移路径(调整优先级) */
export function pathMoveUp(list: StringList, index: number): boolean {
if (index <= 0 || index >= list.length) return false;
list.swap(index, index - 1);
return true;
}
/** 下移路径(调整优先级) */
export function pathMoveDown(list: StringList, index: number): boolean {
if (index < 0 || index >= list.length - 1) return false;
list.swap(index, index + 1);
return true;
}
/** 标记路径的有效性(调用方负责提供验证函数和展开 env vars) */
export interface PathValidation {
isValid: boolean;
isDuplicate: boolean;
isEnvVar: boolean;
}
/**
* 分析路径列表中各条目的状态
* validateFn: 验证路径是否有效(需调用 Rust validate_path
*/
export function analyzePaths(
list: StringList,
paths: readonly string[],
validateFn: (path: string) => boolean,
): PathValidation[] {
const result: PathValidation[] = [];
const seen = new Set<string>();
for (let i = 0; i < list.length; i++) {
const path = list.get(i)!;
for (const path of paths) {
const lower = path.toLowerCase();
const isDuplicate = seen.has(lower);
seen.add(lower);
result.push({
isValid: validateFn(path),
isDuplicate,
isEnvVar: path.includes('%'),
});
result.push({ isValid: validateFn(path), isDuplicate, isEnvVar: path.includes('%') });
}
return result;
}
/**
* 批量删除选中的索引(从大到小排序以避免偏移)
*/
export function batchRemoveAt(list: StringList, indices: number[]): void {
const sorted = [...indices].sort((a, b) => b - a);
for (const idx of sorted) {
list.removeAt(idx);
}
}
/**
* 一键清理 — 移除无效路径和重复路径
* 从后往前操作以避免索引偏移
* 返回被移除的路径数量
*/
/** 从数组中移除无效和重复路径,返回 [新数组, 被移除的路径] */
export function pathClean(
list: StringList,
paths: readonly string[],
validateFn: (path: string) => boolean,
): string[] {
const analysis = analyzePaths(list, validateFn);
): [string[], string[]] {
const analysis = analyzePaths(paths, validateFn);
const kept: string[] = [];
const removed: string[] = [];
for (let i = analysis.length - 1; i >= 0; i--) {
for (let i = 0; i < paths.length; i++) {
const a = analysis[i];
// 移除无效或重复的路径
if (!a.isValid || a.isDuplicate) {
removed.unshift(list.get(i)!);
list.removeAt(i);
removed.push(paths[i]);
} else {
kept.push(paths[i]);
}
}
return removed;
return [kept, removed];
}