Skip to content

Commit f5238c3

Browse files
Provide Spelling Suggestions for Named Capture Group References in Regular Expressions (#58613)
1 parent dc1ffb1 commit f5238c3

6 files changed

+42
-0
lines changed

src/compiler/scanner.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3568,6 +3568,12 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
35683568
forEach(groupNameReferences, reference => {
35693569
if (!groupSpecifiers?.has(reference.name)) {
35703570
error(Diagnostics.There_is_no_capturing_group_named_0_in_this_regular_expression, reference.pos, reference.end - reference.pos, reference.name);
3571+
if (groupSpecifiers) {
3572+
const suggestion = getSpellingSuggestion(reference.name, groupSpecifiers, identity);
3573+
if (suggestion) {
3574+
error(Diagnostics.Did_you_mean_0, reference.pos, reference.end - reference.pos, suggestion);
3575+
}
3576+
}
35713577
}
35723578
});
35733579
forEach(decimalEscapes, escape => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
regularExpressionGroupNameSuggestions.ts(1,18): error TS1503: Named capturing groups are only available when targeting 'ES2018' or later.
2+
regularExpressionGroupNameSuggestions.ts(1,27): error TS1532: There is no capturing group named 'Foo' in this regular expression.
3+
4+
5+
==== regularExpressionGroupNameSuggestions.ts (2 errors) ====
6+
const regex = /(?<foo>)\k<Foo>/;
7+
~~~~~
8+
!!! error TS1503: Named capturing groups are only available when targeting 'ES2018' or later.
9+
~~~
10+
!!! error TS1532: There is no capturing group named 'Foo' in this regular expression.
11+
!!! related TS1369: Did you mean 'foo'?
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] ////
2+
3+
//// [regularExpressionGroupNameSuggestions.ts]
4+
const regex = /(?<foo>)\k<Foo>/;
5+
6+
7+
//// [regularExpressionGroupNameSuggestions.js]
8+
var regex = /(?<foo>)\k<Foo>/;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] ////
2+
3+
=== regularExpressionGroupNameSuggestions.ts ===
4+
const regex = /(?<foo>)\k<Foo>/;
5+
>regex : Symbol(regex, Decl(regularExpressionGroupNameSuggestions.ts, 0, 5))
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] ////
2+
3+
=== regularExpressionGroupNameSuggestions.ts ===
4+
const regex = /(?<foo>)\k<Foo>/;
5+
>regex : RegExp
6+
> : ^^^^^^
7+
>/(?<foo>)\k<Foo>/ : RegExp
8+
> : ^^^^^^
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const regex = /(?<foo>)\k<Foo>/;

0 commit comments

Comments
 (0)