fix(regex): include the empty string when a zero-min quantifier applies to a number pattern#1628
Open
spokodev wants to merge 1 commit into
Open
Conversation
…es to a number pattern
Contributor
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — fix for regex zero-min quantifier incorrectly narrowing number patterns to exclude the empty string.
- Make
tryFastPathinquantify.tsmin-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\din a capture group.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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
tryFastPathbranch inark/regex/quantify.tsreturns 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 themin extends 0handling, so every zero-min quantifier (*,?,{0,n}) over a number pattern drops the empty branch. Thestringfast path is unaffected becausestringalready 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\din a capture group) fail onmainwithand pass with this change. Full mocha run: 1766 passing; the 2 failures in
ark/attest/__tests__/snapPopulation.test.tsreproduce identically on a clean checkout ofmainand are unrelated. Prettier and ESLint clean on the changed files.