Open History to see the last commit
1import * as babel from '@babel/core'
2import * as t from '@babel/types'
3import { parseAst } from '@tanstack/router-utils'
4import { describe, expect, it } from 'vitest'
5import {
6 getObjectPropertyKeyName,
7 getUniqueProgramIdentifier,
8 normalizePath,
9 routeFactoryCallCodeFilter,
10} from '../src/core/utils'
11import type { NodePath } from '@babel/core'
12β
13function matchesRouteFactoryCallCodeFilter(code: string) {
14 return routeFactoryCallCodeFilter.some((pattern) => {
15 const result = pattern.test(code)
16 pattern.lastIndex = 0
17 return result
18 })
19}
20β
21function getProgramPath(code: string): NodePath<t.Program> {
22 const ast = parseAst({ code })
23 let programPath: NodePath<t.Program> | undefined
24β
25 babel.traverse(ast, {
26 Program(path: NodePath<t.Program>) {
27 programPath = path
28 path.stop()
29 },
30 })
31β
32 if (!programPath) {
33 throw new Error('Program path not found')
34 }
35β
36 return programPath
37}
38β
39describe('normalizePath', () => {
40 it('should convert Windows backslashes to forward slashes', () => {
41 expect(normalizePath('C:\\Users\\project\\src\\routes\\index.tsx')).toBe(
42 'C:/Users/project/src/routes/index.tsx',
43 )
44 })
45β
46 it('should handle mixed slashes', () => {
47 expect(normalizePath('C:/Users\\project/src\\routes/index.tsx')).toBe(
48 'C:/Users/project/src/routes/index.tsx',
49 )
50 })
51β
52 it('should leave forward slashes unchanged', () => {
53 expect(normalizePath('/home/user/project/src/routes/index.tsx')).toBe(
54 '/home/user/project/src/routes/index.tsx',
55 )
56 })
57β
58 it('should handle relative paths with backslashes', () => {
59 expect(normalizePath('src\\routes\\index.tsx')).toBe('src/routes/index.tsx')
60 })
61β
62 it('should handle empty string', () => {
63 expect(normalizePath('')).toBe('')
64 })
65β
66 it('should handle path with query string', () => {
67 expect(normalizePath('C:\\project\\file.tsx?tsr-split=component')).toBe(
68 'C:/project/file.tsx?tsr-split=component',
69 )
70 })
71})
72β
73describe('getUniqueProgramIdentifier', () => {
74 it('returns the base name when unused', () => {
75 const programPath = getProgramPath('const existing = 1')
76β
77 expect(getUniqueProgramIdentifier(programPath, 'TSRComponent').name).toBe(
78 'TSRComponent',
79 )
80 })
81β
82 it('appends numeric suffixes for existing bindings', () => {
83 const programPath = getProgramPath(
84 'const TSRComponent = 1\nconst TSRComponent2 = 2',
85 )
86β
87 expect(getUniqueProgramIdentifier(programPath, 'TSRComponent').name).toBe(
88 'TSRComponent3',
89 )
90 })
91β
92 it('avoids globals too', () => {
93 const programPath = getProgramPath('const existing = window')
94β
95 expect(getUniqueProgramIdentifier(programPath, 'window').name).toBe(
96 'window2',
97 )
98 })
99β
100 it('avoids collisions across consecutive calls with the same base name', () => {
101 const programPath = getProgramPath('const existing = 1')
102β
103 const first = getUniqueProgramIdentifier(programPath, 'TSRComponent')
104 const second = getUniqueProgramIdentifier(programPath, 'TSRComponent')
105β
106 expect(first.name).toBe('TSRComponent')
107 expect(second.name).toBe('TSRComponent2')
108 expect(first.name).not.toBe(second.name)
109 })
110})
111β
112describe('routeFactoryCallCodeFilter', () => {
113 it('matches route factory calls with TypeScript type arguments', () => {
114 expect(
115 matchesRouteFactoryCallCodeFilter(
116 'createRootRouteWithContext<MyContext>()({})',
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('returns identifier keys', () => {
146 const prop = t.objectProperty(t.identifier('component'), t.identifier('x'))
147β
148 expect(getObjectPropertyKeyName(prop)).toBe('component')
149 })
150β
151 it('returns string literal keys', () => {
152 const prop = t.objectProperty(
153 t.stringLiteral('errorComponent'),
154 t.identifier('x'),
155 )
156β
157 expect(getObjectPropertyKeyName(prop)).toBe('errorComponent')
158 })
159β
160 it('returns undefined for computed identifier keys', () => {
161 const prop = t.objectProperty(
162 t.identifier('component'),
163 t.identifier('x'),
164 true,
165 )
166β
167 expect(getObjectPropertyKeyName(prop)).toBeUndefined()
168 })
169β
170 it('returns undefined for computed member expression keys', () => {
171 const prop = t.objectProperty(
172 t.memberExpression(t.identifier('foo'), t.identifier('bar')),
173 t.identifier('x'),
174 true,
175 )
176β
177 expect(getObjectPropertyKeyName(prop)).toBeUndefined()
178 })
179})