6
6
7
7
lib "github.com/nedpals/errgoengine"
8
8
"github.com/nedpals/errgoengine/utils/levenshtein"
9
+ "github.com/nedpals/errgoengine/utils/slice"
9
10
sitter "github.com/smacker/go-tree-sitter"
10
11
)
11
12
@@ -14,6 +15,7 @@ type identifiedExpectedReasonKind int
14
15
const (
15
16
identifierExpectedReasonUnknown identifiedExpectedReasonKind = 0
16
17
identifierExpectedReasonClassInterfaceEnum identifiedExpectedReasonKind = iota
18
+ identifierExpectedReasonTypo identifiedExpectedReasonKind = iota
17
19
)
18
20
19
21
type identifierExpectedFixKind int
@@ -48,8 +50,11 @@ var IdentifierExpectedError = lib.ErrorTemplate{
48
50
49
51
// TODO: check if node is parsable
50
52
if iCtx .reasonKind == identifierExpectedReasonClassInterfaceEnum {
53
+ accessTokens := []string {"public" }
54
+ statementTokens := []string {"class" , "interface" , "enum" }
55
+
51
56
// use levenstein distance to check if the word is a typo
52
- tokens := [] string { "class" , "interface" , "enum" }
57
+ tokens := append ( accessTokens , statementTokens ... )
53
58
54
59
// get the nearest word
55
60
nearestWord := ""
@@ -103,6 +108,11 @@ var IdentifierExpectedError = lib.ErrorTemplate{
103
108
} else {
104
109
m .Nearest = initialNearest
105
110
}
111
+
112
+ // if nearestword is not a statement token, then it's a typo
113
+ if ! slice .ContainsString (statementTokens , nearestWord ) {
114
+ iCtx .reasonKind = identifierExpectedReasonTypo
115
+ }
106
116
}
107
117
} else if tree , err := sitter .ParseCtx (
108
118
context .Background (),
@@ -120,6 +130,8 @@ var IdentifierExpectedError = lib.ErrorTemplate{
120
130
switch iCtx .reasonKind {
121
131
case identifierExpectedReasonClassInterfaceEnum :
122
132
gen .Add ("This error occurs when there's a typo or the keyword `class`, `interface`, or `enum` is missing." )
133
+ case identifierExpectedReasonTypo :
134
+ gen .Add ("This error indicates there's a typo or misspelled word in your code." )
123
135
default :
124
136
gen .Add ("This error occurs when an identifier is expected, but an expression is found in a location where a statement or declaration is expected." )
125
137
}
@@ -152,7 +164,7 @@ var IdentifierExpectedError = lib.ErrorTemplate{
152
164
})
153
165
case identifierExpectedCorrectTypo :
154
166
gen .Add ("Correct the typo" , func (s * lib.BugFixSuggestion ) {
155
- s .AddStep ("Change `%s` to `%s` to properly declare the %s ." , ctx .typoWord , ctx . wordForTypo , ctx .wordForTypo ).
167
+ s .AddStep ("Change `%s` to `%s`." , ctx .typoWord , ctx .wordForTypo ).
156
168
AddFix (lib.FixSuggestion {
157
169
NewText : ctx .wordForTypo ,
158
170
StartPosition : cd .MainError .Nearest .StartPosition (),
0 commit comments