Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Spelling Suggestions for Named Capture Group References in Regular Expressions #58613

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3478,6 +3478,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>/;