Skip to content

ref: Refactor recreateRoute to accept matches or routes#118245

Open
ryan953 wants to merge 5 commits into
masterfrom
recreateRoute-accepts-matches
Open

ref: Refactor recreateRoute to accept matches or routes#118245
ryan953 wants to merge 5 commits into
masterfrom
recreateRoute-accepts-matches

Conversation

@ryan953

@ryan953 ryan953 commented Jun 23, 2026

Copy link
Copy Markdown
Member

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

@ryan953 ryan953 requested a review from a team as a code owner June 23, 2026 16:15
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Jun 23, 2026
@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

📊 Type Coverage Diff

Metric Before After Delta
Coverage 93.79% 93.79% ±0%
Typed 132,941 132,948 🟢 +7
Untyped 8,796 8,802 🔴 +6
🔍 8 new type safety issues introduced

any-typed symbols (8 new)

File Line Detail
static/app/utils/recreateRoute.tsx 46 matchesToRoutesCache (var)
static/app/utils/recreateRoute.tsx 51 cached (var)
static/app/utils/recreateRoute.tsx 56 routes (var)
static/app/utils/recreateRoute.tsx 59 extra (var)
static/app/utils/recreateRoute.tsx 91 route (param)
static/app/utils/recreateRoute.tsx 97 to as any (as-any)
static/app/utils/recreateRoute.tsx 97 route as any (as-any)
static/app/utils/recreateRoute.tsx 109 routes (var)

This is informational only and does not block the PR.

Comment thread static/app/utils/recreateRoute.tsx Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread static/app/utils/recreateRoute.tsx
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>
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/');
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new test because the original findRouteIndex was broken in this case.

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;
};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant