Commit f9bb654a authored by Robert Knight's avatar Robert Knight

Remove an incorrect `RouteMap|RouteMetadata => RouteMap` cast

When traversing the RouteMap there are three possibilities:

 - There is no route that matches the path segment

 - There is a child `RouteMap` that matches the path segment

 - There is a child `RouteMetadata` that matches the path segment, in
   which case we should only return it if we reached the end of the path

Rewrite `findRouteMetadata` to handle these possibilities more
explicitly. We already had tests that covered the expected behavior,
this rewrite just avoid a cast which is technically incorrect.
parent 5cfa841c
...@@ -72,7 +72,7 @@ function stripInternalProperties(obj) { ...@@ -72,7 +72,7 @@ function stripInternalProperties(obj) {
* @return {link is RouteMetadata} * @return {link is RouteMetadata}
*/ */
function isRouteMetadata(link) { function isRouteMetadata(link) {
return link ? 'url' in link : false; return 'url' in link;
} }
/** /**
...@@ -82,15 +82,23 @@ function isRouteMetadata(link) { ...@@ -82,15 +82,23 @@ function isRouteMetadata(link) {
* @param {string} route - Dot-separated path of route in `routeMap` * @param {string} route - Dot-separated path of route in `routeMap`
*/ */
function findRouteMetadata(routeMap, route) { function findRouteMetadata(routeMap, route) {
/** @type {RouteMap|RouteMetadata} */ /** @type {RouteMap} */
let cursor = routeMap; let cursor = routeMap;
for (let segment of route.split('.')) { const pathSegments = route.split('.');
cursor = /** @type {RouteMap} */ (cursor)[segment]; for (let [index, segment] of pathSegments.entries()) {
if (!cursor) { const nextCursor = cursor[segment];
if (!nextCursor || isRouteMetadata(nextCursor)) {
if (nextCursor && index === pathSegments.length - 1) {
// Found the RouteMetadata at the end of the path.
return nextCursor;
}
// Didn't find the route, or found a RouteMetadata before we reached the
// end of the path.
break; break;
} }
cursor = nextCursor;
} }
return isRouteMetadata(cursor) ? cursor : null; return null;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment