Skip to content

Commit 87a3f0c

Browse files
Improved prefixes of empty backreferences
1 parent d756e1b commit 87a3f0c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/longest-prefix.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { CharSet } from "refa";
22
import { Alternative, CapturingGroup, Element, Group, Quantifier } from "regexpp/ast";
3-
import { isPotentiallyZeroLength, isStrictBackreference, isZeroLength, MatchingDirection } from "./basic";
3+
import {
4+
isEmptyBackreference,
5+
isPotentiallyZeroLength,
6+
isStrictBackreference,
7+
isZeroLength,
8+
MatchingDirection,
9+
} from "./basic";
410
import { CacheInstance } from "./cache";
511
import { ReadonlyFlags } from "./flags";
612
import {
@@ -173,6 +179,9 @@ function getElementPrefix(
173179
return getQuantifierPrefix(element, direction, options, flags);
174180

175181
case "Backreference": {
182+
if (isEmptyBackreference(element)) {
183+
return EMPTY_COMPLETE;
184+
}
176185
if (isStrictBackreference(element)) {
177186
const inner = getElementPrefix(element.resolved, direction, { ...options, includeAfter: false }, flags);
178187
return inner;

tests/__snapshots__/longest-prefix.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ Object {
1414
}
1515
`;
1616
17+
exports[`getLongestPrefix /(?<this>\\1\\2b)c(a)/ ltr 1`] = `
18+
Object {
19+
"{\\"includeAfter\\":false,\\"looseGroups\\":false}": /b/,
20+
"{\\"includeAfter\\":true,\\"looseGroups\\":false}": /bc/,
21+
}
22+
`;
23+
24+
exports[`getLongestPrefix /(?<this>\\1\\2b)c(a)/ rtl 1`] = `
25+
Object {
26+
"{\\"includeAfter\\":false,\\"looseGroups\\":false}": /b/,
27+
"{\\"includeAfter\\":true,\\"looseGroups\\":false}": /b\\[\\^\\]/,
28+
}
29+
`;
30+
1731
exports[`getLongestPrefix /(?<this>A(?!a)(?:(?!B)|(?!C)))\\w/ ltr 1`] = `
1832
Object {
1933
"{\\"includeAfter\\":false,\\"looseGroups\\":false}": /A/,

tests/longest-prefix.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe(RAA.getLongestPrefix.name, function () {
5252
/(a)b(?<this>\1)c/,
5353
/(ab?)b(?<this>\1)c/,
5454
/(?:(a)|f)b(?<this>\1)c/,
55+
/(?<this>\1\2b)c(a)/,
5556
]);
5657

5758
function test(regexes: RegExp[]): void {

0 commit comments

Comments
 (0)