Skip to content

Commit

Permalink
[patch] fix defect - no matches when many parenthesis groups (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty authored Mar 1, 2024
1 parent 25a5b97 commit 9246e0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Replacement = { mapName: string; exp: string; inverse: boolean };
type Replacement = { order: number; mapName: string; exp: string; inverse: boolean };

/**
* replace all parenthesis groups with placeholder
Expand All @@ -17,7 +17,7 @@ const replaceParenthesisGroups = (input: string, replacements: Replacement[], nu

const replaceExpression = (expression: string, group: string, inverse: boolean) => {
const mapName = `##R${num}##`;
replacements.push({ mapName, exp: group, inverse });
replacements.push({ mapName, exp: group, inverse, order: num });
replaced = replaced.replace(expression, mapName);

return replaceParenthesisGroups(replaced, replacements, num + 1);
Expand Down Expand Up @@ -74,7 +74,7 @@ export const selectionTestGrep = (str: string): RegExp => {

// last group should be converted first
groups
.sort((a, b) => (a.mapName > b.mapName ? -1 : 1))
.sort((a, b) => (a.order > b.order ? -1 : 1))
.forEach(r => {
convertedString = convertedString.replace(r.mapName, r.reg);
});
Expand Down
13 changes: 13 additions & 0 deletions tests/test-folder/regexp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ describe('suite', () => {
{ expectMatch: false, testLine: 'his test' },
],
},
{
desc: 'parenthesis several more complex - many parent parenthesis',
GREP: '((((((.*)&!((my test)|(his test))&(.*))))))',
regExpected: /(?=.*.*)+(?=.*^(?!.*(my test|his test).*))+(?=.*.*)+.*/i,
cases: [
{ expectMatch: true, testLine: 'test her' },
{ expectMatch: true, testLine: 'her test' },
{ expectMatch: false, testLine: 'her his test' },
{ expectMatch: false, testLine: 'her my test' },
{ expectMatch: false, testLine: 'my test' },
{ expectMatch: false, testLine: 'his test' },
],
},
{
desc: 'tag with dot encoded',
GREP: '@test\\.1',
Expand Down

0 comments on commit 9246e0c

Please sign in to comment.