From 5b350b3a27f0bc97bc3b3fe2b7a9aa5b023d427a Mon Sep 17 00:00:00 2001 From: givensuman Date: Thu, 16 Oct 2025 08:44:14 -0400 Subject: [PATCH 1/2] implement bugfix for maybe wrapping named capture groups --- src/core/inputs.ts | 4 ++-- test/inputs.test.ts | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/inputs.ts b/src/core/inputs.ts index 37722250..732cd1a5 100644 --- a/src/core/inputs.ts +++ b/src/core/inputs.ts @@ -81,11 +81,11 @@ export function maybe< Inputs extends InputSource[], Value extends string = Join, '', ''>, >(...inputs: Inputs): Input< - IfUnwrapped, + `(?:${Value})?`, MapToGroups, MapToCapturedGroupsArr > { - return createInput(`${wrap(exactly(...inputs))}?`) + return createInput(`(?:${exactly(...inputs)})?`) } /** diff --git a/test/inputs.test.ts b/test/inputs.test.ts index 657ff83d..cd7b3e1f 100644 --- a/test/inputs.test.ts +++ b/test/inputs.test.ts @@ -70,10 +70,10 @@ describe('inputs', () => { expect(regexp).toMatchInlineSnapshot('/\\(\\?:foo\\)\\?/') expectTypeOf(extractRegExp(input)).toEqualTypeOf<'(?:foo)?'>() - const nestedInputWithGroup = maybe(exactly('foo').groupedAs('groupName')) - expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOf< - MagicRegExp<'/(?foo)?/', 'groupName', ['(?foo)'], never> - >() + // const nestedInputWithGroup = maybe(exactly('foo').groupedAs('groupName')) + // expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOf< + // MagicRegExp<'/(?: (?foo) )?/', 'groupName', ['(?foo)'], never> + // >() const multi = maybe('foo', input.groupedAs('groupName'), 'bar') const regexp2 = new RegExp(multi as any) @@ -81,6 +81,21 @@ describe('inputs', () => { '/\\(\\?:foo\\(\\?\\(\\?:foo\\)\\?\\)bar\\)\\?/', ) expectTypeOf(extractRegExp(multi)).toEqualTypeOf<'(?:foo(?(?:foo)?)bar)?'>() + + const withCaptureGroup = maybe(charIn('-_.').optionally(), oneOrMore(digit).as('number')) + const regexp3 = new RegExp(withCaptureGroup as any) + expect(regexp3).toMatchInlineSnapshot(`/\\(\\?:\\(\\?:\\[\\\\-_\\.\\]\\)\\?\\(\\?\\\\d\\+\\)\\)\\?/`) + const withCaptureGroup2 = exactly( + anyOf('beta', 'dev'), + maybe( + charIn('-_.').optionally(), + oneOrMore(digit).as('number'), + ), + ) + const regexp4 = createRegExp(withCaptureGroup2, ['g', 'i']) + // /(?:beta|dev)(?:(?:[\-_.])?(?\d+))?/gi + // "/\(\?:beta\|dev\)\(\?:\(\?:\[\\-_\.\]\)\?\(\?\\d\+\)\)\?/gi" + expect(regexp4).toMatchInlineSnapshot(`/\\(\\?:beta\\|dev\\)\\(\\?:\\(\\?:\\[\\\\-_\\.\\]\\)\\?\\(\\?\\\\d\\+\\)\\)\\?/gi`) }) it('oneOrMore', () => { const input = oneOrMore('foo') From 4a646aa042d3565fc2c1072640180a251d989645 Mon Sep 17 00:00:00 2001 From: givensuman Date: Thu, 16 Oct 2025 08:44:14 -0400 Subject: [PATCH 2/2] implement bugfix for maybe wrapping named capture groups --- test/inputs.test.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/inputs.test.ts b/test/inputs.test.ts index cd7b3e1f..772cfe80 100644 --- a/test/inputs.test.ts +++ b/test/inputs.test.ts @@ -70,10 +70,9 @@ describe('inputs', () => { expect(regexp).toMatchInlineSnapshot('/\\(\\?:foo\\)\\?/') expectTypeOf(extractRegExp(input)).toEqualTypeOf<'(?:foo)?'>() - // const nestedInputWithGroup = maybe(exactly('foo').groupedAs('groupName')) - // expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOf< - // MagicRegExp<'/(?: (?foo) )?/', 'groupName', ['(?foo)'], never> - // >() + const nestedInputWithGroup = maybe(exactly('foo').groupedAs('groupName')) + expectTypeOf(createRegExp(nestedInputWithGroup)).toEqualTypeOffoo))?/', 'groupName', ['(?foo)'], never> + >() const multi = maybe('foo', input.groupedAs('groupName'), 'bar') const regexp2 = new RegExp(multi as any) @@ -93,8 +92,6 @@ describe('inputs', () => { ), ) const regexp4 = createRegExp(withCaptureGroup2, ['g', 'i']) - // /(?:beta|dev)(?:(?:[\-_.])?(?\d+))?/gi - // "/\(\?:beta\|dev\)\(\?:\(\?:\[\\-_\.\]\)\?\(\?\\d\+\)\)\?/gi" expect(regexp4).toMatchInlineSnapshot(`/\\(\\?:beta\\|dev\\)\\(\\?:\\(\\?:\\[\\\\-_\\.\\]\\)\\?\\(\\?\\\\d\\+\\)\\)\\?/gi`) }) it('oneOrMore', () => {