Skip to content

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Dec 25, 2025

Per ECMAScript spec, when an optional group matches but with zero-width
(e.g., contains only a lookahead), captures inside should be undefined.
This is because a zero-width optional match is equivalent to skipping
the group entirely.

Implementation:

  • Add new RESET_IF_NO_ADV opcode that resets captures if position unchanged
  • In compiler, detect optional groups with zero-width bodies (e.g., lookaheads)
  • Emit SET_POS before and RESET_IF_NO_ADV after such bodies
  • In VM, handle the new opcode to conditionally reset captures

This fixes the following tests:

  • test_optional_lookahead_no_match
  • test_repeated_optional_lookahead

Claude Code transcript: https://gistpreview.github.io/?65d1a6449a8077c59946435f485121bf/index.html

Per ECMAScript spec, when an optional group matches but with zero-width
(e.g., contains only a lookahead), captures inside should be undefined.
This is because a zero-width optional match is equivalent to skipping
the group entirely.

Implementation:
- Add new RESET_IF_NO_ADV opcode that resets captures if position unchanged
- In compiler, detect optional groups with zero-width bodies (e.g., lookaheads)
- Emit SET_POS before and RESET_IF_NO_ADV after such bodies
- In VM, handle the new opcode to conditionally reset captures

This fixes the following tests:
- test_optional_lookahead_no_match
- test_repeated_optional_lookahead
@simonw simonw merged commit 11a2091 into main Dec 25, 2025
10 checks passed
simonw added a commit that referenced this pull request Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants