Skip to content

Commit 6f399e2

Browse files
committed
refactor: deprecate createPrefixTree
1 parent f651961 commit 6f399e2

File tree

6 files changed

+78
-77
lines changed

6 files changed

+78
-77
lines changed

src/codegen/generateRouteMap.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import { generateRouteNamedMap } from './generateRouteMap'
3-
import { createPrefixTree } from '../core/tree'
3+
import { createPrefixTree, PrefixTree } from '../core/tree'
44
import { resolveOptions } from '../options'
55

66
const DEFAULT_OPTIONS = resolveOptions({})
@@ -14,7 +14,7 @@ function formatExports(exports: string) {
1414

1515
describe('generateRouteNamedMap', () => {
1616
it('works with some paths at root', () => {
17-
const tree = createPrefixTree(DEFAULT_OPTIONS)
17+
const tree = new PrefixTree(DEFAULT_OPTIONS)
1818
tree.insert('index.vue')
1919
tree.insert('a.vue')
2020
tree.insert('b.vue')
@@ -30,7 +30,7 @@ describe('generateRouteNamedMap', () => {
3030
})
3131

3232
it('adds params', () => {
33-
const tree = createPrefixTree(DEFAULT_OPTIONS)
33+
const tree = new PrefixTree(DEFAULT_OPTIONS)
3434
tree.insert('[a].vue')
3535
tree.insert('partial-[a].vue')
3636
tree.insert('[[a]].vue') // optional
@@ -52,7 +52,7 @@ describe('generateRouteNamedMap', () => {
5252
})
5353

5454
it('handles params from raw routes', () => {
55-
const tree = createPrefixTree(DEFAULT_OPTIONS)
55+
const tree = new PrefixTree(DEFAULT_OPTIONS)
5656
const a = tree.insertParsedPath(':a', 'a.vue')
5757
const b = tree.insertParsedPath(':b()', 'a.vue')
5858
expect(a.name).toBe('/:a')
@@ -66,7 +66,7 @@ describe('generateRouteNamedMap', () => {
6666
})
6767

6868
it('handles nested params in folders', () => {
69-
const tree = createPrefixTree(DEFAULT_OPTIONS)
69+
const tree = new PrefixTree(DEFAULT_OPTIONS)
7070
tree.insert('n/[a]/index.vue') // normal
7171
tree.insert('n/[a]/other.vue')
7272
tree.insert('n/[a]/[b].vue')
@@ -82,7 +82,7 @@ describe('generateRouteNamedMap', () => {
8282
})
8383

8484
it('adds nested params', () => {
85-
const tree = createPrefixTree(DEFAULT_OPTIONS)
85+
const tree = new PrefixTree(DEFAULT_OPTIONS)
8686
tree.insert('n/[a].vue') // normal
8787
// tree.insert('n/partial-[a].vue') // partial
8888
tree.insert('n/[[a]].vue') // optional
@@ -101,7 +101,7 @@ describe('generateRouteNamedMap', () => {
101101
})
102102

103103
it('nested children', () => {
104-
const tree = createPrefixTree(DEFAULT_OPTIONS)
104+
const tree = new PrefixTree(DEFAULT_OPTIONS)
105105
tree.insert('a/a.vue')
106106
tree.insert('a/b.vue')
107107
tree.insert('a/c.vue')
@@ -125,7 +125,7 @@ describe('generateRouteNamedMap', () => {
125125
})
126126

127127
it('keeps parent path overrides', () => {
128-
const tree = createPrefixTree(DEFAULT_OPTIONS)
128+
const tree = new PrefixTree(DEFAULT_OPTIONS)
129129
const parent = tree.insert('parent.vue')
130130
const child = tree.insert('parent/child.vue')
131131
parent.value.setOverride('parent.vue', { path: '/' })

src/codegen/generateRouteRecords.spec.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { basename } from 'pathe'
22
import { describe, expect, it } from 'vitest'
3-
import { createPrefixTree, TreeNode } from '../core/tree'
3+
import { PrefixTree, TreeNode } from '../core/tree'
44
import { ResolvedOptions, resolveOptions } from '../options'
55
import { generateRouteRecord } from './generateRouteRecords'
66
import { ImportsMap } from '../core/utils'
@@ -13,7 +13,7 @@ describe('generateRouteRecord', () => {
1313
}
1414

1515
it('works with an empty tree', () => {
16-
const tree = createPrefixTree(DEFAULT_OPTIONS)
16+
const tree = new PrefixTree(DEFAULT_OPTIONS)
1717

1818
expect(generateRouteRecordSimple(tree)).toMatchInlineSnapshot(`
1919
"[
@@ -23,29 +23,29 @@ describe('generateRouteRecord', () => {
2323
})
2424

2525
it('works with some paths at root', () => {
26-
const tree = createPrefixTree(DEFAULT_OPTIONS)
26+
const tree = new PrefixTree(DEFAULT_OPTIONS)
2727
tree.insert('a.vue')
2828
tree.insert('b.vue')
2929
tree.insert('c.vue')
3030
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
3131
})
3232

3333
it('handles multiple named views', () => {
34-
const tree = createPrefixTree(DEFAULT_OPTIONS)
34+
const tree = new PrefixTree(DEFAULT_OPTIONS)
3535
tree.insert('foo.vue')
3636
tree.insert('[email protected]')
3737
tree.insert('[email protected]')
3838
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
3939
})
4040

4141
it('handles single named views', () => {
42-
const tree = createPrefixTree(DEFAULT_OPTIONS)
42+
const tree = new PrefixTree(DEFAULT_OPTIONS)
4343
tree.insert('[email protected]')
4444
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
4545
})
4646

4747
it('nested children', () => {
48-
const tree = createPrefixTree(DEFAULT_OPTIONS)
48+
const tree = new PrefixTree(DEFAULT_OPTIONS)
4949
tree.insert('a/a.vue')
5050
tree.insert('a/b.vue')
5151
tree.insert('a/c.vue')
@@ -59,7 +59,7 @@ describe('generateRouteRecord', () => {
5959
})
6060

6161
it('adds children and name when folder and component exist', () => {
62-
const tree = createPrefixTree(DEFAULT_OPTIONS)
62+
const tree = new PrefixTree(DEFAULT_OPTIONS)
6363
tree.insert('a/c.vue')
6464
tree.insert('b/c.vue')
6565
tree.insert('a.vue')
@@ -68,14 +68,14 @@ describe('generateRouteRecord', () => {
6868
})
6969

7070
it('correctly names index.vue files', () => {
71-
const tree = createPrefixTree(DEFAULT_OPTIONS)
71+
const tree = new PrefixTree(DEFAULT_OPTIONS)
7272
tree.insert('index.vue')
7373
tree.insert('b/index.vue')
7474
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
7575
})
7676

7777
it('handles non nested routes', () => {
78-
const tree = createPrefixTree(DEFAULT_OPTIONS)
78+
const tree = new PrefixTree(DEFAULT_OPTIONS)
7979
tree.insert('users.vue')
8080
tree.insert('users/index.vue')
8181
tree.insert('users/other.vue')
@@ -89,7 +89,7 @@ describe('generateRouteRecord', () => {
8989
})
9090

9191
it('removes trailing slashes', () => {
92-
const tree = createPrefixTree(DEFAULT_OPTIONS)
92+
const tree = new PrefixTree(DEFAULT_OPTIONS)
9393
tree.insert('users/index.vue')
9494
tree.insert('users/other.vue')
9595
tree.insert('nested.vue')
@@ -103,7 +103,7 @@ describe('generateRouteRecord', () => {
103103
...DEFAULT_OPTIONS,
104104
importMode: 'sync',
105105
} as const
106-
const tree = createPrefixTree(options)
106+
const tree = new PrefixTree(options)
107107
tree.insert('a.vue')
108108
tree.insert('b.vue')
109109
tree.insert('nested/file/c.vue')
@@ -120,7 +120,7 @@ describe('generateRouteRecord', () => {
120120
basename(filepath) === 'a.vue' ? 'sync' : 'async',
121121
}
122122

123-
const tree = createPrefixTree(options)
123+
const tree = new PrefixTree(options)
124124
tree.insert('a.vue')
125125
tree.insert('b.vue')
126126
tree.insert('nested/file/c.vue')
@@ -132,7 +132,7 @@ describe('generateRouteRecord', () => {
132132

133133
describe('names', () => {
134134
it('creates single word names', () => {
135-
const tree = createPrefixTree(DEFAULT_OPTIONS)
135+
const tree = new PrefixTree(DEFAULT_OPTIONS)
136136
tree.insert('index.vue')
137137
tree.insert('about.vue')
138138
tree.insert('users/index.vue')
@@ -144,7 +144,7 @@ describe('generateRouteRecord', () => {
144144
})
145145

146146
it('creates multi word names', () => {
147-
const tree = createPrefixTree(DEFAULT_OPTIONS)
147+
const tree = new PrefixTree(DEFAULT_OPTIONS)
148148
tree.insert('index.vue')
149149
tree.insert('my-users.vue')
150150
tree.insert('MyPascalCaseUsers.vue')
@@ -154,7 +154,7 @@ describe('generateRouteRecord', () => {
154154
})
155155

156156
it('works with nested views', () => {
157-
const tree = createPrefixTree(DEFAULT_OPTIONS)
157+
const tree = new PrefixTree(DEFAULT_OPTIONS)
158158
tree.insert('index.vue')
159159
tree.insert('users.vue')
160160
tree.insert('users/index.vue')
@@ -167,7 +167,7 @@ describe('generateRouteRecord', () => {
167167

168168
describe('route block', () => {
169169
it('adds meta data', async () => {
170-
const tree = createPrefixTree(DEFAULT_OPTIONS)
170+
const tree = new PrefixTree(DEFAULT_OPTIONS)
171171
const node = tree.insert('index.vue')
172172
node.setCustomRouteBlock('index.vue', {
173173
meta: {
@@ -180,7 +180,7 @@ describe('generateRouteRecord', () => {
180180
})
181181

182182
it('merges multiple meta properties', async () => {
183-
const tree = createPrefixTree(DEFAULT_OPTIONS)
183+
const tree = new PrefixTree(DEFAULT_OPTIONS)
184184
const node = tree.insert('index.vue')
185185
node.setCustomRouteBlock('index.vue', {
186186
path: '/custom',
@@ -199,7 +199,7 @@ describe('generateRouteRecord', () => {
199199
})
200200

201201
it('merges regardless of order', async () => {
202-
const tree = createPrefixTree(DEFAULT_OPTIONS)
202+
const tree = new PrefixTree(DEFAULT_OPTIONS)
203203
const node = tree.insert('index.vue')
204204
node.setCustomRouteBlock('index.vue', {
205205
name: 'a',
@@ -223,7 +223,7 @@ describe('generateRouteRecord', () => {
223223
})
224224

225225
it('handles named views with empty route blocks', () => {
226-
const tree = createPrefixTree(DEFAULT_OPTIONS)
226+
const tree = new PrefixTree(DEFAULT_OPTIONS)
227227
const node = tree.insert('index.vue')
228228
const n2 = tree.insert('[email protected]')
229229
expect(node).toBe(n2)
@@ -242,7 +242,7 @@ describe('generateRouteRecord', () => {
242242

243243
// FIXME: allow aliases
244244
it.todo('merges alias properties', async () => {
245-
const tree = createPrefixTree(DEFAULT_OPTIONS)
245+
const tree = new PrefixTree(DEFAULT_OPTIONS)
246246
const node = tree.insert('index.vue')
247247
node.setCustomRouteBlock('index.vue', {
248248
alias: '/one',
@@ -265,7 +265,7 @@ describe('generateRouteRecord', () => {
265265
})
266266

267267
it('merges deep meta properties', async () => {
268-
const tree = createPrefixTree(DEFAULT_OPTIONS)
268+
const tree = new PrefixTree(DEFAULT_OPTIONS)
269269
const node = tree.insert('index.vue')
270270
node.setCustomRouteBlock('index.vue', {
271271
meta: {
@@ -286,31 +286,31 @@ describe('generateRouteRecord', () => {
286286

287287
describe('raw paths insertions', () => {
288288
it('works with raw paths', () => {
289-
const tree = createPrefixTree(DEFAULT_OPTIONS)
289+
const tree = new PrefixTree(DEFAULT_OPTIONS)
290290
tree.insertParsedPath('a', 'a.vue')
291291
tree.insertParsedPath('b', 'b.vue')
292292
tree.insertParsedPath('c', 'c.vue')
293293
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
294294
})
295295

296296
it('works with mixed nodes', () => {
297-
const tree = createPrefixTree(DEFAULT_OPTIONS)
297+
const tree = new PrefixTree(DEFAULT_OPTIONS)
298298
tree.insertParsedPath('a', 'a.vue')
299299
tree.insert('b.vue')
300300
tree.insertParsedPath('c', 'c.vue')
301301
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
302302
})
303303

304304
it('works with nested nodes', () => {
305-
const tree = createPrefixTree(DEFAULT_OPTIONS)
305+
const tree = new PrefixTree(DEFAULT_OPTIONS)
306306
tree.insertParsedPath('a/b/c', 'a.vue')
307307
tree.insertParsedPath('a/b/d', 'a.vue')
308308
tree.insertParsedPath('a/d/c', 'a.vue')
309309
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
310310
})
311311

312312
it('do not nest raw segments with file based', () => {
313-
const tree = createPrefixTree(DEFAULT_OPTIONS)
313+
const tree = new PrefixTree(DEFAULT_OPTIONS)
314314
tree.insert('a/b.vue')
315315
// should be separated
316316
tree.insertParsedPath('a/b/c', 'a.vue')

src/core/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ResolvedOptions } from '../options'
2-
import { createPrefixTree, TreeNode } from './tree'
2+
import { TreeNode, PrefixTree } from './tree'
33
import { promises as fs } from 'fs'
44
import {
55
appendExtensionListToPattern,
@@ -35,7 +35,7 @@ export function createRoutesContext(options: ResolvedOptions) {
3535
? resolve(root, 'typed-router.d.ts')
3636
: resolve(root, preferDTS)
3737

38-
const routeTree = createPrefixTree(options)
38+
const routeTree = new PrefixTree(options)
3939
const editableRoutes = new EditableTreeNode(routeTree)
4040

4141
function log(...args: any[]) {

0 commit comments

Comments
 (0)