Skip to content

fix(regex): include the empty string when a zero-min quantifier applies to a number pattern#1628

Merged
ssalbdivad merged 1 commit into
arktypeio:mainfrom
spokodev:fix/arkregex-zero-quantifier-capture
Jun 16, 2026
Merged

fix(regex): include the empty string when a zero-min quantifier applies to a number pattern#1628
ssalbdivad merged 1 commit into
arktypeio:mainfrom
spokodev:fix/arkregex-zero-quantifier-capture

Conversation

@spokodev

Copy link
Copy Markdown
Contributor

Fixes #1625

Problem

regex("(\\d*)") types the capture group as `${number}`, which excludes the empty string even though * matches zero repetitions. ? has the same problem (regex("(\\d?)")`${number}`).

The tryFastPath branch in ark/regex/quantify.ts returns early for number-like patterns on the premise that repeating `${number}` does not change the type — true for one or more repetitions, but zero repetitions produce "", which `${number}` does not include. The early return runs before the min extends 0 handling, so every zero-min quantifier (*, ?, {0,n}) over a number pattern drops the empty branch. The string fast path is unaffected because string already contains "".

Fix

Make the number fast path min-aware: zero-min quantifiers produce "" | ${number}`` and quantifiers withmin >= 1keep the existing ``${number}` `` result.

Tests

Two type-level tests (* and ? over \d in a capture group) fail on main with

+ 'Regex<`${number}`, { captures: [`${number}`]; }>'
- 'Regex<`${number}` | "", { captures: [`${number}` | ""]; }>'

and pass with this change. Full mocha run: 1766 passing; the 2 failures in ark/attest/__tests__/snapPopulation.test.ts reproduce identically on a clean checkout of main and are unrelated. Prettier and ESLint clean on the changed files.

@github-project-automation github-project-automation Bot moved this to To do in arktypeio Jun 12, 2026
@spokodev spokodev marked this pull request as ready for review June 12, 2026 18:15

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — fix for regex zero-min quantifier incorrectly narrowing number patterns to exclude the empty string.

  • Make tryFastPath in quantify.ts min-aware for number patterns — when a zero-min quantifier (*, ?, {0,n}) applies to a `${number}` pattern, the result now includes "" | `${number}` instead of just `${number}`.
  • Add type-level tests for * and ? over \d in a capture group.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@ssalbdivad ssalbdivad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@ssalbdivad ssalbdivad merged commit a0bce07 into arktypeio:main Jun 16, 2026
5 of 6 checks passed
@github-project-automation github-project-automation Bot moved this from To do to Done (merged or closed) in arktypeio Jun 16, 2026
ssalbdivad added a commit that referenced this pull request Jun 16, 2026
Bump arkregex to 0.0.6 for the zero-min quantifier number-pattern fix
(#1628) and propagate to arktype 2.2.1. Bump downstream publishable
packages @ark/json-schema, @ark/fast-check, and @ark/attest.

Add fast-check to packageScopes so it is included in ci:publish.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done (merged or closed)

Development

Successfully merging this pull request may close these issues.

Capture group mistyped with * quantifier

2 participants