Skip to content

Commit

Permalink
flip invariant assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance committed Sep 2, 2021
1 parent 5c6fc6b commit d67c77a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ function flattenRoutes(
let path: string;
if (route.path.startsWith("/")) {
invariant(
parentPath && !route.path.startsWith(parentPath),
!(parentPath && !route.path.startsWith(parentPath)),

This comment was marked as resolved.

Copy link
@mjackson

mjackson Sep 2, 2021

Member

Thanks for catching this. Boolean logic was never my forte 😅

FWIW I think the cleanest way to express this is probably:

invariant(
  !parentPath || route.path.startsWith(parentPath),
  "..."
);

It uses one less ! and reads more like you'd say it out loud: "make sure there is either no parent path or the route path starts with the parent path".

This comment was marked as resolved.

Copy link
@mjackson

mjackson Sep 2, 2021

Member

Actually, on second thought I think it can be even simpler:

invariant(
  route.path.startsWith(parentPath),
  "..."
);

Since parentPath defaults to "", any string will start with it.

`Absolute <Route path="${route.path}"> must begin ` +
`with its parent path "${parentPath}", otherwise it ` +
`will be unreachable.`
Expand Down

0 comments on commit d67c77a

Please sign in to comment.