Skip to content

Commit

Permalink
fix(route-cli): make quote style configurable (#894)
Browse files Browse the repository at this point in the history
* feat(router-cli): make quoteStyle configurable

* set `quoteStyle` to `double` in one example
  • Loading branch information
schiller-manuel authored Dec 29, 2023
1 parent c96a623 commit 3b6b8d0
Show file tree
Hide file tree
Showing 20 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/api/router-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The CLI can be configured via a `tsr.config.json` file in the project's root dir
- **`routeFileIgnorePrefix`**: (Optional, **Defaults to `-`**) Route files and directories that start with this string will be ignored. By default this is set to `-` to allow for the use of directories to house related files that do not contain any route files.
- **`routesDirectory`**: (Required) The directory containing the routes relative to the cwd.
- **`generatedRouteTree`**: (Required) The path to the file where the generated route tree will be saved, relative to the cwd.
- **`quoteStyle`**: (Optional, **Defaults to `single`**) whether to use `single` or `double` quotes when writing the route ids in `new FileRoute()`

## Example

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { FileRoute } from '@tanstack/react-router'

export const Route = new FileRoute('/_auth/profile').createRoute({
export const Route = new FileRoute("/_auth/profile").createRoute({
component: ProfileComponent,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { FileRoute, Link, redirect } from '@tanstack/react-router'
import { auth } from '../utils/auth'

export const Route = new FileRoute('/_auth').createRoute({
export const Route = new FileRoute("/_auth").createRoute({
// Before loading, authenticate the user via our auth context
// This will also happen during prefetching (e.g. hovering over links, etc)
beforeLoad: ({ context, location }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { FileRoute } from '@tanstack/react-router'

export const Route = new FileRoute('/_layout/layout-a').createRoute({
export const Route = new FileRoute("/_layout/layout-a").createRoute({
component: LayoutAComponent,
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { FileRoute } from '@tanstack/react-router'

export const Route = new FileRoute('/_layout/layout-b').createRoute({
export const Route = new FileRoute("/_layout/layout-b").createRoute({
component: LayoutBComponent,
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { Link, Outlet, FileRoute } from '@tanstack/react-router'

export const Route = new FileRoute('/_layout').createRoute({
export const Route = new FileRoute("/_layout").createRoute({
component: LayoutComponent,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FileRoute } from '@tanstack/react-router'
import { invoicesQueryOptions } from '../utils/queryOptions'
import { useSuspenseQuery } from '@tanstack/react-query'

export const Route = new FileRoute('/dashboard/').createRoute({
export const Route = new FileRoute("/dashboard/").createRoute({
loader: (opts) =>
opts.context.queryClient.ensureQueryData(invoicesQueryOptions()),
component: DashboardIndexComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../utils/queryOptions'
import { useSuspenseQuery } from '@tanstack/react-query'

export const Route = new FileRoute('/dashboard/invoices/$invoiceId').createRoute({
export const Route = new FileRoute("/dashboard/invoices/$invoiceId").createRoute({
parseParams: (params) => ({
invoiceId: z.number().int().parse(Number(params.invoiceId)),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Spinner } from '../components/Spinner'
import { useCreateInvoiceMutation } from '../utils/queryOptions'

// @ts-ignore
export const Route = new FileRoute('/dashboard/invoices/').createRoute({
export const Route = new FileRoute("/dashboard/invoices/").createRoute({
component: InvoicesIndexComponent,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Spinner } from '../components/Spinner'
import { invoicesQueryOptions } from '../utils/queryOptions'
import { useSuspenseQuery } from '@tanstack/react-query'

export const Route = new FileRoute('/dashboard/invoices').createRoute({
export const Route = new FileRoute("/dashboard/invoices").createRoute({
loader: (opts) =>
opts.context.queryClient.ensureQueryData(invoicesQueryOptions()),
component: InvoicesComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { FileRoute, Link, Outlet } from '@tanstack/react-router'

export const Route = new FileRoute('/dashboard').createRoute({
export const Route = new FileRoute("/dashboard").createRoute({
component: DashboardComponent,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { FileRoute } from '@tanstack/react-router'

// @ts-ignore
export const Route = new FileRoute('/dashboard/users/').createRoute({
export const Route = new FileRoute("/dashboard/users/").createRoute({
component: UsersIndexComponent,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { usersQueryOptions } from '../utils/queryOptions'

type UsersViewSortBy = 'name' | 'id' | 'email'

export const Route = new FileRoute('/dashboard/users').createRoute({
export const Route = new FileRoute("/dashboard/users").createRoute({
validateSearch: z.object({
usersView: z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from 'zod'
import { userQueryOptions } from '../utils/queryOptions'
import { useSuspenseQuery } from '@tanstack/react-query'

export const Route = new FileRoute('/dashboard/users/user').createRoute({
export const Route = new FileRoute("/dashboard/users/user").createRoute({
validateSearch: z.object({
userId: z.number(),
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FileRoute, lazyRouteComponent } from '@tanstack/react-router'

export const Route = new FileRoute('/expensive/').createRoute({
export const Route = new FileRoute("/expensive/").createRoute({
component: lazyRouteComponent(() => import('./-components/Expensive')),
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { FileRoute, Link } from '@tanstack/react-router'

export const Route = new FileRoute('/').createRoute({
export const Route = new FileRoute("/").createRoute({
component: IndexComponent,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { FileRoute, useRouter } from '@tanstack/react-router'
import { z } from 'zod'

export const Route = new FileRoute('/login')
export const Route = new FileRoute("/login")
.createRoute({
validateSearch: z.object({
redirect: z.string().optional(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rootDirectory": ".",
"routesDirectory": "./src/routes",
"generatedRouteTree": "./src/routeTree.gen.ts"
"generatedRouteTree": "./src/routeTree.gen.ts",
"quoteStyle": "double"
}
1 change: 1 addition & 0 deletions packages/router-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const configSchema = z.object({
routeFileIgnorePrefix: z.string().optional(),
routesDirectory: z.string(),
generatedRouteTree: z.string(),
quoteStyle: z.enum(['single', 'double']).optional().default('single'),
})

export type Config = z.infer<typeof configSchema>
Expand Down
3 changes: 2 additions & 1 deletion packages/router-cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ export async function generator(config: Config) {
// we have to double escape it into $$$$. For more information, see
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement
const escapedRoutePath = node.routePath?.replaceAll('$', '$$$$') ?? ''
const quote = config.quoteStyle === 'single' ? `'` : `"`
const replaced = routeCode.replace(
fileRouteRegex,
`new FileRoute('${escapedRoutePath}')`,
`new FileRoute(${quote}${escapedRoutePath}${quote})`,
)

if (replaced !== routeCode) {
Expand Down

0 comments on commit 3b6b8d0

Please sign in to comment.