fix(wrap): correctly detect single balanced group (#549)#699
fix(wrap): correctly detect single balanced group (#549)#699MukundaKatta wants to merge 1 commit into
Conversation
…njs#549) `wrap()` previously used `/^(?:\(.*\)|\\?.)$/` to decide whether a value was "already grouped". That only checks the leading `(` and trailing `)`, so a concatenation like `(?:[\-_.])?(?<number>\d+)` (which starts with `(` and ends with `)` but is really two atoms) was treated as already-grouped. The resulting `maybe(...)` output `(?:[\-_.])?(?<number>\d+)?` only made the trailing named capture optional. Replace the regex with a small balance-aware scan that returns true only when the string is a single parenthesised group spanning from the first `(` to the last `)`. Character classes are skipped so a literal `[(]` doesn't unbalance the counter. Fixes unjs#549.
|
@MukundaKatta is attempting to deploy a commit to the Nuxt Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis pull request fixes the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes #549 — `maybe()` / `optionally()` incorrectly handled named captures.
`wrap()` decided whether a value was already grouped via:
```ts
const NO_WRAP_RE = /^(?:(.*)|\?.)$/
```
That check only looks at the first and last characters. A concatenation such as `(?:[-_.])?(?\d+)` starts with `(` and ends with `)` but is really two adjacent atoms, so `wrap()` skipped grouping it. The final `?` from `maybe` then bound to the trailing named capture alone:
```
/(?:beta|dev)(?:[-.])?(?\d+)?/gi // before
/(?:beta|dev)(?:(?:[-.])?(?\d+))?/gi // after
```
Replaced the regex with a small balance-aware scan (`isSingleGroup`) that returns true only when the value is a single parenthesised group spanning from the first `(` to the last `)`. Character classes are skipped so literal `[(]` / `[)]` don't unbalance the count; escaped characters are also skipped.
Test plan
Summary by CodeRabbit
Bug Fixes
Tests