Skip to content

Commit

Permalink
Provide Spelling Suggestions for Named Capture Group References in Re…
Browse files Browse the repository at this point in the history
…gular Expressions (#58613)
  • Loading branch information
graphemecluster authored Jun 4, 2024
1 parent dc1ffb1 commit f5238c3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3568,6 +3568,12 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
forEach(groupNameReferences, reference => {
if (!groupSpecifiers?.has(reference.name)) {
error(Diagnostics.There_is_no_capturing_group_named_0_in_this_regular_expression, reference.pos, reference.end - reference.pos, reference.name);
if (groupSpecifiers) {
const suggestion = getSpellingSuggestion(reference.name, groupSpecifiers, identity);
if (suggestion) {
error(Diagnostics.Did_you_mean_0, reference.pos, reference.end - reference.pos, suggestion);
}
}
}
});
forEach(decimalEscapes, escape => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
regularExpressionGroupNameSuggestions.ts(1,18): error TS1503: Named capturing groups are only available when targeting 'ES2018' or later.
regularExpressionGroupNameSuggestions.ts(1,27): error TS1532: There is no capturing group named 'Foo' in this regular expression.


==== regularExpressionGroupNameSuggestions.ts (2 errors) ====
const regex = /(?<foo>)\k<Foo>/;
~~~~~
!!! error TS1503: Named capturing groups are only available when targeting 'ES2018' or later.
~~~
!!! error TS1532: There is no capturing group named 'Foo' in this regular expression.
!!! related TS1369: Did you mean 'foo'?

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] ////

//// [regularExpressionGroupNameSuggestions.ts]
const regex = /(?<foo>)\k<Foo>/;


//// [regularExpressionGroupNameSuggestions.js]
var regex = /(?<foo>)\k<Foo>/;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] ////

=== regularExpressionGroupNameSuggestions.ts ===
const regex = /(?<foo>)\k<Foo>/;
>regex : Symbol(regex, Decl(regularExpressionGroupNameSuggestions.ts, 0, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] ////

=== regularExpressionGroupNameSuggestions.ts ===
const regex = /(?<foo>)\k<Foo>/;
>regex : RegExp
> : ^^^^^^
>/(?<foo>)\k<Foo>/ : RegExp
> : ^^^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const regex = /(?<foo>)\k<Foo>/;

0 comments on commit f5238c3

Please sign in to comment.