From 54f90e41c07645e34d49967cdb2e0c2089390b9f Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 11 May 2023 12:49:10 -0700 Subject: [PATCH] eslint: Fix unicorn/prefer-string-replace-all. Signed-off-by: Anders Kaseorg --- tools/check-openapi | 2 +- web/shared/src/fenced_code.ts | 2 +- web/shared/src/internal_url.js | 8 +++++--- web/shared/src/typeahead.ts | 8 ++++---- web/src/alert_words.js | 2 +- web/src/compose_actions.js | 4 +++- web/src/copy_and_paste.js | 6 +++--- web/src/emoji_picker.js | 2 +- web/src/filter.js | 14 +++++++------- web/src/markdown.js | 8 ++++---- web/src/notifications.js | 3 ++- web/src/people.js | 4 ++-- web/src/pill_typeahead.js | 2 +- web/src/settings_emoji.js | 4 ++-- web/src/settings_org.js | 8 ++++---- web/src/typeahead_helper.js | 2 +- 16 files changed, 42 insertions(+), 37 deletions(-) diff --git a/tools/check-openapi b/tools/check-openapi index 761a8fb4e8..f138490a66 100755 --- a/tools/check-openapi +++ b/tools/check-openapi @@ -106,7 +106,7 @@ async function checkFile(file) { diff += prefix; diff += part.value .replace(/\n$/, "") - .replace(/\n/g, "\n" + prefix); + .replaceAll("\n", "\n" + prefix); diff += "\n"; } diff += "\u001B[0m"; diff --git a/web/shared/src/fenced_code.ts b/web/shared/src/fenced_code.ts index 5b30b2745a..c8bf8fd71c 100644 --- a/web/shared/src/fenced_code.ts +++ b/web/shared/src/fenced_code.ts @@ -68,7 +68,7 @@ export function wrap_code(code: string, lang?: string): string { } // Trim trailing \n until there's just one left // This mirrors how pygments handles code input - return header + _.escape(code.replace(/^\n+|\n+$/g, "")) + "\n"; + return header + _.escape(code.replaceAll(/^\n+|\n+$/g, "")) + "\n"; } function wrap_quote(text: string): string { diff --git a/web/shared/src/internal_url.js b/web/shared/src/internal_url.js index 45c51485fb..756a6788bb 100644 --- a/web/shared/src/internal_url.js +++ b/web/shared/src/internal_url.js @@ -9,7 +9,9 @@ const hashReplacements = new Map([ // window.location.hash. So we hide our URI-encoding // by replacing % with . (like MediaWiki). export function encodeHashComponent(str) { - return encodeURIComponent(str).replace(/[%().]/g, (matched) => hashReplacements.get(matched)); + return encodeURIComponent(str).replaceAll(/[%().]/g, (matched) => + hashReplacements.get(matched), + ); } export function decodeHashComponent(str) { @@ -18,7 +20,7 @@ export function decodeHashComponent(str) { // of such characters when encoding. This can also, // fail independent of our fault. // Here we let the calling code handle the exception. - return decodeURIComponent(str.replace(/\./g, "%")); + return decodeURIComponent(str.replaceAll(".", "%")); } export function stream_id_to_slug(stream_id, maybe_get_stream_name) { @@ -29,7 +31,7 @@ export function stream_id_to_slug(stream_id, maybe_get_stream_name) { // TODO: Convert this to replaceAll once mobile no longer supports // browsers that don't have it. - name = name.replace(/ /g, "-"); + name = name.replaceAll(" ", "-"); return stream_id + "-" + name; } diff --git a/web/shared/src/typeahead.ts b/web/shared/src/typeahead.ts index d306fbc6a4..be21d3c97c 100644 --- a/web/shared/src/typeahead.ts +++ b/web/shared/src/typeahead.ts @@ -45,7 +45,7 @@ type UnicodeEmoji = { }; export function remove_diacritics(s: string): string { - return s.normalize("NFKD").replace(unicode_marks, ""); + return s.normalize("NFKD").replaceAll(unicode_marks, ""); } // This function attempts to match a query with a source text. @@ -79,7 +79,7 @@ function clean_query(query: string): string { // contenteditable widget such as the composebox PM section, the // space at the end was a `no break-space (U+00A0)` instead of // `space (U+0020)`, which lead to no matches in those cases. - query = query.replace(/\u00A0/g, " "); + query = query.replaceAll("\u00A0", " "); return query; } @@ -98,7 +98,7 @@ export const parse_unicode_emoji_code = (code: string): string => export function get_emoji_matcher(query: string): (emoji: Emoji) => boolean { // replace spaces with underscores for emoji matching - query = query.replace(/ /g, "_"); + query = query.replaceAll(" ", "_"); query = clean_query_lowercase(query); return function (emoji) { @@ -166,7 +166,7 @@ export function triage( export function sort_emojis(objs: T[], query: string): T[] { // replace spaces with underscores for emoji matching - query = query.replace(/ /g, "_"); + query = query.replaceAll(" ", "_"); query = query.toLowerCase(); function decent_match(name: string): boolean { diff --git a/web/src/alert_words.js b/web/src/alert_words.js index a48837bcee..81024c2b03 100644 --- a/web/src/alert_words.js +++ b/web/src/alert_words.js @@ -41,7 +41,7 @@ export function process_message(message) { } for (const word of my_alert_words) { - const clean = _.escapeRegExp(word).replace(/["&'<>]/g, (c) => + const clean = _.escapeRegExp(word).replaceAll(/["&'<>]/g, (c) => alert_regex_replacements.get(c), ); const before_punctuation = "\\s|^|>|[\\(\\\".,';\\[]"; diff --git a/web/src/compose_actions.js b/web/src/compose_actions.js index 4051aaa7d1..16fd68300e 100644 --- a/web/src/compose_actions.js +++ b/web/src/compose_actions.js @@ -224,7 +224,9 @@ export function start(msg_type, opts) { compose_state.topic(opts.topic); // Set the recipients with a space after each comma, so it looks nice. - compose_state.private_message_recipient(opts.private_message_recipient.replace(/,\s*/g, ", ")); + compose_state.private_message_recipient( + opts.private_message_recipient.replaceAll(/,\s*/g, ", "), + ); // If the user opens the compose box, types some text, and then clicks on a // different stream/topic, we want to keep the text in the compose box diff --git a/web/src/copy_and_paste.js b/web/src/copy_and_paste.js index e4c379a967..9ae03e8810 100644 --- a/web/src/copy_and_paste.js +++ b/web/src/copy_and_paste.js @@ -43,7 +43,7 @@ function construct_recipient_header($message_row) { const message_header_content = rows .get_message_recipient_header($message_row) .text() - .replace(/\s+/g, " ") + .replaceAll(/\s+/g, " ") .replace(/^\s/, "") .replace(/\s$/, ""); return $("

").append($("").text(message_header_content)); @@ -322,10 +322,10 @@ export function paste_handler_converter(paste_html) { let markdown_text = turndownService.turndown(paste_html); // Checks for escaped ordered list syntax. - markdown_text = markdown_text.replace(/^(\W* {0,3})(\d+)\\\. /gm, "$1$2. "); + markdown_text = markdown_text.replaceAll(/^(\W* {0,3})(\d+)\\\. /gm, "$1$2. "); // Removes newlines before the start of a list and between list elements. - markdown_text = markdown_text.replace(/\n+([*+-])/g, "\n$1"); + markdown_text = markdown_text.replaceAll(/\n+([*+-])/g, "\n$1"); return markdown_text; } diff --git a/web/src/emoji_picker.js b/web/src/emoji_picker.js index 366ae28334..f6b85c09ab 100644 --- a/web/src/emoji_picker.js +++ b/web/src/emoji_picker.js @@ -356,7 +356,7 @@ function update_emoji_showcase($focused_emoji) { const emoji_dict = { ...focused_emoji_dict, - name: focused_emoji_name.replace(/_/g, " "), + name: focused_emoji_name.replaceAll("_", " "), }; const rendered_showcase = render_emoji_showcase({ emoji_dict, diff --git a/web/src/filter.js b/web/src/filter.js index f266457180..fd86abc809 100644 --- a/web/src/filter.js +++ b/web/src/filter.js @@ -271,7 +271,7 @@ export class Filter { operand = operand .toString() .toLowerCase() - .replace(/[\u201C\u201D]/g, '"'); + .replaceAll(/[\u201C\u201D]/g, '"'); break; default: operand = operand.toString().toLowerCase(); @@ -294,20 +294,20 @@ export class Filter { URI encoding to avoid problematic characters. */ static encodeOperand(operand) { return operand - .replace(/%/g, "%25") - .replace(/\+/g, "%2B") - .replace(/ /g, "+") - .replace(/"/g, "%22"); + .replaceAll("%", "%25") + .replaceAll("+", "%2B") + .replaceAll(" ", "+") + .replaceAll('"', "%22"); } static decodeOperand(encoded, operator) { - encoded = encoded.replace(/"/g, ""); + encoded = encoded.replaceAll('"', ""); if ( ["dm-including", "dm", "sender", "from", "pm-with", "group-pm-with"].includes( operator, ) === false ) { - encoded = encoded.replace(/\+/g, " "); + encoded = encoded.replaceAll("+", " "); } return util.robust_url_decode(encoded).trim(); } diff --git a/web/src/markdown.js b/web/src/markdown.js index 57315062ec..a9148c23d3 100644 --- a/web/src/markdown.js +++ b/web/src/markdown.js @@ -232,13 +232,13 @@ function parse_with_options({raw_content, helper_config, options}) { }, silencedMentionHandler(quote) { // Silence quoted mentions. - quote = quote.replace( + quote = quote.replaceAll( /()@/g, "$1 silent$2", ); // Silence quoted user group mentions. - quote = quote.replace( + quote = quote.replaceAll( /()@/g, "$1 silent$2", ); @@ -375,7 +375,7 @@ function handleUnicodeEmoji({unicode_emoji, get_emoji_name}) { if (emoji_name) { const alt_text = ":" + emoji_name + ":"; - const title = emoji_name.replace(/_/g, " "); + const title = emoji_name.replaceAll("_", " "); return make_emoji_span(codepoint, title, alt_text); } @@ -384,7 +384,7 @@ function handleUnicodeEmoji({unicode_emoji, get_emoji_name}) { function handleEmoji({emoji_name, get_realm_emoji_url, get_emoji_codepoint}) { const alt_text = ":" + emoji_name + ":"; - const title = emoji_name.replace(/_/g, " "); + const title = emoji_name.replaceAll("_", " "); // Zulip supports both standard/Unicode emoji, served by a // spritesheet and custom realm-specific emoji (served by URL). diff --git a/web/src/notifications.js b/web/src/notifications.js index 130775b691..a14709fa9b 100644 --- a/web/src/notifications.js +++ b/web/src/notifications.js @@ -328,7 +328,8 @@ export function process_notification(notification) { if (content.length + title.length + other_recipients.length > 230) { // Then count how many people are in the conversation and summarize // by saying the conversation is with "you and [number] other people" - other_recipients = other_recipients.replace(/[^,]/g, "").length + " other people"; + other_recipients = + other_recipients.replaceAll(/[^,]/g, "").length + " other people"; } title += " (to you and " + other_recipients + ")"; diff --git a/web/src/people.js b/web/src/people.js index 26d30ad6c1..41e5ebb3a6 100644 --- a/web/src/people.js +++ b/web/src/people.js @@ -530,7 +530,7 @@ export function pm_with_url(message) { } else { const person = get_by_user_id(user_ids[0]); if (person && person.full_name) { - suffix = person.full_name.replace(/[ "%/<>`\p{C}]+/gu, "-"); + suffix = person.full_name.replaceAll(/[ "%/<>`\p{C}]+/gu, "-"); } else { blueslip.error("Unknown people in message"); suffix = "unk"; @@ -605,7 +605,7 @@ export function emails_to_slug(emails_string) { if (emails.length === 1) { const name = get_by_email(emails[0]).full_name; - slug += name.replace(/[ "%/<>`\p{C}]+/gu, "-"); + slug += name.replaceAll(/[ "%/<>`\p{C}]+/gu, "-"); } else { slug += "group"; } diff --git a/web/src/pill_typeahead.js b/web/src/pill_typeahead.js index 3322dce8dc..17dfb7b41d 100644 --- a/web/src/pill_typeahead.js +++ b/web/src/pill_typeahead.js @@ -76,7 +76,7 @@ export function set_up($input, pills, opts) { }, matcher(item) { let query = this.query.toLowerCase(); - query = query.replace(/\u00A0/g, " "); + query = query.replaceAll("\u00A0", " "); if (include_streams(query)) { query = query.trim().slice(1); diff --git a/web/src/settings_emoji.js b/web/src/settings_emoji.js index a5bd67e3b2..c94fbf5a0a 100644 --- a/web/src/settings_emoji.js +++ b/web/src/settings_emoji.js @@ -79,7 +79,7 @@ function sort_author_full_name(a, b) { function is_default_emoji(emoji_name) { // Spaces are replaced with `_` to match how the emoji name will // actually be stored in the backend. - return emoji_codes.names.includes(emoji_name.replace(/ /g, "_")); + return emoji_codes.names.includes(emoji_name.replaceAll(" ", "_")); } function is_custom_emoji(emoji_name) { @@ -116,7 +116,7 @@ export function populate_emoji() { return render_admin_emoji_list({ emoji: { name: item.name, - display_name: item.name.replace(/_/g, " "), + display_name: item.name.replaceAll("_", " "), source_url: item.source_url, author: item.author || "", can_delete_emoji: can_delete_emoji(item), diff --git a/web/src/settings_org.js b/web/src/settings_org.js index 33a4d7ebcc..4559ab09c5 100644 --- a/web/src/settings_org.js +++ b/web/src/settings_org.js @@ -190,7 +190,7 @@ export function extract_property_name($elem, for_realm_default_settings) { // "realm_{settings_name}}" because both user and realm default // settings use the same template and each element should have // unique id. - return /^realm_(.*)$/.exec($elem.attr("id").replace(/-/g, "_"))[1]; + return /^realm_(.*)$/.exec($elem.attr("id").replaceAll("-", "_"))[1]; } if ($elem.attr("id").startsWith("id_authmethod")) { @@ -203,7 +203,7 @@ export function extract_property_name($elem, for_realm_default_settings) { return /^id_authmethod[\da-z]+_(.*)$/.exec($elem.attr("id"))[1]; } - return /^id_(.*)$/.exec($elem.attr("id").replace(/-/g, "_"))[1]; + return /^id_(.*)$/.exec($elem.attr("id").replaceAll("-", "_"))[1]; } export function get_subsection_property_elements(subsection) { @@ -545,7 +545,7 @@ export function populate_auth_methods(auth_methods) { // As a result, the only two "incoming" assumptions on the auth method name are: // 1) It contains at least one allowed symbol // 2) No two auth method names are identical after this allowlist filtering - prefix: "id_authmethod" + auth_method.toLowerCase().replace(/[^\da-z]/g, "") + "_", + prefix: "id_authmethod" + auth_method.toLowerCase().replaceAll(/[^\da-z]/g, "") + "_", }); } $auth_methods_list.html(rendered_auth_method_rows); @@ -1230,7 +1230,7 @@ export function register_save_discard_widget_handlers( // fields that must be submitted together, which is // managed by the get_complete_data_for_subsection function. const [, subsection_id] = /^org-(.*)$/.exec($subsection_elem.attr("id")); - const subsection = subsection_id.replace(/-/g, "_"); + const subsection = subsection_id.replaceAll("-", "_"); extra_data = get_complete_data_for_subsection(subsection); } diff --git a/web/src/typeahead_helper.js b/web/src/typeahead_helper.js index b6fde756b5..01bae1d6d7 100644 --- a/web/src/typeahead_helper.js +++ b/web/src/typeahead_helper.js @@ -133,7 +133,7 @@ export function render_stream(stream) { export function render_emoji(item) { const args = { is_emoji: true, - primary: item.emoji_name.replace(/_/g, " "), + primary: item.emoji_name.replaceAll("_", " "), }; if (item.emoji_url) {