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
+12 -81
View File
@@ -1,100 +1,31 @@
import { describe, it, expect, beforeEach } from 'vitest';
import {
pathRemoveAt,
pathMoveUp,
pathMoveDown,
pathClean,
batchRemoveAt,
} from '../../src/core/path-manager';
import { StringList } from '../../src/core/string-list';
import { describe, it, expect } from 'vitest';
import { pathClean } from '../../src/core/path-manager';
// 模拟验证函数:所有路径都"有效"
const alwaysValid = () => true;
// 模拟验证函数:C:\\Invalid 无效
const validateFn = (path: string) => !path.includes('Invalid');
describe('pathRemoveAt', () => {
it('删除指定索引', () => {
const list = StringList.fromArray(['a', 'b', 'c']);
pathRemoveAt(list, 1);
expect(list.toArray()).toEqual(['a', 'c']);
});
});
describe('pathMoveUp', () => {
it('上移元素', () => {
const list = StringList.fromArray(['a', 'b', 'c']);
pathMoveUp(list, 1);
expect(list.toArray()).toEqual(['b', 'a', 'c']);
});
it('第一个元素不能上移', () => {
const list = StringList.fromArray(['a', 'b']);
expect(pathMoveUp(list, 0)).toBe(false);
expect(list.toArray()).toEqual(['a', 'b']);
});
it('无效索引不能上移', () => {
const list = StringList.fromArray(['a']);
expect(pathMoveUp(list, -1)).toBe(false);
expect(pathMoveUp(list, 5)).toBe(false);
});
});
describe('pathMoveDown', () => {
it('下移元素', () => {
const list = StringList.fromArray(['a', 'b', 'c']);
pathMoveDown(list, 0);
expect(list.toArray()).toEqual(['b', 'a', 'c']);
});
it('最后一个元素不能下移', () => {
const list = StringList.fromArray(['a', 'b']);
expect(pathMoveDown(list, 1)).toBe(false);
});
});
describe('batchRemoveAt', () => {
it('批量删除(按从大到小排序)', () => {
const list = StringList.fromArray(['a', 'b', 'c', 'd', 'e']);
batchRemoveAt(list, [0, 2, 4]);
expect(list.toArray()).toEqual(['b', 'd']);
});
it('删除乱序索引', () => {
const list = StringList.fromArray(['a', 'b', 'c', 'd']);
batchRemoveAt(list, [3, 0]);
expect(list.toArray()).toEqual(['b', 'c']);
});
});
describe('pathClean', () => {
it('移除无效路径', () => {
const list = StringList.fromArray(['C:\\Valid', 'C:\\Invalid', 'D:\\Valid']);
const removed = pathClean(list, validateFn);
expect(list.toArray()).toEqual(['C:\\Valid', 'D:\\Valid']);
const [kept, removed] = pathClean(['C:\\Valid', 'C:\\Invalid', 'D:\\Valid'], validateFn);
expect(kept).toEqual(['C:\\Valid', 'D:\\Valid']);
expect(removed).toEqual(['C:\\Invalid']);
});
it('移除重复路径保留一个', () => {
const list = StringList.fromArray(['C:\\Valid', 'C:\\Valid', 'D:\\Valid']);
const removed = pathClean(list, alwaysValid);
expect(list.length).toBe(2);
expect(removed.length).toBeGreaterThanOrEqual(1);
it('移除重复路径保留一个', () => {
const [kept, removed] = pathClean(['C:\\Valid', 'C:\\Valid', 'D:\\Valid'], alwaysValid);
expect(kept.length).toBe(2);
expect(removed.length).toBe(1);
});
it('全部有效无变化', () => {
const list = StringList.fromArray(['C:\\a', 'D:\\b']);
const removed = pathClean(list, alwaysValid);
expect(list.toArray()).toEqual(['C:\\a', 'D:\\b']);
const [kept, removed] = pathClean(['C:\\a', 'D:\\b'], alwaysValid);
expect(kept).toEqual(['C:\\a', 'D:\\b']);
expect(removed.length).toBe(0);
});
it('全部无效全部移除', () => {
const list = StringList.fromArray(['C:\\Invalid1', 'C:\\Invalid2']);
const removed = pathClean(list, validateFn);
expect(list.length).toBe(0);
const [kept, removed] = pathClean(['C:\\Invalid1', 'C:\\Invalid2'], validateFn);
expect(kept.length).toBe(0);
expect(removed.length).toBe(2);
});
});
-91
View File
@@ -1,91 +0,0 @@
import { describe, it, expect } from 'vitest';
import { StringList } from '../../src/core/string-list';
describe('StringList', () => {
it('初始为空', () => {
const list = new StringList();
expect(list.length).toBe(0);
});
it('添加和读取元素', () => {
const list = new StringList();
list.add('C:\\Windows');
list.add('C:\\Users');
expect(list.length).toBe(2);
expect(list.get(0)).toBe('C:\\Windows');
expect(list.get(1)).toBe('C:\\Users');
expect(list.get(99)).toBeUndefined();
});
it('在指定位置插入', () => {
const list = new StringList();
list.add('a');
list.add('c');
list.insertAt(1, 'b');
expect(list.toArray()).toEqual(['a', 'b', 'c']);
});
it('在开头插入', () => {
const list = StringList.fromArray(['b', 'c']);
list.insertAt(0, 'a');
expect(list.toArray()).toEqual(['a', 'b', 'c']);
});
it('删除指定位置', () => {
const list = StringList.fromArray(['a', 'b', 'c']);
list.removeAt(1);
expect(list.toArray()).toEqual(['a', 'c']);
});
it('设置元素', () => {
const list = StringList.fromArray(['old']);
list.set(0, 'new');
expect(list.get(0)).toBe('new');
});
it('不区分大小写查找', () => {
const list = StringList.fromArray(['C:\\Windows', 'C:\\Users']);
expect(list.contains('c:\\windows')).toBe(true);
expect(list.contains('C:\\WINDOWS')).toBe(true);
expect(list.contains('C:\\Other')).toBe(false);
});
it('不区分大小写索引', () => {
const list = StringList.fromArray(['C:\\Windows', 'C:\\Users']);
expect(list.indexOfIgnoreCase('c:\\windows')).toBe(0);
expect(list.indexOfIgnoreCase('c:\\users')).toBe(1);
expect(list.indexOfIgnoreCase('nope')).toBe(-1);
});
it('交换元素', () => {
const list = StringList.fromArray(['a', 'b']);
list.swap(0, 1);
expect(list.toArray()).toEqual(['b', 'a']);
});
it('清空', () => {
const list = StringList.fromArray(['a', 'b', 'c']);
list.clear();
expect(list.length).toBe(0);
});
it('深拷贝', () => {
const original = StringList.fromArray(['a', 'b']);
const cloned = original.clone();
cloned.set(0, 'modified');
expect(original.get(0)).toBe('a');
expect(cloned.get(0)).toBe('modified');
});
it('fromArray 和 toArray', () => {
const arr = ['x', 'y', 'z'];
const list = StringList.fromArray(arr);
expect(list.toArray()).toEqual(arr);
expect(list.length).toBe(3);
});
it('all 返回只读数组', () => {
const list = StringList.fromArray(['a', 'b']);
expect(list.all).toEqual(['a', 'b']);
});
});
+66 -151
View File
@@ -1,223 +1,138 @@
import { describe, it, expect, beforeEach } from 'vitest';
import {
UndoRedoManager,
OperationType,
TargetType,
type OpRecord,
} from '../../src/core/undo-redo';
import { StringList } from '../../src/core/string-list';
import { UndoRedoManager, OperationType, TargetType, type OpRecord } from '../../src/core/undo-redo';
function makeRecord(
type: OperationType,
target: TargetType,
index: number,
count: number,
oldPaths: string[],
newPaths: string[],
): OpRecord {
function makeRecord(type: OperationType, target: TargetType, index: number, count: number, oldPaths: string[], newPaths: string[]): OpRecord {
return { type, target, index, count, oldPaths, newPaths };
}
describe('UndoRedoManager', () => {
let mgr: UndoRedoManager;
let sysPaths: StringList;
let userPaths: StringList;
let sys: string[];
let user: string[];
beforeEach(() => {
mgr = new UndoRedoManager(50);
sysPaths = StringList.fromArray(['C:\\Windows', 'C:\\Program Files']);
userPaths = StringList.fromArray(['C:\\Users\\me\\AppData']);
sys = ['C:\\Windows', 'C:\\Program Files'];
user = ['C:\\Users\\me\\AppData'];
});
// ── 基本状态 ──
it('初始不可撤销不可重做', () => {
expect(mgr.canUndo()).toBe(false);
expect(mgr.canRedo()).toBe(false);
});
// ── ADD ──
it('ADD 撤销/重做', () => {
sysPaths.add('C:\\NewPath');
sys.push('C:\\NewPath');
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 2, 1, [], ['C:\\NewPath']));
mgr.push(
makeRecord(OperationType.ADD, TargetType.SYSTEM, 2, 1, [], ['C:\\NewPath']),
);
const u = mgr.undo(sys, user)!;
expect(u[0]).toEqual(['C:\\Windows', 'C:\\Program Files']);
expect(mgr.canUndo()).toBe(true);
mgr.undo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Windows', 'C:\\Program Files']);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Windows', 'C:\\Program Files', 'C:\\NewPath']);
const r = mgr.redo(...u)!;
expect(r[0]).toEqual(['C:\\Windows', 'C:\\Program Files', 'C:\\NewPath']);
});
// ── DELETE ──
it('DELETE 撤销/重做', () => {
const removed = sysPaths.get(0)!;
mgr.push(
makeRecord(OperationType.DELETE, TargetType.SYSTEM, 0, 1, [removed], []),
);
const removed = sys[0];
mgr.push(makeRecord(OperationType.DELETE, TargetType.SYSTEM, 0, 1, [removed], []));
sys.splice(0, 1);
sysPaths.removeAt(0);
const u = mgr.undo(sys, user)!;
expect(u[0][0]).toBe(removed);
mgr.undo(sysPaths, userPaths);
expect(sysPaths.get(0)).toBe(removed);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Program Files']);
const r = mgr.redo(...u)!;
expect(r[0]).toEqual(['C:\\Program Files']);
});
// ── EDIT ──
it('EDIT 撤销/重做', () => {
const oldVal = sysPaths.get(0)!;
mgr.push(
makeRecord(OperationType.EDIT, TargetType.SYSTEM, 0, 1, [oldVal], ['C:\\Edited']),
);
mgr.push(makeRecord(OperationType.EDIT, TargetType.SYSTEM, 0, 1, ['C:\\Windows'], ['C:\\Edited']));
sys[0] = 'C:\\Edited';
sysPaths.set(0, 'C:\\Edited');
const u = mgr.undo(sys, user)!;
expect(u[0][0]).toBe('C:\\Windows');
mgr.undo(sysPaths, userPaths);
expect(sysPaths.get(0)).toBe(oldVal);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.get(0)).toBe('C:\\Edited');
const r = mgr.redo(...u)!;
expect(r[0][0]).toBe('C:\\Edited');
});
// ── MOVE_UP ──
it('MOVE_UP 撤销/重做', () => {
mgr.push(
makeRecord(OperationType.MOVE_UP, TargetType.SYSTEM, 1, 1, [], []),
);
mgr.push(makeRecord(OperationType.MOVE_UP, TargetType.SYSTEM, 1, 1, [], []));
[sys[0], sys[1]] = [sys[1], sys[0]];
sysPaths.swap(0, 1);
const u = mgr.undo(sys, user)!;
expect(u[0]).toEqual(['C:\\Windows', 'C:\\Program Files']);
expect(sysPaths.toArray()).toEqual(['C:\\Program Files', 'C:\\Windows']);
mgr.undo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Windows', 'C:\\Program Files']);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Program Files', 'C:\\Windows']);
const r = mgr.redo(...u)!;
expect(r[0]).toEqual(['C:\\Program Files', 'C:\\Windows']);
});
// ── MOVE_DOWN ──
it('MOVE_DOWN 撤销/重做', () => {
mgr.push(
makeRecord(OperationType.MOVE_DOWN, TargetType.SYSTEM, 0, 1, [], []),
);
mgr.push(makeRecord(OperationType.MOVE_DOWN, TargetType.SYSTEM, 0, 1, [], []));
[sys[0], sys[1]] = [sys[1], sys[0]];
sysPaths.swap(0, 1);
mgr.undo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Windows', 'C:\\Program Files']);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(['C:\\Program Files', 'C:\\Windows']);
const u = mgr.undo(sys, user)!;
expect(u[0]).toEqual(['C:\\Windows', 'C:\\Program Files']);
});
// ── CLEAN ──
it('CLEAN 撤销/重做', () => {
const oldPaths = sysPaths.toArray();
const newPaths = ['C:\\Windows']; // 假设 Program Files 被清理掉了
const old = [...sys];
const cleaned = ['C:\\Windows'];
mgr.push(makeRecord(OperationType.CLEAN, TargetType.SYSTEM, 0, 2, old, cleaned));
sys = cleaned;
mgr.push(
makeRecord(OperationType.CLEAN, TargetType.SYSTEM, 0, 2, oldPaths, newPaths),
);
const u = mgr.undo(sys, user)!;
expect(u[0]).toEqual(old);
sysPaths.clear();
for (const p of newPaths) sysPaths.add(p);
mgr.undo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(oldPaths);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(newPaths);
const r = mgr.redo(...u)!;
expect(r[0]).toEqual(cleaned);
});
// ── CLEAR ──
it('CLEAR 撤销/重做', () => {
const oldPaths = sysPaths.toArray();
const old = [...sys];
mgr.push(makeRecord(OperationType.CLEAR, TargetType.SYSTEM, 0, 2, old, []));
sys = [];
mgr.push(
makeRecord(OperationType.CLEAR, TargetType.SYSTEM, 0, 2, oldPaths, []),
);
const u = mgr.undo(sys, user)!;
expect(u[0]).toEqual(old);
sysPaths.clear();
mgr.undo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(oldPaths);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.length).toBe(0);
const r = mgr.redo(...u)!;
expect(r[0]).toEqual([]);
});
// ── IMPORT ──
it('IMPORT 撤销/重做', () => {
const oldPaths = sysPaths.toArray();
const old = [...sys];
const imported = ['C:\\New1', 'C:\\New2'];
mgr.push(makeRecord(OperationType.IMPORT, TargetType.SYSTEM, 0, 2, old, imported));
sys = imported;
mgr.push(
makeRecord(OperationType.IMPORT, TargetType.SYSTEM, 0, 2, oldPaths, imported),
);
const u = mgr.undo(sys, user)!;
expect(u[0]).toEqual(old);
sysPaths.clear();
for (const p of imported) sysPaths.add(p);
mgr.undo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(oldPaths);
mgr.redo(sysPaths, userPaths);
expect(sysPaths.toArray()).toEqual(imported);
const r = mgr.redo(...u)!;
expect(r[0]).toEqual(imported);
});
// ── 重做分支截断 ──
it('新操作后截断重做分支', () => {
mgr.push(
makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], ['first']),
);
mgr.undo(sysPaths, userPaths);
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], ['first']));
mgr.undo(sys, user);
expect(mgr.canRedo()).toBe(true);
// 推入新操作,重做分支被截断
mgr.push(
makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], ['second']),
);
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], ['second']));
expect(mgr.canRedo()).toBe(false);
});
// ── 历史限制 ──
it('超出最大历史容量时移除最旧记录', () => {
const small = new UndoRedoManager(3);
for (let i = 0; i < 5; i++) {
small.push(
makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], [`path_${i}`]),
);
small.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], [`path_${i}`]));
}
expect(small.historyLength).toBe(3);
});
// ── USER 目标 ──
it('操作 USER 路径', () => {
userPaths.add('C:\\NewUserPath');
mgr.push(
makeRecord(OperationType.ADD, TargetType.USER, 1, 1, [], ['C:\\NewUserPath']),
);
mgr.undo(sysPaths, userPaths);
expect(userPaths.toArray()).toEqual(['C:\\Users\\me\\AppData']);
expect(sysPaths.toArray()).toEqual(['C:\\Windows', 'C:\\Program Files']);
user.push('C:\\NewUserPath');
mgr.push(makeRecord(OperationType.ADD, TargetType.USER, 1, 1, [], ['C:\\NewUserPath']));
const u = mgr.undo(sys, user)!;
expect(u[1]).toEqual(['C:\\Users\\me\\AppData']);
expect(u[0]).toEqual(['C:\\Windows', 'C:\\Program Files']);
});
});