fix(filters): preserve regex meaning when changing operators

Swap wrapped and raw regex sources only when both compile identically after saving. Leave literal, glob, multiline and Unicode-sensitive comparisons unchanged rather than inventing non-equivalent conversions.
This commit is contained in:
翅膀 committed 2026-10-17 07:47:03 +08:00
1 parent 2717ab49dc
commit 54b2944dd4
5 files changed
+129 -8

No files matched your search

+32
View File
@@ -1198,6 +1198,38 @@ function typeValueOptions(
}
}
// ---------------------------------------------------------------------------
// Operator changes
// ---------------------------------------------------------------------------
/** Operators whose values may be a wrapped `/regex/` (server `matchField`). */
function readsWrappedRegex(op: FilterOp): boolean {
return op === "eq" || op === "ne" || op === "in";
}
/**
* Values rewritten for a new operator where the rewrite matches exactly the
* same: a wrapped `/x/` of the default operators and the raw `x` of `regex`
* compile to the same server regex, so they are swapped (`/^v\d+/` ↔
* `^v\d+`). Nothing else is rewritten — literal, glob and substring
* comparisons differ from regexes on line breaks and Unicode case folding —
* so other values stay as typed, and validation flags those that can never
* match. Saving trims every value, so a source that is empty or starts/ends
* with whitespace (`//`, `/ main /`) cannot stand alone and stays wrapped.
*/
export function convertValuesForOp(values: string[], from: FilterOp, to: FilterOp): string[] {
if (readsWrappedRegex(from) && to === "regex") {
return values.map((v) => {
const value = v.trim();
const source = value.slice(1, -1);
return isWrappedRegex(value) && source && source === source.trim() ? source : v;
});
}
if (from === "regex" && readsWrappedRegex(to))
return values.map((v) => (v.trim() ? `/${v.trim()}/` : v));
return values;
}
// ---------------------------------------------------------------------------
// Validation (mirrors server/lib/events/filter-ast.ts)
// ---------------------------------------------------------------------------