Skip to content

Always include spelling suggestions in name errors #1440

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

Merged
merged 2 commits into from
Jul 23, 2025
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
25 changes: 9 additions & 16 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ type Checker struct {
wasCanceled bool
arrayVariances []VarianceFlags
globals ast.SymbolTable
globalSymbols []*ast.Symbol
evaluate evaluator.Evaluator
stringLiteralTypes map[string]*Type
numberLiteralTypes map[jsnum.Number]*Type
Expand Down Expand Up @@ -755,7 +754,6 @@ type Checker struct {
typeResolutions []TypeResolution
resolutionStart int
inVarianceComputation bool
suggestionCount int
apparentArgumentCount *int
lastGetCombinedNodeFlagsNode *ast.Node
lastGetCombinedNodeFlagsResult ast.NodeFlags
Expand Down Expand Up @@ -1518,27 +1516,22 @@ func (c *Checker) onFailedToResolveSymbol(errorLocation *ast.Node, name string,
suggestedLib := c.getSuggestedLibForNonExistentName(name)
if suggestedLib != "" {
c.error(errorLocation, nameNotFoundMessage, name, suggestedLib)
c.suggestionCount++
return
}
// Then spelling suggestions
if c.suggestionCount < 10 {
suggestion := c.getSuggestedSymbolForNonexistentSymbol(errorLocation, name, meaning)
if suggestion != nil && !(suggestion.ValueDeclaration != nil && ast.IsAmbientModule(suggestion.ValueDeclaration) && ast.IsGlobalScopeAugmentation(suggestion.ValueDeclaration)) {
suggestionName := c.symbolToString(suggestion)
message := core.IfElse(meaning == ast.SymbolFlagsNamespace, diagnostics.Cannot_find_namespace_0_Did_you_mean_1, diagnostics.Cannot_find_name_0_Did_you_mean_1)
diagnostic := NewDiagnosticForNode(errorLocation, message, name, suggestionName)
if suggestion.ValueDeclaration != nil {
diagnostic.AddRelatedInfo(NewDiagnosticForNode(suggestion.ValueDeclaration, diagnostics.X_0_is_declared_here, suggestionName))
}
c.diagnostics.Add(diagnostic)
c.suggestionCount++
return
suggestion := c.getSuggestedSymbolForNonexistentSymbol(errorLocation, name, meaning)
if suggestion != nil && !(suggestion.ValueDeclaration != nil && ast.IsAmbientModule(suggestion.ValueDeclaration) && ast.IsGlobalScopeAugmentation(suggestion.ValueDeclaration)) {
suggestionName := c.symbolToString(suggestion)
message := core.IfElse(meaning == ast.SymbolFlagsNamespace, diagnostics.Cannot_find_namespace_0_Did_you_mean_1, diagnostics.Cannot_find_name_0_Did_you_mean_1)
diagnostic := NewDiagnosticForNode(errorLocation, message, name, suggestionName)
if suggestion.ValueDeclaration != nil {
diagnostic.AddRelatedInfo(NewDiagnosticForNode(suggestion.ValueDeclaration, diagnostics.X_0_is_declared_here, suggestionName))
}
c.diagnostics.Add(diagnostic)
return
}
// And then fall back to unspecified "not found"
c.error(errorLocation, nameNotFoundMessage, name)
c.suggestionCount++
}

func (c *Checker) checkAndReportErrorForUsingTypeAsNamespace(errorLocation *ast.Node, name string, meaning ast.SymbolFlags) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func GetSpellingSuggestion[T any](name string, candidates []T, getName func(T) s
}
// Only consider candidates less than 3 characters long when they differ by case.
// Otherwise, don't bother, since a user would usually notice differences of a 2-character name.
if len(candidateName) < 3 && strings.ToLower(candidateName) != strings.ToLower(name) {
if len(candidateName) < 3 && !strings.EqualFold(candidateName, name) {
continue
}
distance := levenshteinWithMax(runeName, []rune(candidateName), bestDistance-0.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ commonMissingSemicolons.ts(15,1): error TS2304: Cannot find name 'consd'.
commonMissingSemicolons.ts(15,7): error TS2552: Cannot find name 'myConst2'. Did you mean 'myConst1'?
commonMissingSemicolons.ts(16,1): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
commonMissingSemicolons.ts(16,1): error TS2304: Cannot find name 'constd'.
commonMissingSemicolons.ts(16,8): error TS2304: Cannot find name 'myConst3'.
commonMissingSemicolons.ts(16,8): error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
commonMissingSemicolons.ts(19,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare'?
commonMissingSemicolons.ts(19,1): error TS2304: Cannot find name 'declared'.
commonMissingSemicolons.ts(20,1): error TS2304: Cannot find name 'declare'.
Expand All @@ -28,11 +28,11 @@ commonMissingSemicolons.ts(21,1): error TS2304: Cannot find name 'declared'.
commonMissingSemicolons.ts(21,10): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
commonMissingSemicolons.ts(21,10): error TS2304: Cannot find name 'constd'.
commonMissingSemicolons.ts(22,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
commonMissingSemicolons.ts(22,1): error TS2304: Cannot find name 'declareconst'.
commonMissingSemicolons.ts(22,14): error TS2304: Cannot find name 'myDeclareConst5'.
commonMissingSemicolons.ts(22,1): error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
commonMissingSemicolons.ts(22,14): error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
commonMissingSemicolons.ts(25,1): error TS1435: Unknown keyword or identifier. Did you mean 'function'?
commonMissingSemicolons.ts(25,1): error TS2304: Cannot find name 'functiond'.
commonMissingSemicolons.ts(25,11): error TS2304: Cannot find name 'myFunction2'.
commonMissingSemicolons.ts(25,1): error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
commonMissingSemicolons.ts(25,11): error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
commonMissingSemicolons.ts(25,25): error TS1005: ';' expected.
commonMissingSemicolons.ts(26,10): error TS1359: Identifier expected. 'function' is a reserved word that cannot be used here.
commonMissingSemicolons.ts(26,18): error TS1003: Identifier expected.
Expand All @@ -48,8 +48,8 @@ commonMissingSemicolons.ts(33,11): error TS2427: Interface name cannot be 'void'
commonMissingSemicolons.ts(34,1): error TS1435: Unknown keyword or identifier. Did you mean 'interface MyInterface'?
commonMissingSemicolons.ts(34,1): error TS2304: Cannot find name 'interfaceMyInterface'.
commonMissingSemicolons.ts(38,1): error TS1435: Unknown keyword or identifier. Did you mean 'let'?
commonMissingSemicolons.ts(38,1): error TS2304: Cannot find name 'letd'.
commonMissingSemicolons.ts(38,6): error TS2304: Cannot find name 'let2'.
commonMissingSemicolons.ts(38,1): error TS2552: Cannot find name 'letd'. Did you mean 'let'?
commonMissingSemicolons.ts(38,6): error TS2552: Cannot find name 'let2'. Did you mean 'let'?
commonMissingSemicolons.ts(39,1): error TS2304: Cannot find name 'letMyLet'.
commonMissingSemicolons.ts(41,10): error TS1005: '=' expected.
commonMissingSemicolons.ts(45,1): error TS1435: Unknown keyword or identifier. Did you mean 'type'?
Expand All @@ -62,7 +62,7 @@ commonMissingSemicolons.ts(46,15): error TS2693: 'type' only refers to a type, b
commonMissingSemicolons.ts(47,1): error TS2304: Cannot find name 'typeMyType'.
commonMissingSemicolons.ts(50,1): error TS1435: Unknown keyword or identifier. Did you mean 'var'?
commonMissingSemicolons.ts(50,1): error TS2304: Cannot find name 'vard'.
commonMissingSemicolons.ts(50,6): error TS2304: Cannot find name 'myVar2'.
commonMissingSemicolons.ts(50,6): error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
commonMissingSemicolons.ts(51,1): error TS2304: Cannot find name 'varMyVar'.
commonMissingSemicolons.ts(55,3): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
commonMissingSemicolons.ts(56,1): error TS1128: Declaration or statement expected.
Expand Down Expand Up @@ -135,7 +135,8 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
~~~~~~
!!! error TS2304: Cannot find name 'constd'.
~~~~~~~~
!!! error TS2304: Cannot find name 'myConst3'.
!!! error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
!!! related TS2728 commonMissingSemicolons.ts:14:7: 'myConst1' is declared here.

declare const myDeclareConst1: 1;
declared const myDeclareConst2: 1;
Expand Down Expand Up @@ -163,18 +164,22 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
~~~~~~~~~~~~
!!! error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'declareconst'.
!!! error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.
~~~~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'myDeclareConst5'.
!!! error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.

function myFunction1() { }
functiond myFunction2() { }
~~~~~~~~~
!!! error TS1435: Unknown keyword or identifier. Did you mean 'function'?
~~~~~~~~~
!!! error TS2304: Cannot find name 'functiond'.
!!! error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
!!! related TS2728 lib.es5.d.ts:--:--: 'Function' is declared here.
~~~~~~~~~~~
!!! error TS2304: Cannot find name 'myFunction2'.
!!! error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
!!! related TS2728 commonMissingSemicolons.ts:24:10: 'myFunction1' is declared here.
~
!!! error TS1005: ';' expected.
function function() { }
Expand Down Expand Up @@ -219,9 +224,11 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
~~~~
!!! error TS1435: Unknown keyword or identifier. Did you mean 'let'?
~~~~
!!! error TS2304: Cannot find name 'letd'.
!!! error TS2552: Cannot find name 'letd'. Did you mean 'let'?
!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
~~~~
!!! error TS2304: Cannot find name 'let2'.
!!! error TS2552: Cannot find name 'let2'. Did you mean 'let'?
!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
letMyLet;
~~~~~~~~
!!! error TS2304: Cannot find name 'letMyLet'.
Expand Down Expand Up @@ -259,7 +266,8 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
~~~~
!!! error TS2304: Cannot find name 'vard'.
~~~~~~
!!! error TS2304: Cannot find name 'myVar2'.
!!! error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
!!! related TS2728 commonMissingSemicolons.ts:49:5: 'myVar1' is declared here.
varMyVar;
~~~~~~~~
!!! error TS2304: Cannot find name 'varMyVar'.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
--- old.commonMissingSemicolons.errors.txt
+++ new.commonMissingSemicolons.errors.txt
@@= skipped -16, +16 lines =@@
commonMissingSemicolons.ts(15,7): error TS2552: Cannot find name 'myConst2'. Did you mean 'myConst1'?
commonMissingSemicolons.ts(16,1): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
commonMissingSemicolons.ts(16,1): error TS2304: Cannot find name 'constd'.
-commonMissingSemicolons.ts(16,8): error TS2304: Cannot find name 'myConst3'.
+commonMissingSemicolons.ts(16,8): error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
commonMissingSemicolons.ts(19,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare'?
commonMissingSemicolons.ts(19,1): error TS2304: Cannot find name 'declared'.
commonMissingSemicolons.ts(20,1): error TS2304: Cannot find name 'declare'.
@@= skipped -11, +11 lines =@@
commonMissingSemicolons.ts(21,10): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
commonMissingSemicolons.ts(21,10): error TS2304: Cannot find name 'constd'.
commonMissingSemicolons.ts(22,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
-commonMissingSemicolons.ts(22,1): error TS2304: Cannot find name 'declareconst'.
-commonMissingSemicolons.ts(22,14): error TS2304: Cannot find name 'myDeclareConst5'.
+commonMissingSemicolons.ts(22,1): error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
+commonMissingSemicolons.ts(22,14): error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
commonMissingSemicolons.ts(25,1): error TS1435: Unknown keyword or identifier. Did you mean 'function'?
-commonMissingSemicolons.ts(25,1): error TS2304: Cannot find name 'functiond'.
-commonMissingSemicolons.ts(25,11): error TS2304: Cannot find name 'myFunction2'.
+commonMissingSemicolons.ts(25,1): error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
+commonMissingSemicolons.ts(25,11): error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
commonMissingSemicolons.ts(25,25): error TS1005: ';' expected.
commonMissingSemicolons.ts(26,10): error TS1359: Identifier expected. 'function' is a reserved word that cannot be used here.
commonMissingSemicolons.ts(26,18): error TS1003: Identifier expected.
@@= skipped -20, +20 lines =@@
commonMissingSemicolons.ts(34,1): error TS1435: Unknown keyword or identifier. Did you mean 'interface MyInterface'?
commonMissingSemicolons.ts(34,1): error TS2304: Cannot find name 'interfaceMyInterface'.
commonMissingSemicolons.ts(38,1): error TS1435: Unknown keyword or identifier. Did you mean 'let'?
-commonMissingSemicolons.ts(38,1): error TS2304: Cannot find name 'letd'.
-commonMissingSemicolons.ts(38,6): error TS2304: Cannot find name 'let2'.
+commonMissingSemicolons.ts(38,1): error TS2552: Cannot find name 'letd'. Did you mean 'let'?
+commonMissingSemicolons.ts(38,6): error TS2552: Cannot find name 'let2'. Did you mean 'let'?
commonMissingSemicolons.ts(39,1): error TS2304: Cannot find name 'letMyLet'.
commonMissingSemicolons.ts(41,10): error TS1005: '=' expected.
commonMissingSemicolons.ts(45,1): error TS1435: Unknown keyword or identifier. Did you mean 'type'?
@@= skipped -14, +14 lines =@@
commonMissingSemicolons.ts(47,1): error TS2304: Cannot find name 'typeMyType'.
commonMissingSemicolons.ts(50,1): error TS1435: Unknown keyword or identifier. Did you mean 'var'?
commonMissingSemicolons.ts(50,1): error TS2304: Cannot find name 'vard'.
-commonMissingSemicolons.ts(50,6): error TS2304: Cannot find name 'myVar2'.
+commonMissingSemicolons.ts(50,6): error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
commonMissingSemicolons.ts(51,1): error TS2304: Cannot find name 'varMyVar'.
commonMissingSemicolons.ts(55,3): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
commonMissingSemicolons.ts(56,1): error TS1128: Declaration or statement expected.
@@= skipped -73, +73 lines =@@
~~~~~~
!!! error TS2304: Cannot find name 'constd'.
~~~~~~~~
-!!! error TS2304: Cannot find name 'myConst3'.
+!!! error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
+!!! related TS2728 commonMissingSemicolons.ts:14:7: 'myConst1' is declared here.

declare const myDeclareConst1: 1;
declared const myDeclareConst2: 1;
@@= skipped -28, +29 lines =@@
~~~~~~~~~~~~
!!! error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
~~~~~~~~~~~~
-!!! error TS2304: Cannot find name 'declareconst'.
+!!! error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
+!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.
~~~~~~~~~~~~~~~
-!!! error TS2304: Cannot find name 'myDeclareConst5'.
+!!! error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
+!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.

function myFunction1() { }
functiond myFunction2() { }
~~~~~~~~~
!!! error TS1435: Unknown keyword or identifier. Did you mean 'function'?
~~~~~~~~~
-!!! error TS2304: Cannot find name 'functiond'.
+!!! error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
+!!! related TS2728 lib.es5.d.ts:--:--: 'Function' is declared here.
~~~~~~~~~~~
-!!! error TS2304: Cannot find name 'myFunction2'.
+!!! error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
+!!! related TS2728 commonMissingSemicolons.ts:24:10: 'myFunction1' is declared here.
~
!!! error TS1005: ';' expected.
function function() { }
@@= skipped -56, +60 lines =@@
~~~~
!!! error TS1435: Unknown keyword or identifier. Did you mean 'let'?
~~~~
-!!! error TS2304: Cannot find name 'letd'.
+!!! error TS2552: Cannot find name 'letd'. Did you mean 'let'?
+!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
~~~~
-!!! error TS2304: Cannot find name 'let2'.
+!!! error TS2552: Cannot find name 'let2'. Did you mean 'let'?
+!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
letMyLet;
~~~~~~~~
!!! error TS2304: Cannot find name 'letMyLet'.
@@= skipped -40, +42 lines =@@
~~~~
!!! error TS2304: Cannot find name 'vard'.
~~~~~~
-!!! error TS2304: Cannot find name 'myVar2'.
+!!! error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
+!!! related TS2728 commonMissingSemicolons.ts:49:5: 'myVar1' is declared here.
varMyVar;
~~~~~~~~
!!! error TS2304: Cannot find name 'varMyVar'.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ constructorWithIncompleteTypeAnnotation.ts(227,13): error TS1109: Expression exp
constructorWithIncompleteTypeAnnotation.ts(234,14): error TS1005: '{' expected.
constructorWithIncompleteTypeAnnotation.ts(235,9): error TS1128: Declaration or statement expected.
constructorWithIncompleteTypeAnnotation.ts(235,16): error TS2304: Cannot find name 'method1'.
constructorWithIncompleteTypeAnnotation.ts(235,24): error TS2304: Cannot find name 'val'.
constructorWithIncompleteTypeAnnotation.ts(235,24): error TS2552: Cannot find name 'val'. Did you mean 'eval'?
constructorWithIncompleteTypeAnnotation.ts(235,27): error TS1005: ',' expected.
constructorWithIncompleteTypeAnnotation.ts(235,28): error TS2693: 'number' only refers to a type, but is being used as a value here.
constructorWithIncompleteTypeAnnotation.ts(235,36): error TS1005: ';' expected.
Expand Down Expand Up @@ -439,7 +439,8 @@ constructorWithIncompleteTypeAnnotation.ts(261,1): error TS1128: Declaration or
~~~~~~~
!!! error TS2304: Cannot find name 'method1'.
~~~
!!! error TS2304: Cannot find name 'val'.
!!! error TS2552: Cannot find name 'val'. Did you mean 'eval'?
!!! related TS2728 lib.es5.d.ts:--:--: 'eval' is declared here.
~
!!! error TS1005: ',' expected.
~~~~~~
Expand Down
Loading
Loading