Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PatchHelper: Fix incorrect error handling with canonicalizeMatch #3194

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/VencordSettings/PatchHelperTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ function ReplacementComponent({ module, match, replacement, setReplacementError
const [patchedCode, matchResult, diff] = React.useMemo(() => {
const src: string = fact.toString().replaceAll("\n", "");

let canonicalMatch;
try {
new RegExp(match);
canonicalMatch = canonicalizeMatch(new RegExp(match));
} catch (e) {
return ["", [], []];
}
const canonicalMatch = canonicalizeMatch(new RegExp(match));
try {
const canonicalReplace = canonicalizeReplace(replacement, "YourPlugin");
var patched = src.replace(canonicalMatch, canonicalReplace as string);
Expand Down Expand Up @@ -327,7 +327,7 @@ function PatchHelper() {
setMatch(v);

try {
new RegExp(v);
canonicalizeMatch(new RegExp(v));
setMatchError(void 0);
} catch (e: any) {
setMatchError((e as Error).message);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import { runtimeHashMessageKey } from "./intlHash";
import { Patch, PatchReplacement, ReplaceFn } from "./types";

/**
* NOTE: This function can throw an error, even if `new RegExp()` would not.
* Eg: `canonicalizeMatch(/\\i/)` throws, but `new RegExp(/\\i/)` does not.
*/
export function canonicalizeMatch<T extends RegExp | string>(match: T): T {
let partialCanon = typeof match === "string" ? match : match.source;
partialCanon = partialCanon.replaceAll(/#{intl::([\w$+/]*)(?:::(\w+))?}/g, (_, key, modifier) => {
Expand Down