forked from mysticatea/regexpp
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for ES2025 duplicate named capturing groups
- Loading branch information
Showing
9 changed files
with
2,615 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Holds information for all GroupSpecifiers included in the pattern. | ||
*/ | ||
export interface GroupSpecifiers { | ||
/** | ||
* @returns true if there are no GroupSpecifiers included in the pattern. | ||
*/ | ||
isEmpty: () => boolean | ||
clear: () => void | ||
/** | ||
* Called when visiting the Alternative. | ||
* For ES2025, manage nesting with new Alternative scopes. | ||
*/ | ||
enterAlternative: () => void | ||
/** | ||
* Called when leaving the Alternative. | ||
*/ | ||
leaveAlternative: () => void | ||
/** | ||
* Checks whether the given group name is within the pattern. | ||
*/ | ||
hasInPattern: (name: string) => boolean | ||
/** | ||
* Checks whether the given group name is within the current scope. | ||
*/ | ||
hasInScope: (name: string) => boolean | ||
/** | ||
* Adds the given group name to the current scope. | ||
*/ | ||
addToScope: (name: string) => void | ||
} | ||
|
||
export class GroupSpecifiersAsES2018 implements GroupSpecifiers { | ||
private groupName = new Set<string>() | ||
|
||
public clear(): void { | ||
this.groupName.clear() | ||
} | ||
|
||
public isEmpty(): boolean { | ||
return !this.groupName.size | ||
} | ||
|
||
public hasInPattern(name: string): boolean { | ||
return this.groupName.has(name) | ||
} | ||
|
||
public hasInScope(name: string): boolean { | ||
return this.hasInPattern(name) | ||
} | ||
|
||
public addToScope(name: string): void { | ||
this.groupName.add(name) | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public enterAlternative(): void { | ||
// Prior to ES2025, it does not manage alternative scopes. | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public leaveAlternative(): void { | ||
// Prior to ES2025, it does not manage alternative scopes. | ||
} | ||
} | ||
|
||
export class GroupSpecifiersAsES2025 implements GroupSpecifiers { | ||
private groupNamesInAlternative = new Set<string>() | ||
private upperGroupNamesStack: Set<string>[] = [] | ||
|
||
private groupNamesInPattern = new Set<string>() | ||
|
||
public clear(): void { | ||
this.groupNamesInAlternative.clear() | ||
this.upperGroupNamesStack.length = 0 | ||
this.groupNamesInPattern.clear() | ||
} | ||
|
||
public isEmpty(): boolean { | ||
return !this.groupNamesInPattern.size | ||
} | ||
|
||
public enterAlternative(): void { | ||
this.upperGroupNamesStack.push(this.groupNamesInAlternative) | ||
this.groupNamesInAlternative = new Set(this.groupNamesInAlternative) | ||
} | ||
|
||
public leaveAlternative(): void { | ||
this.groupNamesInAlternative = this.upperGroupNamesStack.pop()! | ||
} | ||
|
||
public hasInPattern(name: string): boolean { | ||
return this.groupNamesInPattern.has(name) | ||
} | ||
|
||
public hasInScope(name: string): boolean { | ||
return this.groupNamesInAlternative.has(name) | ||
} | ||
|
||
public addToScope(name: string): void { | ||
this.groupNamesInAlternative.add(name) | ||
this.groupNamesInPattern.add(name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
test/fixtures/parser/literal/duplicate-named-capturing-group-invalid-2024.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"options": { | ||
"strict": false, | ||
"ecmaVersion": 2024 | ||
}, | ||
"patterns": { | ||
"/(?<year>[0-9]{4})-[0-9]{2}|[0-9]{2}-(?<year>[0-9]{4})/": { | ||
"error": { | ||
"message": "Invalid regular expression: /(?<year>[0-9]{4})-[0-9]{2}|[0-9]{2}-(?<year>[0-9]{4})/: Duplicate capture group name", | ||
"index": 45 | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/fixtures/parser/literal/duplicate-named-capturing-group-invalid-2025.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"options": { | ||
"strict": false, | ||
"ecmaVersion": 2025 | ||
}, | ||
"patterns": { | ||
"/(?<year>[0-9]{4})-(?<year>[0-9]{2})/": { | ||
"error": { | ||
"message": "Invalid regular expression: /(?<year>[0-9]{4})-(?<year>[0-9]{2})/: Duplicate capture group name", | ||
"index": 27 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.