From 0a09ee1dc6186e357191a72a357cadc287680ef1 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:07:04 -0300 Subject: [PATCH] Cleanup custom regex parsing --- scripts/generateReport.ts | 2 +- src/plugins/index.ts | 11 ++++++----- src/utils/patches.ts | 16 +++++++++++----- src/webpack/patchWebpack.ts | 3 --- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/scripts/generateReport.ts b/scripts/generateReport.ts index a2f8bb1fb..03f803633 100644 --- a/scripts/generateReport.ts +++ b/scripts/generateReport.ts @@ -225,7 +225,7 @@ page.on("console", async e => { plugin, type, id, - match: regex.replace(/\[A-Za-z_\$\]\[\\w\$\]\*/g, "\\i"), + match: regex, error: await maybeGetError(e.args()[3]) }); diff --git a/src/plugins/index.ts b/src/plugins/index.ts index e6caf7340..3975b377e 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -20,7 +20,7 @@ import { registerCommand, unregisterCommand } from "@api/Commands"; import { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu"; import { Settings } from "@api/Settings"; import { Logger } from "@utils/Logger"; -import { canonicalizeFind } from "@utils/patches"; +import { canonicalizeFind, canonicalizeReplacement } from "@utils/patches"; import { Patch, Plugin, ReporterTestable, StartAt } from "@utils/types"; import { FluxDispatcher } from "@webpack/common"; import { FluxEvents } from "@webpack/types"; @@ -67,11 +67,12 @@ export function addPatch(newPatch: Omit, pluginName: string) { } patch.replacement = patch.replacement.filter(({ predicate }) => !predicate || predicate()); + for (const replacement of patch.replacement) { + canonicalizeReplacement(replacement, pluginName); - if (IS_REPORTER) { - patch.replacement.forEach(r => { - delete r.predicate; - }); + if (IS_REPORTER) { + delete replacement.predicate; + } } patches.push(patch); diff --git a/src/utils/patches.ts b/src/utils/patches.ts index 87f3ce78c..979e49fd7 100644 --- a/src/utils/patches.ts +++ b/src/utils/patches.ts @@ -19,17 +19,23 @@ import { Patch, PatchReplacement, ReplaceFn } from "./types"; export function canonicalizeMatch(match: T): T { - if (typeof match === "string") return match; - const canonSource = match.source - .replaceAll("\\i", "[A-Za-z_$][\\w$]*"); - return new RegExp(canonSource, match.flags) as T; + if (typeof match === "string") { + return match; + } + + const canonRegex = new RegExp(match.source.replaceAll(String.raw`\i`, String.raw`(?:[A-Za-z_$][\w$]*)`), match.flags); + const originalToString = canonRegex.toString.bind(canonRegex); + canonRegex.toString = () => originalToString().replaceAll(String.raw`(?:[A-Za-z_$][\w$]*)`, String.raw`\i`); + + return canonRegex as T; } export function canonicalizeReplace(replace: T, pluginName: string): T { const self = `Vencord.Plugins.plugins[${JSON.stringify(pluginName)}]`; - if (typeof replace !== "function") + if (typeof replace !== "function") { return replace.replaceAll("$self", self) as T; + } return ((...args) => replace(...args).replaceAll("$self", self)) as T; } diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index 226978597..4156f0c13 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -7,7 +7,6 @@ import { Settings } from "@api/Settings"; import { Logger } from "@utils/Logger"; import { interpolateIfDefined } from "@utils/misc"; -import { canonicalizeReplacement } from "@utils/patches"; import { PatchReplacement } from "@utils/types"; import { traceFunctionWithResults } from "../debug/Tracer"; @@ -432,8 +431,6 @@ function patchFactory(id: PropertyKey, factory: AnyModuleFactory) { const lastCode = code; const lastFactory = factory; - canonicalizeReplacement(replacement, patch.plugin); - try { const [newCode, totalTime] = executePatch(replacement.match, replacement.replace as string);