mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
fix(contracts): omit widened regex disjunctions (#11781)
This commit is contained in:
2 files changed
+32
-3
No files matched your search
@@ -206,6 +206,29 @@ const PRESENTERS = [
|
||||
] as const;
|
||||
|
||||
describe("advertised pattern presentation", () => {
|
||||
it.each(PRESENTERS)(
|
||||
"omits overlapping widened alternatives from the %s schema",
|
||||
(_kind, present) => {
|
||||
for (const pattern of ["^(?:é|è)+$", "^(?:é|[èa])+$", "^(?:(?:é|è))+$"]) {
|
||||
const canonical = new Ajv({ strict: false }).compile({
|
||||
type: "string",
|
||||
pattern,
|
||||
});
|
||||
const schema = present(property({ type: "string", pattern }));
|
||||
expect(
|
||||
(schema.properties as Record<string, unknown>).name,
|
||||
).not.toHaveProperty("pattern");
|
||||
const validate = new Ajv({ strict: false }).compile(schema);
|
||||
expect(validate({ name: "é".repeat(1000) + "A" })).toBe(true);
|
||||
expect(canonical("éè"), pattern).toBe(true);
|
||||
expect(canonical("éèA"), pattern).toBe(false);
|
||||
}
|
||||
expect(projectPortablePattern("^(?:a|b)+$").kind).toBe("exact");
|
||||
expect(projectPortablePattern("^[éè]+$").kind).toBe("wider");
|
||||
expect(projectPortablePattern("^é(?:a|b)+$").kind).toBe("wider");
|
||||
},
|
||||
);
|
||||
|
||||
const lookAround = "^(?!con$)[a-z]+$";
|
||||
const property = (schema: Record<string, unknown>) => ({
|
||||
type: "object",
|
||||
|
||||
@@ -143,7 +143,7 @@ const SHORTHAND_NON_ASCII: Readonly<Record<string, NonAscii>> = {
|
||||
|
||||
class Projector {
|
||||
#index = 0;
|
||||
#wider = false;
|
||||
#widenings = 0;
|
||||
|
||||
constructor(private readonly source: string) {}
|
||||
|
||||
@@ -165,7 +165,7 @@ class Projector {
|
||||
completed.length === 1
|
||||
? `^${completed[0]!}$`
|
||||
: `^(?:${completed.join("|")})$`;
|
||||
return { kind: this.#wider ? "wider" : "exact", pattern };
|
||||
return { kind: this.#widenings > 0 ? "wider" : "exact", pattern };
|
||||
}
|
||||
|
||||
#peek(offset = 0): string {
|
||||
@@ -173,15 +173,21 @@ class Projector {
|
||||
}
|
||||
|
||||
#widen = (): void => {
|
||||
this.#wider = true;
|
||||
this.#widenings++;
|
||||
};
|
||||
|
||||
#disjunction(): string[][] {
|
||||
const wideningsBefore = this.#widenings;
|
||||
const alternatives = [this.#alternative()];
|
||||
while (this.#peek() === "|") {
|
||||
this.#index++;
|
||||
alternatives.push(this.#alternative());
|
||||
}
|
||||
// Widening can make disjoint alternatives overlap. Repeating such a
|
||||
// group, including through a parent group, can backtrack exponentially.
|
||||
// Omit the advertisement rather than assume those branches stay disjoint.
|
||||
if (alternatives.length > 1 && this.#widenings > wideningsBefore)
|
||||
throw new Unexpressible();
|
||||
return alternatives;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user