Cleanup custom regex parsing

This commit is contained in:
Nuckyz 2024-08-30 07:07:04 -03:00
parent 4ae693b2fb
commit 0a09ee1dc6
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
4 changed files with 18 additions and 14 deletions

View file

@ -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])
});

View file

@ -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<Patch, "plugin">, 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);

View file

@ -19,17 +19,23 @@
import { Patch, PatchReplacement, ReplaceFn } from "./types";
export function canonicalizeMatch<T extends RegExp | string>(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<T extends string | ReplaceFn>(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;
}

View file

@ -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);