github arktypeio/arktype arkregex@0.0.16

34 minutes ago
  • Prevented TS2589 ("Type instantiation is excessively deep") on long case-insensitive patterns and sibling (?i:...) groups by sharing and widening the cased-char expansion budget cleanly (#1533).
  • Accepted flags in any order and normalized them to canonical alphabetical order (e.g. "ig" -> "gi"), extracted ErrorMessage across union flags, accepted wide string input as Regex<string>, and returned never from regex.validate on never-parsing sources while rejecting duplicate, invalid, or mutually exclusive (u and v) flags as type errors.
  • Improved JavaScript RegExp parity (#1771): reject out-of-order character class ranges (including inside negated sets like [^z-a] and \u/\x range escapes), duplicate capture group names in the same branch, \p{...} / \P{...} without u/v, \- outside character classes under u/v (while keeping it legal without u/v), incomplete quantifiers and lone }/] under u/v, and non-ASCII-digit quantifiers; enforce ES2024 v-flag ClassSetExpression grammar (mixed set ops, reserved double punctuators, nested [...], negated \q{...}) and cross-branch \k<name> references.
  • Fixed soundness bugs in regex type inference: \d and ^a+ assignability under TypeScript 7.1, case-insensitive character sets and backreferences, m modifier groups like (?m:$), zero-quantified captures like (a){0}, modifier groups like (?i-:abc), and invalid capture group names.
  • Exported RegexParser helper type (#1777).

Type an unmatched capture's indices as undefined

With the d flag, exec sets indices[i] and indices.groups[name] to undefined for a capture that didn't participate in the match, but each was typed as a range. A capture that may not match is now [start, end] | undefined, and one absent from the matching branch undefined:

const { indices } = regex("^(a)?b$", "d").exec("b")!

// was: [start: number, end: number], now: [start: number, end: number] | undefined
indices[1]

infer capture indices across alternation branches

A capture in one alternation branch is undefined in every other branch, so the inferred tuples line up with what exec returns (#1530):

// was: ["1"] | [undefined, "2"] | [undefined, "3"]
// now: ["1", undefined, undefined] | [undefined, "2", undefined] | [undefined, undefined, "3"]
regex("^(?:(?<a>1)|(?<b>2)|(?<c>3))$").inferCaptures

infer alternations whose branches each match several strings

Each branch of an alternation was inferred once for every combination of the strings the branches before it could match, so a few short case-insensitive branches were excessively deep (#1533). Each branch is now inferred once:

// was: Type instantiation is excessively deep and possibly infinite.
// now: each casing of "relay", "relai" and "point", in a string
regex("relay|relai|point", "i")

A long case-insensitive literal like instagram\.com still matches too many strings to infer.

infer escapes in a character set as what they match

In a character set, \b is a backspace and an escape that's unnecessary elsewhere matches its character, so they're inferred as those characters rather than as a boundary or an error message. An escape the u or v flag makes a SyntaxError, like [\a], is a type error with those flags:

// was: Regex<"", {}>
// now: Regex<"\b", {}>
regex("^[\\b]$")

// was: Regex<"Escape preceding / is unnecessary and should be removed.", {}>
// now: Regex<"/", {}>
regex("^[\\/]$")

infer \s as string

\s matches \r, \f, \v and Unicode spaces as well as " ", "\n" and "\t", so it's inferred as string:

// was: Regex<`a${" " | "\n" | "\t"}c`, {}>
// now: Regex<`a${string}c`, {}>
regex("^a\\sc$")

don't anchor a pattern at ^ or $ with the m flag

With m, ^ and $ also match at line breaks, so they no longer anchor the inferred pattern, and they're allowed mid-pattern:

// was: Regex<"abc", { flags: "m" }>
// now: Regex<`${string}abc${string}`, { flags: "m" }>
regex("^abc$", "m")

infer an unbounded run of digits as string

`${number}` doesn't include a run of digits too long to be a finite number, so \d+, \d* and any unbounded repetition of a digit pattern infer string (#1663). A bounded repetition of a union including digits, like (a|\d){2}, also no longer drops its other branches:

// was: Regex<`${number}`, {}>
// now: Regex<string, {}>
regex("^\\d+$")

// was: Regex<`${number}`, {}>
// now: Regex<"aa" | `${number}${number}` | `${number}a` | `a${number}`, {}>
regex("^(?:a|\\d){2}$")

count captures inside lookarounds

A capture inside a lookaround wasn't counted, so later captures were inferred at the wrong index (#1532). A capture inside a negative lookaround is undefined:

// was: Regex<"ab", { captures: ["b"] }>
// now: Regex<"ab", { captures: ["a", "b"] }>
regex("^(?=(a))a(b)$")

install arkregex, not @ark/regex

arkregex publishes under one name. @ark/regex, the same package under another name, is deprecated at its last version, and @arktype/regex is no longer published. Replace each @ark/regex import with arkregex.

import internals from arkregex/internal

arkregex/internal exports the names of every arkregex module. A deep import like arkregex/internal/parse.ts keeps its module's types and loads that entry at runtime.

Full changelog: https://github.com/arktypeio/arktype/blob/arkregex@0.0.16/ark/regex/CHANGELOG.md

Don't miss a new arktype release

NewReleases is sending notifications on new releases.