Skip to content

Commit

Permalink
Fix multiplexing edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Oct 30, 2024
1 parent a5c8f18 commit 33d3808
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Specifically, this option enables the following additional features, depending o

If `null`, any use of recursion throws. If an integer between `2` and `100` (and `allowBestEffort` is on), common recursion forms are supported and recurse up to the specified max depth.

Using a higher limit is not a problem if needed. Although it can add a slight performance cost, that's limited to regexes that actually use recursion.

*Default: `6`.*

### `optimize`
Expand Down
2 changes: 1 addition & 1 deletion dist/index.min.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion spec/match-backreference.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Backreference', () => {
expect('').not.toFindMatch(r`(\1)`);
expect('').not.toFindMatch(r`(((\2)))`);
expect(['a', 'aa']).not.toFindMatch(r`(a\1)`);
expect('').not.toFindMatch(r`(\g<2>(\1))`);
});

it('should throw if not enough captures to the left', () => {
Expand Down Expand Up @@ -86,6 +87,7 @@ describe('Backreference', () => {
expect('').not.toFindMatch(r`(\k<1>)`);
expect('').not.toFindMatch(r`(((\k<2>)))`);
expect(['a', 'aa']).not.toFindMatch(r`(a\k<1>)`);
expect('').not.toFindMatch(r`(\g<2>(\k<1>))`);
});

it('should throw if not enough captures to the left', () => {
Expand Down Expand Up @@ -167,6 +169,7 @@ describe('Backreference', () => {
expect('').not.toFindMatch(r`(\k<-1>)`);
expect('').not.toFindMatch(r`(((\k<-2>)))`);
expect(['a', 'aa']).not.toFindMatch(r`(a\k<-1>)`);
expect('').not.toFindMatch(r`(\g<+1>(\k<-2>))`);
});

it('should throw if not enough captures to the left', () => {
Expand Down Expand Up @@ -255,12 +258,13 @@ describe('Backreference', () => {
expect('').not.toFindMatch(r`(?<a>\k<a>)`);
expect('').not.toFindMatch(r`(?<a>(?<b>(?<c>\k<b>)))`);
expect(['a', 'aa']).not.toFindMatch(r`(?<a>a\k<a>)`);
expect('').not.toFindMatch(r`(?<a>\g<b>(?<b>\k<a>))`);
expect('').not.toFindMatch(r`(?<a>(?<a>\k<a>))`);
expect('aa').toExactlyMatch(r`(?<n>a)\k<n>|(?<n>b\k<n>)`);
expect(['a', 'b', 'ba', 'bb']).not.toFindMatch(r`(?<n>a)\k<n>|(?<n>b\k<n>)`);
});

it('should only preclude the not-yet-closed groups when multiplexing', () => {
it('should preclude only the not-yet-closed groups when multiplexing', () => {
expect('aa').toExactlyMatch(r`(?<a>a)(?<a>\k<a>)`);
expect('aba').toExactlyMatch(r`(?<n>a)(?<n>b\k<n>)`);
expect(['aa', 'bcb']).toExactlyMatch(r`(?<n>a)\k<n>|(?<n>b)(?<n>c\k<n>)`);
Expand Down Expand Up @@ -306,6 +310,7 @@ describe('Backreference', () => {

it('should increase multiplexing as duplicate names are added to the left', () => {
expect(['aaba', 'aabb']).toExactlyMatch(r`(?<n>a)\k<n>(?<n>b)\k<n>`);
expect(['aaba', 'aabb']).toExactlyMatch(r`((?<n>a)\k<n>)(?<n>b)\k<n>`);
expect(['abba', 'abbb']).not.toFindMatch(r`(?<n>a)\k<n>(?<n>b)\k<n>`);
});

Expand Down
2 changes: 2 additions & 0 deletions spec/match-subroutine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ beforeEach(() => {
});

describe('Subroutine', () => {
// TODO: Test `\g'n'` syntax

describe('numbered', () => {
it('should match the expression within the referenced group', () => {
expect('aa').toExactlyMatch(r`(a)\g<1>`);
Expand Down
4 changes: 2 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ const SecondPassVisitor = {
// named groups from their preceding siblings
let upAlt = getParentAlternative(parentAlt);
if (upAlt) {
while ((upAlt = getParentAlternative(upAlt))) {
do {
getOrCreate(namedGroupsInScopeByAlt, upAlt, new Map()).set(name, node);
}
} while ((upAlt = getParentAlternative(upAlt)));
}
}
},
Expand Down

0 comments on commit 33d3808

Please sign in to comment.