♻️ refactor(vscode): 文件注释的存法抽成按路径记的通用表,给问答共用

This commit is contained in:
cruldra committed 2026-09-26 21:12:34 +08:00
1 parent 2282646a43
commit badc722640
8 files changed
+378 -320

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
import { describe, expect, it } from 'vitest'
import { changedKeys, dropEntries, entriesUnder, moveEntries, normalizeKey, placeEntries, toPathKey } from './pathMap'
describe('路径的键', () => {
it('相对工作区文件夹、用 / 分隔', () => {
expect(toPathKey('/work/luffy', '/work/luffy/backend/src/campaign')).toBe('backend/src/campaign')
expect(toPathKey('/work/luffy/', '/work/luffy/a.ts')).toBe('a.ts')
// Windows 上 Uri.path 也是 / 分隔,盘符在最前面
expect(toPathKey('/c:/work/luffy', '/c:/work/luffy/src/a.ts')).toBe('src/a.ts')
})
it('工作区文件夹本身和外面的路径没有键', () => {
expect(toPathKey('/work/luffy', '/work/luffy')).toBeUndefined()
expect(toPathKey('/work/luffy', '/work/other/a.ts')).toBeUndefined()
expect(toPathKey('/work/luffy', '/work/luffy-2/a.ts')).toBeUndefined()
})
it('手写的键规范成同一种写法,不合法的丢掉', () => {
expect(normalizeKey('./src/a.ts')).toBe('src/a.ts')
expect(normalizeKey('src//campaign/')).toBe('src/campaign')
expect(normalizeKey('src/x/../a.ts')).toBe('src/a.ts')
expect(normalizeKey('')).toBeUndefined()
expect(normalizeKey('.')).toBeUndefined()
expect(normalizeKey('../a.ts')).toBeUndefined()
expect(normalizeKey('/etc/passwd')).toBeUndefined()
})
})
describe('改名、挪位置', () => {
const notes = {
'campaign': '营销活动',
'campaign/benefits.py': '权益发放',
'campaign/console/views.py': '后台',
'campaign2/x.py': '名字只是前缀相同',
'user/routes.py': '用户接口',
}
it('文件改名,注释跟着走', () => {
expect(moveEntries(notes, 'user/routes.py', 'user/api.py')).toEqual({
'campaign': '营销活动',
'campaign/benefits.py': '权益发放',
'campaign/console/views.py': '后台',
'campaign2/x.py': '名字只是前缀相同',
'user/api.py': '用户接口',
})
})
it('目录挪位置,里面各层的注释一起走,名字只是前缀相同的不动', () => {
expect(moveEntries(notes, 'campaign', 'marketing/campaign')).toEqual({
'marketing/campaign': '营销活动',
'marketing/campaign/benefits.py': '权益发放',
'marketing/campaign/console/views.py': '后台',
'campaign2/x.py': '名字只是前缀相同',
'user/routes.py': '用户接口',
})
})
it('没有注释的目录挪走,里面有注释的文件照样跟着走', () => {
expect(moveEntries({ 'src/a/b.ts': 'B' }, 'src/a', 'lib/a')).toEqual({ 'lib/a/b.ts': 'B' })
})
it('挪到另一个工作区文件夹:先取出再放进去', () => {
const moving = entriesUnder(notes, 'campaign')
expect(moving).toEqual({ '': '营销活动', 'benefits.py': '权益发放', 'console/views.py': '后台' })
expect(placeEntries({ 'other.ts': 'O' }, 'pkg/campaign', moving)).toEqual({
'other.ts': 'O',
'pkg/campaign': '营销活动',
'pkg/campaign/benefits.py': '权益发放',
'pkg/campaign/console/views.py': '后台',
})
})
})
describe('删除', () => {
it('删目录连带里面各层的注释,名字只是前缀相同的留着', () => {
expect(dropEntries({
'campaign': '营销活动',
'campaign/benefits.py': '权益发放',
'campaign/console/views.py': '后台',
'campaign2/x.py': '留着',
}, 'campaign')).toEqual({ 'campaign2/x.py': '留着' })
})
it('删文件只删它自己', () => {
expect(dropEntries({ 'a.ts': 'A', 'b.ts': 'B' }, 'a.ts')).toEqual({ 'b.ts': 'B' })
})
})
describe('变了哪些行', () => {
it('增、删、改都算,没变的不算', () => {
expect(changedKeys({ a: '1', b: '2', c: '3' }, { a: '1', b: '二', d: '4' }).sort()).toEqual(['b', 'c', 'd'])
expect(changedKeys({ a: '1' }, { a: '1' })).toEqual([])
})
})