Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router-generator): reset route-group Regex when calling handleNode #3289

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/router-generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
removeTrailingSlash,
removeUnderscores,
replaceBackslash,
resetRegex,
routePathToVariable,
trimPathLeft,
writeIfDifferent,
Expand Down Expand Up @@ -158,6 +159,11 @@ export async function generator(config: Config, root: string) {
await handleRootNode(rootRouteNode)

const handleNode = async (node: RouteNode) => {
// Do not remove this as we need to set the lastIndex to 0 as it
// is necessary to reset the regex's index when using the global flag
// otherwise it might not match the next time it's used
resetRegex(routeGroupPatternRegex)

let parentRoute = hasParentRoute(routeNodes, node, node.routePath)

// if the parent route is a virtual parent route, we need to find the real parent route
Expand Down
13 changes: 13 additions & 0 deletions packages/router-generator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,16 @@ export async function writeIfDifferent(

return false
}

/**
* This function resets the regex index to 0 so that it can be reused
* without having to create a new regex object or worry about the last
* state when using the global flag.
*
* @param regex The regex object to reset
* @returns
*/
export function resetRegex(regex: RegExp) {
regex.lastIndex = 0
return
}
Loading