fix: correctly replace bracked escaped part#4968
Conversation
WalkthroughA module-scoped regex constant was added and used in determineInitialRoutePath to replace bracketed content in route parts, changing from single-character to multi-character bracket content support. Local regex declaration was removed. No public APIs or error handling logic were altered. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
View your CI Pipeline Execution ↗ for commit 61cec50
☁️ Nx Cloud last updated this comment at |
More templates
@tanstack/arktype-adapter
@tanstack/directive-functions-plugin
@tanstack/eslint-plugin-router
@tanstack/history
@tanstack/react-router
@tanstack/react-router-devtools
@tanstack/react-router-ssr-query
@tanstack/react-start
@tanstack/react-start-client
@tanstack/react-start-plugin
@tanstack/react-start-server
@tanstack/router-cli
@tanstack/router-core
@tanstack/router-devtools
@tanstack/router-devtools-core
@tanstack/router-generator
@tanstack/router-plugin
@tanstack/router-ssr-query-core
@tanstack/router-utils
@tanstack/router-vite-plugin
@tanstack/server-functions-plugin
@tanstack/solid-router
@tanstack/solid-router-devtools
@tanstack/solid-start
@tanstack/solid-start-client
@tanstack/solid-start-plugin
@tanstack/solid-start-server
@tanstack/start-client-core
@tanstack/start-plugin-core
@tanstack/start-server-core
@tanstack/start-server-functions-client
@tanstack/start-server-functions-fetcher
@tanstack/start-server-functions-server
@tanstack/start-storage-context
@tanstack/valibot-adapter
@tanstack/virtual-file-routes
@tanstack/zod-adapter
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
packages/router-generator/src/utils.ts (1)
75-96: Fix stateful /g regex reuse (lastIndex) and validate multi-char bracket content correctly.Because BRACKET_CONTENT_RE is a global regex and used with exec, lastIndex must be reset per string; otherwise matches can be skipped across parts. Also, after switching to multi-char capture, the disallowed-char check should scan each character, not the entire captured string.
Apply this diff to address both issues:
// Check if any disallowed characters are used in brackets - let match - while ((match = BRACKET_CONTENT_RE.exec(part)) !== null) { - const character = match[1] - if (character === undefined) continue - if (DISALLOWED_ESCAPE_CHARS.has(character)) { + let match + // Reset global regex state before scanning a new string + resetRegex(BRACKET_CONTENT_RE) + while ((match = BRACKET_CONTENT_RE.exec(part)) !== null) { + const content = match[1] ?? '' + const invalidChars = [...new Set([...content].filter((ch) => DISALLOWED_ESCAPE_CHARS.has(ch)))] + if (invalidChars.length) { console.error( - `Error: Disallowed character "${character}" found in square brackets in route path "${routePath}".\nYou cannot use any of the following characters in square brackets: ${Array.from( + `Error: Disallowed character(s) "${invalidChars.join(', ')}" found in square brackets in route path "${routePath}".\nYou cannot use any of the following characters in square brackets: ${Array.from( DISALLOWED_ESCAPE_CHARS, ).join(', ')}\nPlease remove and/or replace them.`, ) process.exit(1) } } // Since this split segment is safe at this point, we can // remove the brackets and replace them with the content inside return part.replace(BRACKET_CONTENT_RE, '$1')Notes:
- resetRegex is already defined in this file and safely resets lastIndex.
- String.prototype.replace with a global regex resets lastIndex internally, so no extra reset is required before the replace.
🧹 Nitpick comments (1)
packages/router-generator/src/utils.ts (1)
55-56: Good extraction to a module-scoped regex; consider disallowing empty brackets.Defining BRACKET_CONTENT_RE once at module scope is cleaner and avoids per-call allocations. Minor: if empty brackets "[]" are not a valid case, prefer (.+?) over (.*?) to enforce at least one character inside the brackets.
Apply this minimal tweak if empties should be invalid:
-const BRACKET_CONTENT_RE = /\[(.*?)\]/g +const BRACKET_CONTENT_RE = /\[(.+?)\]/g
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/router-generator/src/utils.ts(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Routing now correctly handles bracketed dynamic segments with multiple characters in paths. Contents like [userId] are preserved in full (no longer truncated to a single character), ensuring accurate initial route generation and consistent navigation behavior for dynamic routes. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Routing now correctly handles bracketed dynamic segments with multiple characters in paths. Contents like [userId] are preserved in full (no longer truncated to a single character), ensuring accurate initial route generation and consistent navigation behavior for dynamic routes. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit