Skip to content

Commit

Permalink
_setDisplayName: Replace optional chaining with proper error handing
Browse files Browse the repository at this point in the history
With the current code, it is not possible for resources[i-1] to be
undefined. Make this clear with type narrowing.
  • Loading branch information
victorlin committed Nov 14, 2024
1 parent 8afb18d commit 9ab5067
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion static-site/src/components/ListResources/ResourceGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ function _setDisplayName(resources: Resource[]): DisplayNamedResource[] {
if (i===0) {
name = r.nameParts.join(sep);
} else {
let matchIdx = r.nameParts.map((word, j) => word === resources[i-1]?.nameParts[j]).findIndex((v) => !v);
const previousResource = resources[i-1];
if (previousResource === undefined) {
throw new InternalError("Previous resource is undefined. Check that this is not run on i===0.");
}
let matchIdx = r.nameParts.map((word, j) => word === previousResource.nameParts[j]).findIndex((v) => !v);
if (matchIdx===-1) { // -1 means every word is in the preceding name, but we should display the last word anyway
matchIdx = r.nameParts.length-2;
}
Expand Down

0 comments on commit 9ab5067

Please sign in to comment.