From f5238c328e553df2c0af868173dfd3789d77205f Mon Sep 17 00:00:00 2001 From: graphemecluster Date: Wed, 5 Jun 2024 04:47:58 +0800 Subject: [PATCH] Provide Spelling Suggestions for Named Capture Group References in Regular Expressions (#58613) --- src/compiler/scanner.ts | 6 ++++++ .../regularExpressionGroupNameSuggestions.errors.txt | 12 ++++++++++++ .../regularExpressionGroupNameSuggestions.js | 8 ++++++++ .../regularExpressionGroupNameSuggestions.symbols | 6 ++++++ .../regularExpressionGroupNameSuggestions.types | 9 +++++++++ .../regularExpressionGroupNameSuggestions.ts | 1 + 6 files changed, 42 insertions(+) create mode 100644 tests/baselines/reference/regularExpressionGroupNameSuggestions.errors.txt create mode 100644 tests/baselines/reference/regularExpressionGroupNameSuggestions.js create mode 100644 tests/baselines/reference/regularExpressionGroupNameSuggestions.symbols create mode 100644 tests/baselines/reference/regularExpressionGroupNameSuggestions.types create mode 100644 tests/cases/compiler/regularExpressionGroupNameSuggestions.ts diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index f30287cf0b0..ad5f270bd4d 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -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 => { diff --git a/tests/baselines/reference/regularExpressionGroupNameSuggestions.errors.txt b/tests/baselines/reference/regularExpressionGroupNameSuggestions.errors.txt new file mode 100644 index 00000000000..b900c35fe69 --- /dev/null +++ b/tests/baselines/reference/regularExpressionGroupNameSuggestions.errors.txt @@ -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 = /(?)\k/; + ~~~~~ +!!! 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'? + \ No newline at end of file diff --git a/tests/baselines/reference/regularExpressionGroupNameSuggestions.js b/tests/baselines/reference/regularExpressionGroupNameSuggestions.js new file mode 100644 index 00000000000..806a861a740 --- /dev/null +++ b/tests/baselines/reference/regularExpressionGroupNameSuggestions.js @@ -0,0 +1,8 @@ +//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] //// + +//// [regularExpressionGroupNameSuggestions.ts] +const regex = /(?)\k/; + + +//// [regularExpressionGroupNameSuggestions.js] +var regex = /(?)\k/; diff --git a/tests/baselines/reference/regularExpressionGroupNameSuggestions.symbols b/tests/baselines/reference/regularExpressionGroupNameSuggestions.symbols new file mode 100644 index 00000000000..2b4d2d7407e --- /dev/null +++ b/tests/baselines/reference/regularExpressionGroupNameSuggestions.symbols @@ -0,0 +1,6 @@ +//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] //// + +=== regularExpressionGroupNameSuggestions.ts === +const regex = /(?)\k/; +>regex : Symbol(regex, Decl(regularExpressionGroupNameSuggestions.ts, 0, 5)) + diff --git a/tests/baselines/reference/regularExpressionGroupNameSuggestions.types b/tests/baselines/reference/regularExpressionGroupNameSuggestions.types new file mode 100644 index 00000000000..a9d110f6ddf --- /dev/null +++ b/tests/baselines/reference/regularExpressionGroupNameSuggestions.types @@ -0,0 +1,9 @@ +//// [tests/cases/compiler/regularExpressionGroupNameSuggestions.ts] //// + +=== regularExpressionGroupNameSuggestions.ts === +const regex = /(?)\k/; +>regex : RegExp +> : ^^^^^^ +>/(?)\k/ : RegExp +> : ^^^^^^ + diff --git a/tests/cases/compiler/regularExpressionGroupNameSuggestions.ts b/tests/cases/compiler/regularExpressionGroupNameSuggestions.ts new file mode 100644 index 00000000000..f1960c6cbcb --- /dev/null +++ b/tests/cases/compiler/regularExpressionGroupNameSuggestions.ts @@ -0,0 +1 @@ +const regex = /(?)\k/;