Skip to content

Commit 505a9bd

Browse files
committed
Fix duplicate recursion check
1 parent a7c73b9 commit 505a9bd

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

spec/recursion-spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe('recursion', () => {
3333
});
3434

3535
it('should throw for multiple direct, nonoverlapping recursions', () => {
36-
// TODO: Has a bug and lets invalid JS syntax through
3736
expect(() => regex({plugins: [recursion]})`(?<r1>a\g<r1&R=2>?)(?<r2>a\g<r2&R=2>?)`).toThrow();
3837
});
3938

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,32 @@ export function recursion(expression) {
4343

4444
if (captureName) {
4545
groupContentsStartPos.set(captureName, token.lastIndex);
46-
// (?R=N)
46+
// `(?R=N)`
4747
} else if (rDepth) {
4848
assertMaxInBounds(rDepth);
4949
const maxDepth = +rDepth;
5050
const pre = expression.slice(0, match.index);
5151
const post = expression.slice(token.lastIndex);
5252
assertNoFollowingRecursion(post);
5353
return makeRecursive(pre, post, maxDepth, false);
54-
// \g<name&R=N>
54+
// `\g<name&R=N>`
5555
} else if (gRName) {
5656
assertMaxInBounds(gRDepth);
5757
const maxDepth = +gRDepth;
5858
const outsideOwnGroupMsg = `Recursion via \\g<${gRName}&R=${gRDepth}> must be used within the referenced group`;
59-
// Appears before/outside the referenced group
59+
// Appears before (outside) the referenced group
6060
if (!groupContentsStartPos.has(gRName)) {
6161
throw new Error(outsideOwnGroupMsg);
6262
}
6363
const startPos = groupContentsStartPos.get(gRName);
6464
const recursiveGroupContents = getGroupContents(expression, startPos);
65-
// Appears after/outside the referenced group
65+
// Appears after (outside) the referenced group
6666
if (!hasUnescaped(recursiveGroupContents, gRToken, Context.DEFAULT)) {
6767
throw new Error(outsideOwnGroupMsg)
6868
}
6969
const pre = expression.slice(startPos, match.index);
7070
const post = recursiveGroupContents.slice(pre.length + m.length);
71-
assertNoFollowingRecursion(post);
71+
assertNoFollowingRecursion(expression.slice(token.lastIndex));
7272
return expression.slice(0, startPos) +
7373
makeRecursive(pre, post, maxDepth, true) +
7474
expression.slice(startPos + recursiveGroupContents.length);

0 commit comments

Comments
 (0)