Open History to see the last commit
1import { analyzeModule, parseExpression } from '@tanstack/router-utils'
2import { is } from 'yuku-ast'
3import { describe, expect, it } from 'vitest'
4import {
5 getObjectPropertyKeyName,
6 getUniqueProgramIdentifier,
7 normalizePath,
8 routeFactoryCallCodeFilter,
9} from '../src/core/utils'
10β
11function matchesRouteFactoryCallCodeFilter(code: string) {
12 return routeFactoryCallCodeFilter.some((pattern) => {
13 const result = pattern.test(code)
14 pattern.lastIndex = 0
15 return result
16 })
17}
18β
19function getProgramPath(code: string) {
20 return analyzeModule({ code }).ast
21}
22β
23describe('normalizePath', () => {
24 it('should convert Windows backslashes to forward slashes', () => {
25 expect(normalizePath('C:\\Users\\project\\src\\routes\\index.tsx')).toBe(
26 'C:/Users/project/src/routes/index.tsx',
27 )
28 })
29β
30 it('should handle mixed slashes', () => {
31 expect(normalizePath('C:/Users\\project/src\\routes/index.tsx')).toBe(
32 'C:/Users/project/src/routes/index.tsx',
33 )
34 })
35β
36 it('should leave forward slashes unchanged', () => {
37 expect(normalizePath('/home/user/project/src/routes/index.tsx')).toBe(
38 '/home/user/project/src/routes/index.tsx',
39 )
40 })
41β
42 it('should handle relative paths with backslashes', () => {
43 expect(normalizePath('src\\routes\\index.tsx')).toBe('src/routes/index.tsx')
44 })
45β
46 it('should handle empty string', () => {
47 expect(normalizePath('')).toBe('')
48 })
49β
50 it('should handle path with query string', () => {
51 expect(normalizePath('C:\\project\\file.tsx?tsr-split=component')).toBe(
52 'C:/project/file.tsx?tsr-split=component',
53 )
54 })
55})
56β
57describe('getUniqueProgramIdentifier', () => {
58 it('returns the base name when unused', () => {
59 const programPath = getProgramPath('const existing = 1')
60β
61 expect(getUniqueProgramIdentifier(programPath, 'TSRComponent').name).toBe(
62 'TSRComponent',
63 )
64 })
65β
66 it('appends numeric suffixes for existing bindings', () => {
67 const programPath = getProgramPath(
68 'const TSRComponent = 1\nconst TSRComponent2 = 2',
69 )
70β
71 expect(getUniqueProgramIdentifier(programPath, 'TSRComponent').name).toBe(
72 'TSRComponent3',
73 )
74 })
75β
76 it('avoids globals too', () => {
77 const programPath = getProgramPath('const existing = window')
78β
79 expect(getUniqueProgramIdentifier(programPath, 'window').name).toBe(
80 'window2',
81 )
82 })
83β
84 it('avoids collisions across consecutive calls with the same base name', () => {
85 const programPath = getProgramPath('const existing = 1')
86β
87 const first = getUniqueProgramIdentifier(programPath, 'TSRComponent')
88 const second = getUniqueProgramIdentifier(programPath, 'TSRComponent')
89β
90 expect(first.name).toBe('TSRComponent')
91 expect(second.name).toBe('TSRComponent2')
92 expect(first.name).not.toBe(second.name)
93 })
94})
95β
96describe('routeFactoryCallCodeFilter', () => {
97 it('matches route factory calls with TypeScript type arguments', () => {
98 expect(
99 matchesRouteFactoryCallCodeFilter(
100 'createRootRouteWithContext<MyContext>()({})',
101 ),
102 ).toBe(true)
103 expect(
104 matchesRouteFactoryCallCodeFilter(
105 "createFileRoute<'/posts'>('/posts')({})",
106 ),
107 ).toBe(true)
108 expect(matchesRouteFactoryCallCodeFilter('createRootRoute<Ctx>({})')).toBe(
109 true,
110 )
111 })
112β
113 it('matches a route file that renames a route factory, so it is reported', () => {
114 expect(
115 matchesRouteFactoryCallCodeFilter(
116 "import { createFileRoute as cfr } from '@tanstack/react-router'\nexport const Route = cfr('/posts')({})",
117 ),
118 ).toBe(true)
119 })
120β
121 it('matches route factory calls without TypeScript type arguments', () => {
122 expect(
123 matchesRouteFactoryCallCodeFilter('createRootRouteWithContext()({})'),
124 ).toBe(true)
125 expect(matchesRouteFactoryCallCodeFilter('createRootRoute({})')).toBe(true)
126 expect(
127 matchesRouteFactoryCallCodeFilter("createFileRoute('/posts')({})"),
128 ).toBe(true)
129 })
130β
131 it('matches route factory calls with whitespace before invocation', () => {
132 expect(
133 matchesRouteFactoryCallCodeFilter(
134 'createRootRouteWithContext <MyContext>()({})',
135 ),
136 ).toBe(true)
137 expect(matchesRouteFactoryCallCodeFilter('createRootRoute ({})')).toBe(true)
138 expect(
139 matchesRouteFactoryCallCodeFilter("createFileRoute ('/posts')({})"),
140 ).toBe(true)
141 })
142})
143β
144describe('getObjectPropertyKeyName', () => {
145 it.each([
146 ['component: x', 'component'],
147 ['"errorComponent": x', 'errorComponent'],
148 ['[component]: x', undefined],
149 ['[foo.bar]: x', undefined],
150 ])('reads static keys in %s', (source, expected) => {
151 const expression = parseExpression(`{${source}}`)
152 if (
153 !is.ObjectExpression(expression) ||
154 !is.Property(expression.properties[0])
155 ) {
156 throw new Error('Expected an object property')
157 }
158 expect(getObjectPropertyKeyName(expression.properties[0])).toBe(expected)
159 })
160})