refactor(frontend): satisfy noUncheckedIndexedAccess (S3b)
Enable noUncheckedIndexedAccess and fix all 98 call sites it surfaced across 18 files. Every fix is behaviour-preserving — the flag flagged reads TypeScript couldn't prove in-bounds, all of which were already guarded by a length/index check, a findIndex result, or a non-empty invariant: - read-once so a ternary's narrowing sticks (Feed overrides, linkify mention); - non-null assertion AFTER an existing guard (PlayerModal/VideoEditor queue+cut-list indexing, modules step, useUndoable stacks) with a comment stating the invariant; - nullish fallback where undefined is a real possibility (GlassTuner slider ?? def, ConfigPanel active group ?? [], descriptionLinks id ?? null); - optional chaining where the entry genuinely can be absent (PlexBrowse IO entry); - a non-empty tuple type for the RELATIVE_UNITS constant (encodes "always has [0]"). The single highest-leverage fix: PlayerModal's `active` became `queue?.[index] ?? video` (always Video), clearing 34 of the 45 errors in that file at once. Verified: tsc/eslint/prettier clean, 56 vitest + 34 pytest + 17/17 e2e green (the e2e suite exercises the feed/player/channel components touched here).
This commit is contained in:
@@ -14,7 +14,8 @@ const INLINE_RE =
|
||||
|
||||
function tsToSeconds(ts: string): number {
|
||||
const parts = ts.split(":").map((n) => parseInt(n, 10));
|
||||
return parts.length === 3 ? parts[0] * 3600 + parts[1] * 60 + parts[2] : parts[0] * 60 + parts[1];
|
||||
const [a = 0, b = 0, c = 0] = parts; // hh:mm:ss or mm:ss — absent segments default to 0
|
||||
return parts.length === 3 ? a * 3600 + b * 60 + c : a * 60 + b;
|
||||
}
|
||||
|
||||
// Parse a YouTube `t`/`start` param: "90", "90s", or "1h2m3s".
|
||||
@@ -44,7 +45,7 @@ function parseYouTube(url: string): { id: string; start: number | null } | null
|
||||
if (u.pathname === "/watch") id = u.searchParams.get("v");
|
||||
else {
|
||||
const m = u.pathname.match(/^\/(?:shorts|embed|live)\/([^/?#]+)/);
|
||||
if (m) id = m[1];
|
||||
if (m) id = m[1] ?? null;
|
||||
}
|
||||
}
|
||||
if (!id) return null;
|
||||
@@ -128,7 +129,9 @@ export function renderDescription(
|
||||
</a>,
|
||||
);
|
||||
} else {
|
||||
const ts = m[3];
|
||||
// Reached only when neither m[1] (email) nor m[2] (hashtag) matched, so the third
|
||||
// alternative — the timestamp group — is the one that fired.
|
||||
const ts = m[3]!;
|
||||
out.push(
|
||||
<button
|
||||
key={key++}
|
||||
|
||||
@@ -33,7 +33,12 @@ export function formatSpeed(bps: number | null | undefined): string {
|
||||
* MUST stay sorted ascending, with the first row doubling as the "just now" threshold: sorting it
|
||||
* largest-first — the natural instinct — would make every timestamp under a year read "just now".
|
||||
* Module-level because it is constant, and exported so the tests can hold that invariant. */
|
||||
export const RELATIVE_UNITS: readonly [number, Intl.RelativeTimeFormatUnit][] = [
|
||||
// A non-empty tuple type (first element + rest) so `RELATIVE_UNITS[0]` is known-present under
|
||||
// noUncheckedIndexedAccess — the "sub-minute" branch relies on that first entry always existing.
|
||||
export const RELATIVE_UNITS: readonly [
|
||||
[number, Intl.RelativeTimeFormatUnit],
|
||||
...[number, Intl.RelativeTimeFormatUnit][],
|
||||
] = [
|
||||
[60, "minute"],
|
||||
[3600, "hour"],
|
||||
[86400, "day"],
|
||||
|
||||
@@ -62,10 +62,14 @@ export function linkify(text: string): ReactNode[] {
|
||||
const raw = m[1];
|
||||
out.push(link(/^https?:\/\//i.test(raw) ? raw : `https://${raw}`, raw));
|
||||
} else {
|
||||
// "<platform>: @handle" — keep the "<platform>: " prefix as text, link only the handle.
|
||||
const handle = m[3];
|
||||
// Reached only when the MENTION alternative matched, so its groups are present: m[2] is the
|
||||
// platform (always one of PLATFORMS' own keys — the pattern is built from them) and m[3] the
|
||||
// handle. "<platform>: @handle" — keep the "<platform>: " prefix as text, link only the handle.
|
||||
const platform = m[2]!;
|
||||
const handle = m[3]!;
|
||||
const base = PLATFORMS[platform.toLowerCase()]!;
|
||||
out.push(<Fragment key={key++}>{m[0].slice(0, m[0].length - handle.length - 1)}</Fragment>);
|
||||
out.push(link(PLATFORMS[m[2].toLowerCase()] + handle, "@" + handle));
|
||||
out.push(link(base + handle, "@" + handle));
|
||||
}
|
||||
last = m.index + m[0].length;
|
||||
}
|
||||
|
||||
@@ -128,6 +128,7 @@ export function stepModule(me: Me, current: Page, dir: 1 | -1): Page {
|
||||
const order = moduleOrder(me);
|
||||
if (order.length === 0) return current;
|
||||
const i = order.indexOf(current);
|
||||
if (i === -1) return dir === 1 ? order[0] : order[order.length - 1];
|
||||
return order[(i + dir + order.length) % order.length];
|
||||
// order is non-empty (guarded above) and every index below is in-bounds, so none are undefined.
|
||||
if (i === -1) return dir === 1 ? order[0]! : order[order.length - 1]!;
|
||||
return order[(i + dir + order.length) % order.length]!;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ export function useUndoable<T>(
|
||||
const undo = useCallback(() => {
|
||||
const s = ref.current;
|
||||
if (!s.past.length) return;
|
||||
const prev = s.past[s.past.length - 1];
|
||||
const prev = s.past[s.past.length - 1]!; // past is non-empty (guarded above)
|
||||
ref.current = {
|
||||
past: s.past.slice(0, -1),
|
||||
present: prev,
|
||||
@@ -67,7 +67,8 @@ export function useUndoable<T>(
|
||||
const redo = useCallback(() => {
|
||||
const s = ref.current;
|
||||
if (!s.future.length) return;
|
||||
const [next, ...rest] = s.future;
|
||||
const next = s.future[0]!; // future is non-empty (guarded above)
|
||||
const rest = s.future.slice(1);
|
||||
ref.current = { past: [...s.past, s.present], present: next, future: rest };
|
||||
force();
|
||||
onApplyRef.current?.(next);
|
||||
|
||||
Reference in New Issue
Block a user