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, plugin,
type, type,
id, id,
match: regex.replace(/\[A-Za-z_\$\]\[\\w\$\]\*/g, "\\i"), match: regex,
error: await maybeGetError(e.args()[3]) 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 { addContextMenuPatch, removeContextMenuPatch } from "@api/ContextMenu";
import { Settings } from "@api/Settings"; import { Settings } from "@api/Settings";
import { Logger } from "@utils/Logger"; 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 { Patch, Plugin, ReporterTestable, StartAt } from "@utils/types";
import { FluxDispatcher } from "@webpack/common"; import { FluxDispatcher } from "@webpack/common";
import { FluxEvents } from "@webpack/types"; 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()); patch.replacement = patch.replacement.filter(({ predicate }) => !predicate || predicate());
for (const replacement of patch.replacement) {
canonicalizeReplacement(replacement, pluginName);
if (IS_REPORTER) { if (IS_REPORTER) {
patch.replacement.forEach(r => { delete replacement.predicate;
delete r.predicate; }
});
} }
patches.push(patch); patches.push(patch);

View file

@ -19,17 +19,23 @@
import { Patch, PatchReplacement, ReplaceFn } from "./types"; import { Patch, PatchReplacement, ReplaceFn } from "./types";
export function canonicalizeMatch<T extends RegExp | string>(match: T): T { export function canonicalizeMatch<T extends RegExp | string>(match: T): T {
if (typeof match === "string") return match; if (typeof match === "string") {
const canonSource = match.source return match;
.replaceAll("\\i", "[A-Za-z_$][\\w$]*"); }
return new RegExp(canonSource, match.flags) as T;
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 { export function canonicalizeReplace<T extends string | ReplaceFn>(replace: T, pluginName: string): T {
const self = `Vencord.Plugins.plugins[${JSON.stringify(pluginName)}]`; const self = `Vencord.Plugins.plugins[${JSON.stringify(pluginName)}]`;
if (typeof replace !== "function") if (typeof replace !== "function") {
return replace.replaceAll("$self", self) as T; return replace.replaceAll("$self", self) as T;
}
return ((...args) => replace(...args).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 { Settings } from "@api/Settings";
import { Logger } from "@utils/Logger"; import { Logger } from "@utils/Logger";
import { interpolateIfDefined } from "@utils/misc"; import { interpolateIfDefined } from "@utils/misc";
import { canonicalizeReplacement } from "@utils/patches";
import { PatchReplacement } from "@utils/types"; import { PatchReplacement } from "@utils/types";
import { traceFunctionWithResults } from "../debug/Tracer"; import { traceFunctionWithResults } from "../debug/Tracer";
@ -432,8 +431,6 @@ function patchFactory(id: PropertyKey, factory: AnyModuleFactory) {
const lastCode = code; const lastCode = code;
const lastFactory = factory; const lastFactory = factory;
canonicalizeReplacement(replacement, patch.plugin);
try { try {
const [newCode, totalTime] = executePatch(replacement.match, replacement.replace as string); const [newCode, totalTime] = executePatch(replacement.match, replacement.replace as string);