fix(176): total escaping so wildcard/quoted values round-trip (parse review)

This commit is contained in:
2026-07-18 02:41:50 +02:00
parent 0b5258533a
commit 218e1810cb
4 changed files with 53 additions and 18 deletions
@@ -761,10 +761,14 @@ function lcg(seed: number) {
};
}
const pick = <T,>(rng: () => number, arr: T[]): T => arr[Math.floor(rng() * arr.length)];
// Include Lucene specials, spaces, quotes and backslashes: escaping must be TOTAL, so string
// values must round-trip regardless of content (this is what catches escaping bugs the pure
// alphanumeric generator would miss). Numbers/dates are generated separately in makeRule.
const VALUE_CHARS = 'abcdefghijABCDEFGHIJ0123456789 *?"\\():-';
const token = (rng: () => number): string => {
const n = 3 + Math.floor(rng() * 5);
let out = '';
for (let i = 0; i < n; i++) out += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'[Math.floor(rng() * 62)];
for (let i = 0; i < n; i++) out += VALUE_CHARS[Math.floor(rng() * VALUE_CHARS.length)];
return out;
};
+5 -2
View File
@@ -5,9 +5,12 @@ function quote(value: string): string {
return `"${value.replace(/(["\\])/g, '\\$1')}"`;
}
// Wildcard/prefix values escape Lucene specials but keep the wildcard the operator adds.
const WILD_SPECIAL = /([\s+\-!(){}[\]^"~*?:\\/])/g;
// Wildcard/prefix values escape whitespace and every Lucene special (incl. * and ?) so the
// operator's boundary * are the only unescaped stars — the parser keys off exactly that.
function escapeWild(value: string): string {
return value.replace(/([+\-!(){}[\]^"~:\\/])/g, '\\$1');
return value.replace(WILD_SPECIAL, '\\$1');
}
function compileRule(rule: Rule): string {
+20
View File
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { parse } from './parse';
import { compile } from './compile';
import type { FieldType } from './types';
const FIELDS: Record<string, FieldType> = {
@@ -52,3 +53,22 @@ describe('parse', () => {
expect(parse('title:jo~2', FIELDS)).toBeNull(); // fuzzy — not in subset
});
});
describe('parse: special-char round-trips', () => {
it('round-trips quoted values with lucene specials (compile∘parse)', () => {
for (const v of ['a*b', 'x y', 'a\\', 'has"quote', '(paren)', 'a AND b', 'a:b']) {
const g = { match: 'all' as const, children: [{ field: 'genre', operator: 'is' as const, value: v }] };
expect(parse(compile(g), FIELDS)).toEqual(g);
}
});
it('round-trips text contains/startsWith values with specials', () => {
for (const v of ['a*b', 'x y', 'plain']) {
const c = { match: 'all' as const, children: [{ field: 'title', operator: 'contains' as const, value: v }] };
expect(parse(compile(c), FIELDS)).toEqual(c);
}
for (const v of ['star*', 'x y', 'ab']) {
const s = { match: 'all' as const, children: [{ field: 'title', operator: 'startsWith' as const, value: v }] };
expect(parse(compile(s), FIELDS)).toEqual(s);
}
});
});
+23 -15
View File
@@ -12,12 +12,9 @@ function splitTopLevel(input: string): { parts: string[]; match: 'all' | 'any' }
for (let i = 0; i < input.length; i++) {
const ch = input[i];
if (quoted) {
token += ch;
if (ch === '"' && input[i - 1] !== '\\') quoted = false;
continue;
}
if (ch === '"') { quoted = true; token += ch; continue; }
if (ch === '\\') { token += ch + (input[i + 1] ?? ''); i++; continue; } // escape pair, anywhere
if (ch === '"') { quoted = !quoted; token += ch; continue; }
if (quoted) { token += ch; continue; }
if (ch === '(') { depth++; token += ch; continue; }
if (ch === ')') { depth--; token += ch; continue; }
if (depth === 0 && input.startsWith(' AND ', i)) { parts.push(token); ops.push('AND'); token = ''; i += 4; continue; }
@@ -38,6 +35,15 @@ function unquote(value: string): string {
return value.replace(/\\(["\\])/g, '$1');
}
// A trailing '*' is an operator wildcard iff it is not escaped — i.e. preceded by an even run of backslashes.
function endsWithOperatorStar(s: string): boolean {
if (!s.endsWith('*')) return false;
let backslashes = 0;
let i = s.length - 2;
while (i >= 0 && s[i] === '\\') { backslashes++; i--; }
return backslashes % 2 === 0;
}
function parseAtom(atom: string, fieldTypes: Record<string, FieldType>): Rule | null {
const trimmed = atom.trim();
const negate = trimmed.startsWith('NOT ');
@@ -71,14 +77,16 @@ function parseAtom(atom: string, fieldTypes: Record<string, FieldType>): Rule |
if (negate) return null; // NOT only valid with quoted forms above
// Wildcards (text only): *v* → contains, v* → startsWith
if (raw.startsWith('*') && raw.endsWith('*') && raw.length >= 2) {
if (type !== 'text') return null;
return mk(field, 'contains', unescapeWild(raw.slice(1, -1)));
}
if (/^[^*]+\*$/.test(raw)) {
if (type !== 'text') return null;
return mk(field, 'startsWith', unescapeWild(raw.slice(0, -1)));
// Wildcards (text only): *v* → contains, v* → startsWith. The operator stars are the
// unescaped boundary stars; any literal * in the value was escaped to \* by the compiler.
if (type === 'text' && endsWithOperatorStar(raw)) {
if (raw.startsWith('*') && raw.length >= 2) {
return mk(field, 'contains', unescapeWild(raw.slice(1, -1)));
}
if (raw[0] !== '*') {
return mk(field, 'startsWith', unescapeWild(raw.slice(0, -1)));
}
return null;
}
// Bare token → numeric eq only (reject fuzzy ~, boosts ^, etc.)
@@ -88,7 +96,7 @@ function parseAtom(atom: string, fieldTypes: Record<string, FieldType>): Rule |
}
function unescapeWild(value: string): string {
return value.replace(/\\([+\-!(){}[\]^"~:\\/])/g, '$1');
return value.replace(/\\([\s\S])/g, '$1');
}
function mk(field: string, operator: Operator, value: string, value2?: string): Rule {