ref: Refactor recreateRoute to accept matches or routes#118245
Open
ryan953 wants to merge 5 commits into
Open
Conversation
Contributor
📊 Type Coverage Diff
🔍 8 new type safety issues introduced
This is informational only and does not block the PR. |
…nsistent everywhere
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 6dd1513. Configure here.
When recreateRoute is given the matches option, it rebuilt routes internally
so the to route was never reference-equal to the local list, forcing a value
comparison. Two pathless layout routes collapse to the same {path: ''} shape,
so the first duplicate won and routeIndex could be too low, producing truncated
or wrong-ancestor URLs.
Memoize matchesToRoutes per matches array so derived routes are identity-stable,
and resolve to by reference (indexOf) first, falling back to value comparison so
a separately-constructed to still resolves instead of silently truncating.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The inline recreateRoute mock returned only recreateRoute, so RedirectToProjectModal rendering useRoutes hit an undefined matchesToRoutes. Spread the real module so the pure transform keeps working while recreateRoute stays stubbed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ryan953
commented
Jun 23, 2026
Comment on lines
+111
to
131
| it('resolves the correct ancestor when pathless layout routes share a shape', () => { | ||
| // Pathless layout routes (e.g. produced by translateSentryRoute) carry no | ||
| // `path`, so `matchesToRoutes` collapses each of them to the identical | ||
| // `{path: ''}` shape. Here indices 2 and 5 are duplicate-shaped pathless | ||
| // routes, with real path segments (`:orgId/`, `foo/`) in between them. | ||
| const m: UIMatch[] = [ | ||
| makeMatch('/', {}, {path: '/'}), | ||
| makeMatch('/settings/', {}, {path: 'settings/'}), | ||
| makeMatch('/settings/', {}, {}), | ||
| makeMatch('/settings/org-slug/', {orgId: 'org-slug'}, {path: ':orgId/'}), | ||
| makeMatch('/settings/org-slug/foo/', {orgId: 'org-slug'}, {path: 'foo/'}), | ||
| makeMatch('/settings/org-slug/foo/', {orgId: 'org-slug'}, {}), | ||
| ]; | ||
| const r = matchesToRoutes(m); | ||
|
|
||
| expect(recreateRoute(r[4]!, {routes: r, params})).toBe('/foo/bar/'); | ||
| // Targeting the *second* pathless route (index 5) must rebuild the full | ||
| // path up to it. The duplicate-shaped pathless route at index 2 must not | ||
| // shadow it, which would otherwise drop the `:orgId/` and `foo/` segments | ||
| // and produce a truncated ancestor URL. | ||
| expect(recreateRoute(r[5]!, {matches: m, params})).toBe('/settings/org-slug/foo/'); | ||
| }); |
Member
Author
There was a problem hiding this comment.
new test because the original findRouteIndex was broken in this case.
ryan953
commented
Jun 23, 2026
Comment on lines
+7
to
+39
| type Options = | ||
| | { | ||
| // parameters to replace any route string parameters (e.g. if route is `:orgId`, | ||
| // params should have `{orgId: slug}` | ||
| params: Record<string, string | undefined>; | ||
| routes: PlainRoute[]; | ||
| location?: Location; | ||
|
|
||
| routes: PlainRoute[]; | ||
| matches?: never; | ||
| /** | ||
| * The number of routes to pop off of `routes | ||
| * Must be < 0 | ||
| * | ||
| * There's no ts type for negative numbers so we are arbitrarily specifying -1-9 | ||
| */ | ||
| stepBack?: -1 | -2 | -3 | -4 | -5 | -6 | -7 | -8 | -9; | ||
| } | ||
| | { | ||
| matches: UIMatch[]; | ||
| // parameters to replace any route string parameters (e.g. if route is `:orgId`, | ||
| // params should have `{orgId: slug}` | ||
| params: Record<string, string | undefined>; | ||
| location?: Location; | ||
|
|
||
| routes?: never; | ||
| /** | ||
| * The number of routes to pop off of `routes | ||
| * Must be < 0 | ||
| * | ||
| * There's no ts type for negative numbers so we are arbitrarily specifying -1-9 | ||
| */ | ||
| stepBack?: -1 | -2 | -3 | -4 | -5 | -6 | -7 | -8 | -9; | ||
| }; |
Member
Author
There was a problem hiding this comment.
You can pass either routes or matches but not both. The code is all passing routes now, and tests all pass matches. The conversion between the two is the same as before, extracted from useRoutes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

matchesis the future, but there are a bunch of callsites, so a quick PR to support both will help speed things along.