From ae50eee13581c54acf076486f69ff120b7a26ae7 Mon Sep 17 00:00:00 2001 From: Kerry Date: Tue, 28 Mar 2023 10:07:49 +1300 Subject: [PATCH] Apply `strictNullChecks` to `src/utils/pillify.tsx` (#10456) * apply strictNullChecks to src/utils/pillify.tsx * include change to utility * apply strictNullChecks to src/utils/permalinks --- src/components/views/elements/Pill.tsx | 4 ++-- src/utils/permalinks/Permalinks.ts | 6 +++--- src/utils/pillify.tsx | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/components/views/elements/Pill.tsx b/src/components/views/elements/Pill.tsx index 67d0f6d6d6..965205e71f 100644 --- a/src/components/views/elements/Pill.tsx +++ b/src/components/views/elements/Pill.tsx @@ -37,8 +37,8 @@ export enum PillType { EventInOtherRoom = "TYPE_EVENT_IN_OTHER_ROOM", } -export const pillRoomNotifPos = (text: string): number => { - return text.indexOf("@room"); +export const pillRoomNotifPos = (text: string | null): number => { + return text?.indexOf("@room") ?? -1; }; export const pillRoomNotifLen = (): number => { diff --git a/src/utils/permalinks/Permalinks.ts b/src/utils/permalinks/Permalinks.ts index d2981905e3..caae2eb6c3 100644 --- a/src/utils/permalinks/Permalinks.ts +++ b/src/utils/permalinks/Permalinks.ts @@ -84,9 +84,9 @@ const ANY_REGEX = /.*/; export class RoomPermalinkCreator { private roomId: string; private highestPlUserId: string | null = null; - private populationMap: { [serverName: string]: number } | null = null; - private bannedHostsRegexps: RegExp[] | null = null; - private allowedHostsRegexps: RegExp[] | null = null; + private populationMap: { [serverName: string]: number } = {}; + private bannedHostsRegexps: RegExp[] = []; + private allowedHostsRegexps: RegExp[] = []; private _serverCandidates?: string[]; private started = false; diff --git a/src/utils/pillify.tsx b/src/utils/pillify.tsx index e1f001eb91..5d52ea53b9 100644 --- a/src/utils/pillify.tsx +++ b/src/utils/pillify.tsx @@ -82,7 +82,7 @@ export function pillifyLinks(nodes: ArrayLike, mxEvent: MatrixEvent, pi ); ReactDOM.render(pill, pillContainer); - node.parentNode.replaceChild(pillContainer, node); + node.parentNode?.replaceChild(pillContainer, node); pills.push(pillContainer); // Pills within pills aren't going to go well, so move on pillified = true; @@ -95,10 +95,10 @@ export function pillifyLinks(nodes: ArrayLike, mxEvent: MatrixEvent, pi // as applying pills happens outside of react, make sure we're not doubly // applying @room pills here, as a rerender with the same content won't touch the DOM // to clear the pills from the last run of pillifyLinks - !node.parentElement.classList.contains("mx_AtRoomPill") + !node.parentElement?.classList.contains("mx_AtRoomPill") ) { let currentTextNode = node as Node as Text | null; - const roomNotifTextNodes = []; + const roomNotifTextNodes: Text[] = []; // Take a textNode and break it up to make all the instances of @room their // own textNode, adding those nodes to roomNotifTextNodes @@ -109,7 +109,7 @@ export function pillifyLinks(nodes: ArrayLike, mxEvent: MatrixEvent, pi let roomTextNode = currentTextNode; if (roomNotifPos > 0) roomTextNode = roomTextNode.splitText(roomNotifPos); - if (roomTextNode.textContent.length > pillRoomNotifLen()) { + if (roomTextNode.textContent && roomTextNode.textContent.length > pillRoomNotifLen()) { nextTextNode = roomTextNode.splitText(pillRoomNotifLen()); } roomNotifTextNodes.push(roomTextNode); @@ -140,7 +140,7 @@ export function pillifyLinks(nodes: ArrayLike, mxEvent: MatrixEvent, pi ); ReactDOM.render(pill, pillContainer); - roomNotifTextNode.parentNode.replaceChild(pillContainer, roomNotifTextNode); + roomNotifTextNode.parentNode?.replaceChild(pillContainer, roomNotifTextNode); pills.push(pillContainer); } // Nothing else to do for a text node (and we don't need to advance