Skip to content

Commit b780b19

Browse files
authored
Always include spelling suggestions in name errors (#1440)
1 parent 46ccafb commit b780b19

30 files changed

+3011
-427
lines changed

internal/checker/checker.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,6 @@ type Checker struct {
588588
wasCanceled bool
589589
arrayVariances []VarianceFlags
590590
globals ast.SymbolTable
591-
globalSymbols []*ast.Symbol
592591
evaluate evaluator.Evaluator
593592
stringLiteralTypes map[string]*Type
594593
numberLiteralTypes map[jsnum.Number]*Type
@@ -755,7 +754,6 @@ type Checker struct {
755754
typeResolutions []TypeResolution
756755
resolutionStart int
757756
inVarianceComputation bool
758-
suggestionCount int
759757
apparentArgumentCount *int
760758
lastGetCombinedNodeFlagsNode *ast.Node
761759
lastGetCombinedNodeFlagsResult ast.NodeFlags
@@ -1518,27 +1516,22 @@ func (c *Checker) onFailedToResolveSymbol(errorLocation *ast.Node, name string,
15181516
suggestedLib := c.getSuggestedLibForNonExistentName(name)
15191517
if suggestedLib != "" {
15201518
c.error(errorLocation, nameNotFoundMessage, name, suggestedLib)
1521-
c.suggestionCount++
15221519
return
15231520
}
15241521
// Then spelling suggestions
1525-
if c.suggestionCount < 10 {
1526-
suggestion := c.getSuggestedSymbolForNonexistentSymbol(errorLocation, name, meaning)
1527-
if suggestion != nil && !(suggestion.ValueDeclaration != nil && ast.IsAmbientModule(suggestion.ValueDeclaration) && ast.IsGlobalScopeAugmentation(suggestion.ValueDeclaration)) {
1528-
suggestionName := c.symbolToString(suggestion)
1529-
message := core.IfElse(meaning == ast.SymbolFlagsNamespace, diagnostics.Cannot_find_namespace_0_Did_you_mean_1, diagnostics.Cannot_find_name_0_Did_you_mean_1)
1530-
diagnostic := NewDiagnosticForNode(errorLocation, message, name, suggestionName)
1531-
if suggestion.ValueDeclaration != nil {
1532-
diagnostic.AddRelatedInfo(NewDiagnosticForNode(suggestion.ValueDeclaration, diagnostics.X_0_is_declared_here, suggestionName))
1533-
}
1534-
c.diagnostics.Add(diagnostic)
1535-
c.suggestionCount++
1536-
return
1522+
suggestion := c.getSuggestedSymbolForNonexistentSymbol(errorLocation, name, meaning)
1523+
if suggestion != nil && !(suggestion.ValueDeclaration != nil && ast.IsAmbientModule(suggestion.ValueDeclaration) && ast.IsGlobalScopeAugmentation(suggestion.ValueDeclaration)) {
1524+
suggestionName := c.symbolToString(suggestion)
1525+
message := core.IfElse(meaning == ast.SymbolFlagsNamespace, diagnostics.Cannot_find_namespace_0_Did_you_mean_1, diagnostics.Cannot_find_name_0_Did_you_mean_1)
1526+
diagnostic := NewDiagnosticForNode(errorLocation, message, name, suggestionName)
1527+
if suggestion.ValueDeclaration != nil {
1528+
diagnostic.AddRelatedInfo(NewDiagnosticForNode(suggestion.ValueDeclaration, diagnostics.X_0_is_declared_here, suggestionName))
15371529
}
1530+
c.diagnostics.Add(diagnostic)
1531+
return
15381532
}
15391533
// And then fall back to unspecified "not found"
15401534
c.error(errorLocation, nameNotFoundMessage, name)
1541-
c.suggestionCount++
15421535
}
15431536

15441537
func (c *Checker) checkAndReportErrorForUsingTypeAsNamespace(errorLocation *ast.Node, name string, meaning ast.SymbolFlags) bool {

internal/core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func GetSpellingSuggestion[T any](name string, candidates []T, getName func(T) s
476476
}
477477
// Only consider candidates less than 3 characters long when they differ by case.
478478
// Otherwise, don't bother, since a user would usually notice differences of a 2-character name.
479-
if len(candidateName) < 3 && strings.ToLower(candidateName) != strings.ToLower(name) {
479+
if len(candidateName) < 3 && !strings.EqualFold(candidateName, name) {
480480
continue
481481
}
482482
distance := levenshteinWithMax(runeName, []rune(candidateName), bestDistance-0.1)

testdata/baselines/reference/submodule/compiler/commonMissingSemicolons.errors.txt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ commonMissingSemicolons.ts(15,1): error TS2304: Cannot find name 'consd'.
1717
commonMissingSemicolons.ts(15,7): error TS2552: Cannot find name 'myConst2'. Did you mean 'myConst1'?
1818
commonMissingSemicolons.ts(16,1): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
1919
commonMissingSemicolons.ts(16,1): error TS2304: Cannot find name 'constd'.
20-
commonMissingSemicolons.ts(16,8): error TS2304: Cannot find name 'myConst3'.
20+
commonMissingSemicolons.ts(16,8): error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
2121
commonMissingSemicolons.ts(19,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare'?
2222
commonMissingSemicolons.ts(19,1): error TS2304: Cannot find name 'declared'.
2323
commonMissingSemicolons.ts(20,1): error TS2304: Cannot find name 'declare'.
@@ -28,11 +28,11 @@ commonMissingSemicolons.ts(21,1): error TS2304: Cannot find name 'declared'.
2828
commonMissingSemicolons.ts(21,10): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
2929
commonMissingSemicolons.ts(21,10): error TS2304: Cannot find name 'constd'.
3030
commonMissingSemicolons.ts(22,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
31-
commonMissingSemicolons.ts(22,1): error TS2304: Cannot find name 'declareconst'.
32-
commonMissingSemicolons.ts(22,14): error TS2304: Cannot find name 'myDeclareConst5'.
31+
commonMissingSemicolons.ts(22,1): error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
32+
commonMissingSemicolons.ts(22,14): error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
3333
commonMissingSemicolons.ts(25,1): error TS1435: Unknown keyword or identifier. Did you mean 'function'?
34-
commonMissingSemicolons.ts(25,1): error TS2304: Cannot find name 'functiond'.
35-
commonMissingSemicolons.ts(25,11): error TS2304: Cannot find name 'myFunction2'.
34+
commonMissingSemicolons.ts(25,1): error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
35+
commonMissingSemicolons.ts(25,11): error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
3636
commonMissingSemicolons.ts(25,25): error TS1005: ';' expected.
3737
commonMissingSemicolons.ts(26,10): error TS1359: Identifier expected. 'function' is a reserved word that cannot be used here.
3838
commonMissingSemicolons.ts(26,18): error TS1003: Identifier expected.
@@ -48,8 +48,8 @@ commonMissingSemicolons.ts(33,11): error TS2427: Interface name cannot be 'void'
4848
commonMissingSemicolons.ts(34,1): error TS1435: Unknown keyword or identifier. Did you mean 'interface MyInterface'?
4949
commonMissingSemicolons.ts(34,1): error TS2304: Cannot find name 'interfaceMyInterface'.
5050
commonMissingSemicolons.ts(38,1): error TS1435: Unknown keyword or identifier. Did you mean 'let'?
51-
commonMissingSemicolons.ts(38,1): error TS2304: Cannot find name 'letd'.
52-
commonMissingSemicolons.ts(38,6): error TS2304: Cannot find name 'let2'.
51+
commonMissingSemicolons.ts(38,1): error TS2552: Cannot find name 'letd'. Did you mean 'let'?
52+
commonMissingSemicolons.ts(38,6): error TS2552: Cannot find name 'let2'. Did you mean 'let'?
5353
commonMissingSemicolons.ts(39,1): error TS2304: Cannot find name 'letMyLet'.
5454
commonMissingSemicolons.ts(41,10): error TS1005: '=' expected.
5555
commonMissingSemicolons.ts(45,1): error TS1435: Unknown keyword or identifier. Did you mean 'type'?
@@ -62,7 +62,7 @@ commonMissingSemicolons.ts(46,15): error TS2693: 'type' only refers to a type, b
6262
commonMissingSemicolons.ts(47,1): error TS2304: Cannot find name 'typeMyType'.
6363
commonMissingSemicolons.ts(50,1): error TS1435: Unknown keyword or identifier. Did you mean 'var'?
6464
commonMissingSemicolons.ts(50,1): error TS2304: Cannot find name 'vard'.
65-
commonMissingSemicolons.ts(50,6): error TS2304: Cannot find name 'myVar2'.
65+
commonMissingSemicolons.ts(50,6): error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
6666
commonMissingSemicolons.ts(51,1): error TS2304: Cannot find name 'varMyVar'.
6767
commonMissingSemicolons.ts(55,3): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
6868
commonMissingSemicolons.ts(56,1): error TS1128: Declaration or statement expected.
@@ -135,7 +135,8 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
135135
~~~~~~
136136
!!! error TS2304: Cannot find name 'constd'.
137137
~~~~~~~~
138-
!!! error TS2304: Cannot find name 'myConst3'.
138+
!!! error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
139+
!!! related TS2728 commonMissingSemicolons.ts:14:7: 'myConst1' is declared here.
139140

140141
declare const myDeclareConst1: 1;
141142
declared const myDeclareConst2: 1;
@@ -163,18 +164,22 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
163164
~~~~~~~~~~~~
164165
!!! error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
165166
~~~~~~~~~~~~
166-
!!! error TS2304: Cannot find name 'declareconst'.
167+
!!! error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
168+
!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.
167169
~~~~~~~~~~~~~~~
168-
!!! error TS2304: Cannot find name 'myDeclareConst5'.
170+
!!! error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
171+
!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.
169172

170173
function myFunction1() { }
171174
functiond myFunction2() { }
172175
~~~~~~~~~
173176
!!! error TS1435: Unknown keyword or identifier. Did you mean 'function'?
174177
~~~~~~~~~
175-
!!! error TS2304: Cannot find name 'functiond'.
178+
!!! error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
179+
!!! related TS2728 lib.es5.d.ts:--:--: 'Function' is declared here.
176180
~~~~~~~~~~~
177-
!!! error TS2304: Cannot find name 'myFunction2'.
181+
!!! error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
182+
!!! related TS2728 commonMissingSemicolons.ts:24:10: 'myFunction1' is declared here.
178183
~
179184
!!! error TS1005: ';' expected.
180185
function function() { }
@@ -219,9 +224,11 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
219224
~~~~
220225
!!! error TS1435: Unknown keyword or identifier. Did you mean 'let'?
221226
~~~~
222-
!!! error TS2304: Cannot find name 'letd'.
227+
!!! error TS2552: Cannot find name 'letd'. Did you mean 'let'?
228+
!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
223229
~~~~
224-
!!! error TS2304: Cannot find name 'let2'.
230+
!!! error TS2552: Cannot find name 'let2'. Did you mean 'let'?
231+
!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
225232
letMyLet;
226233
~~~~~~~~
227234
!!! error TS2304: Cannot find name 'letMyLet'.
@@ -259,7 +266,8 @@ commonMissingSemicolons.ts(78,1): error TS1128: Declaration or statement expecte
259266
~~~~
260267
!!! error TS2304: Cannot find name 'vard'.
261268
~~~~~~
262-
!!! error TS2304: Cannot find name 'myVar2'.
269+
!!! error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
270+
!!! related TS2728 commonMissingSemicolons.ts:49:5: 'myVar1' is declared here.
263271
varMyVar;
264272
~~~~~~~~
265273
!!! error TS2304: Cannot find name 'varMyVar'.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
--- old.commonMissingSemicolons.errors.txt
2+
+++ new.commonMissingSemicolons.errors.txt
3+
@@= skipped -16, +16 lines =@@
4+
commonMissingSemicolons.ts(15,7): error TS2552: Cannot find name 'myConst2'. Did you mean 'myConst1'?
5+
commonMissingSemicolons.ts(16,1): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
6+
commonMissingSemicolons.ts(16,1): error TS2304: Cannot find name 'constd'.
7+
-commonMissingSemicolons.ts(16,8): error TS2304: Cannot find name 'myConst3'.
8+
+commonMissingSemicolons.ts(16,8): error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
9+
commonMissingSemicolons.ts(19,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare'?
10+
commonMissingSemicolons.ts(19,1): error TS2304: Cannot find name 'declared'.
11+
commonMissingSemicolons.ts(20,1): error TS2304: Cannot find name 'declare'.
12+
@@= skipped -11, +11 lines =@@
13+
commonMissingSemicolons.ts(21,10): error TS1435: Unknown keyword or identifier. Did you mean 'const'?
14+
commonMissingSemicolons.ts(21,10): error TS2304: Cannot find name 'constd'.
15+
commonMissingSemicolons.ts(22,1): error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
16+
-commonMissingSemicolons.ts(22,1): error TS2304: Cannot find name 'declareconst'.
17+
-commonMissingSemicolons.ts(22,14): error TS2304: Cannot find name 'myDeclareConst5'.
18+
+commonMissingSemicolons.ts(22,1): error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
19+
+commonMissingSemicolons.ts(22,14): error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
20+
commonMissingSemicolons.ts(25,1): error TS1435: Unknown keyword or identifier. Did you mean 'function'?
21+
-commonMissingSemicolons.ts(25,1): error TS2304: Cannot find name 'functiond'.
22+
-commonMissingSemicolons.ts(25,11): error TS2304: Cannot find name 'myFunction2'.
23+
+commonMissingSemicolons.ts(25,1): error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
24+
+commonMissingSemicolons.ts(25,11): error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
25+
commonMissingSemicolons.ts(25,25): error TS1005: ';' expected.
26+
commonMissingSemicolons.ts(26,10): error TS1359: Identifier expected. 'function' is a reserved word that cannot be used here.
27+
commonMissingSemicolons.ts(26,18): error TS1003: Identifier expected.
28+
@@= skipped -20, +20 lines =@@
29+
commonMissingSemicolons.ts(34,1): error TS1435: Unknown keyword or identifier. Did you mean 'interface MyInterface'?
30+
commonMissingSemicolons.ts(34,1): error TS2304: Cannot find name 'interfaceMyInterface'.
31+
commonMissingSemicolons.ts(38,1): error TS1435: Unknown keyword or identifier. Did you mean 'let'?
32+
-commonMissingSemicolons.ts(38,1): error TS2304: Cannot find name 'letd'.
33+
-commonMissingSemicolons.ts(38,6): error TS2304: Cannot find name 'let2'.
34+
+commonMissingSemicolons.ts(38,1): error TS2552: Cannot find name 'letd'. Did you mean 'let'?
35+
+commonMissingSemicolons.ts(38,6): error TS2552: Cannot find name 'let2'. Did you mean 'let'?
36+
commonMissingSemicolons.ts(39,1): error TS2304: Cannot find name 'letMyLet'.
37+
commonMissingSemicolons.ts(41,10): error TS1005: '=' expected.
38+
commonMissingSemicolons.ts(45,1): error TS1435: Unknown keyword or identifier. Did you mean 'type'?
39+
@@= skipped -14, +14 lines =@@
40+
commonMissingSemicolons.ts(47,1): error TS2304: Cannot find name 'typeMyType'.
41+
commonMissingSemicolons.ts(50,1): error TS1435: Unknown keyword or identifier. Did you mean 'var'?
42+
commonMissingSemicolons.ts(50,1): error TS2304: Cannot find name 'vard'.
43+
-commonMissingSemicolons.ts(50,6): error TS2304: Cannot find name 'myVar2'.
44+
+commonMissingSemicolons.ts(50,6): error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
45+
commonMissingSemicolons.ts(51,1): error TS2304: Cannot find name 'varMyVar'.
46+
commonMissingSemicolons.ts(55,3): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
47+
commonMissingSemicolons.ts(56,1): error TS1128: Declaration or statement expected.
48+
@@= skipped -73, +73 lines =@@
49+
~~~~~~
50+
!!! error TS2304: Cannot find name 'constd'.
51+
~~~~~~~~
52+
-!!! error TS2304: Cannot find name 'myConst3'.
53+
+!!! error TS2552: Cannot find name 'myConst3'. Did you mean 'myConst1'?
54+
+!!! related TS2728 commonMissingSemicolons.ts:14:7: 'myConst1' is declared here.
55+
56+
declare const myDeclareConst1: 1;
57+
declared const myDeclareConst2: 1;
58+
@@= skipped -28, +29 lines =@@
59+
~~~~~~~~~~~~
60+
!!! error TS1435: Unknown keyword or identifier. Did you mean 'declare const'?
61+
~~~~~~~~~~~~
62+
-!!! error TS2304: Cannot find name 'declareconst'.
63+
+!!! error TS2552: Cannot find name 'declareconst'. Did you mean 'myDeclareConst1'?
64+
+!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.
65+
~~~~~~~~~~~~~~~
66+
-!!! error TS2304: Cannot find name 'myDeclareConst5'.
67+
+!!! error TS2552: Cannot find name 'myDeclareConst5'. Did you mean 'myDeclareConst1'?
68+
+!!! related TS2728 commonMissingSemicolons.ts:18:15: 'myDeclareConst1' is declared here.
69+
70+
function myFunction1() { }
71+
functiond myFunction2() { }
72+
~~~~~~~~~
73+
!!! error TS1435: Unknown keyword or identifier. Did you mean 'function'?
74+
~~~~~~~~~
75+
-!!! error TS2304: Cannot find name 'functiond'.
76+
+!!! error TS2552: Cannot find name 'functiond'. Did you mean 'Function'?
77+
+!!! related TS2728 lib.es5.d.ts:--:--: 'Function' is declared here.
78+
~~~~~~~~~~~
79+
-!!! error TS2304: Cannot find name 'myFunction2'.
80+
+!!! error TS2552: Cannot find name 'myFunction2'. Did you mean 'myFunction1'?
81+
+!!! related TS2728 commonMissingSemicolons.ts:24:10: 'myFunction1' is declared here.
82+
~
83+
!!! error TS1005: ';' expected.
84+
function function() { }
85+
@@= skipped -56, +60 lines =@@
86+
~~~~
87+
!!! error TS1435: Unknown keyword or identifier. Did you mean 'let'?
88+
~~~~
89+
-!!! error TS2304: Cannot find name 'letd'.
90+
+!!! error TS2552: Cannot find name 'letd'. Did you mean 'let'?
91+
+!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
92+
~~~~
93+
-!!! error TS2304: Cannot find name 'let2'.
94+
+!!! error TS2552: Cannot find name 'let2'. Did you mean 'let'?
95+
+!!! related TS2728 commonMissingSemicolons.ts:36:5: 'let' is declared here.
96+
letMyLet;
97+
~~~~~~~~
98+
!!! error TS2304: Cannot find name 'letMyLet'.
99+
@@= skipped -40, +42 lines =@@
100+
~~~~
101+
!!! error TS2304: Cannot find name 'vard'.
102+
~~~~~~
103+
-!!! error TS2304: Cannot find name 'myVar2'.
104+
+!!! error TS2552: Cannot find name 'myVar2'. Did you mean 'myVar1'?
105+
+!!! related TS2728 commonMissingSemicolons.ts:49:5: 'myVar1' is declared here.
106+
varMyVar;
107+
~~~~~~~~
108+
!!! error TS2304: Cannot find name 'varMyVar'.

testdata/baselines/reference/submodule/compiler/constructorWithIncompleteTypeAnnotation.errors.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ constructorWithIncompleteTypeAnnotation.ts(227,13): error TS1109: Expression exp
5252
constructorWithIncompleteTypeAnnotation.ts(234,14): error TS1005: '{' expected.
5353
constructorWithIncompleteTypeAnnotation.ts(235,9): error TS1128: Declaration or statement expected.
5454
constructorWithIncompleteTypeAnnotation.ts(235,16): error TS2304: Cannot find name 'method1'.
55-
constructorWithIncompleteTypeAnnotation.ts(235,24): error TS2304: Cannot find name 'val'.
55+
constructorWithIncompleteTypeAnnotation.ts(235,24): error TS2552: Cannot find name 'val'. Did you mean 'eval'?
5656
constructorWithIncompleteTypeAnnotation.ts(235,27): error TS1005: ',' expected.
5757
constructorWithIncompleteTypeAnnotation.ts(235,28): error TS2693: 'number' only refers to a type, but is being used as a value here.
5858
constructorWithIncompleteTypeAnnotation.ts(235,36): error TS1005: ';' expected.
@@ -439,7 +439,8 @@ constructorWithIncompleteTypeAnnotation.ts(261,1): error TS1128: Declaration or
439439
~~~~~~~
440440
!!! error TS2304: Cannot find name 'method1'.
441441
~~~
442-
!!! error TS2304: Cannot find name 'val'.
442+
!!! error TS2552: Cannot find name 'val'. Did you mean 'eval'?
443+
!!! related TS2728 lib.es5.d.ts:--:--: 'eval' is declared here.
443444
~
444445
!!! error TS1005: ',' expected.
445446
~~~~~~

0 commit comments

Comments
 (0)