From f1be2652a0543cf3ebda60b653106de4e278675e Mon Sep 17 00:00:00 2001 From: SohamBhattacharjee2003 <125297948+SohamBhattacharjee2003@users.noreply.github.com> Date: Thu, 2 Apr 2026 13:31:48 +0530 Subject: [PATCH] fix(comment-composer): hoist RegExp out of loop to satisfy js-hoist-regexp rule --- .../comment-composer/comment-composer.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx b/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx index 3e9b4504f..bee3f2da6 100644 --- a/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx +++ b/surfsense_web/components/chat-comments/comment-composer/comment-composer.tsx @@ -15,13 +15,17 @@ function convertDisplayToData(displayContent: string, mentions: InsertedMention[ const sortedMentions = [...mentions].sort((a, b) => b.displayName.length - a.displayName.length); - for (const mention of sortedMentions) { - const displayPattern = new RegExp( + const mentionPatterns = sortedMentions.map((mention) => ({ + pattern: new RegExp( `@${escapeRegExp(mention.displayName)}(?=\\s|$|[.,!?;:])`, "g" - ); - const dataFormat = `@[${mention.id}]`; - result = result.replace(displayPattern, dataFormat); + ), + dataFormat: `@[${mention.id}]`, + })); + + for (const { pattern, dataFormat } of mentionPatterns) { + pattern.lastIndex = 0; // reset global regex state + result = result.replace(pattern, dataFormat); } return result;