Skip to content

Commit 5f2d310

Browse files
committed
fix(error_templates/java): include access modifiers in typo
1 parent 93cc92e commit 5f2d310

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

error_templates/java/identifier_expected_error.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
lib "github.com/nedpals/errgoengine"
88
"github.com/nedpals/errgoengine/utils/levenshtein"
9+
"github.com/nedpals/errgoengine/utils/slice"
910
sitter "github.com/smacker/go-tree-sitter"
1011
)
1112

@@ -14,6 +15,7 @@ type identifiedExpectedReasonKind int
1415
const (
1516
identifierExpectedReasonUnknown identifiedExpectedReasonKind = 0
1617
identifierExpectedReasonClassInterfaceEnum identifiedExpectedReasonKind = iota
18+
identifierExpectedReasonTypo identifiedExpectedReasonKind = iota
1719
)
1820

1921
type identifierExpectedFixKind int
@@ -48,8 +50,11 @@ var IdentifierExpectedError = lib.ErrorTemplate{
4850

4951
// TODO: check if node is parsable
5052
if iCtx.reasonKind == identifierExpectedReasonClassInterfaceEnum {
53+
accessTokens := []string{"public"}
54+
statementTokens := []string{"class", "interface", "enum"}
55+
5156
// use levenstein distance to check if the word is a typo
52-
tokens := []string{"class", "interface", "enum"}
57+
tokens := append(accessTokens, statementTokens...)
5358

5459
// get the nearest word
5560
nearestWord := ""
@@ -103,6 +108,11 @@ var IdentifierExpectedError = lib.ErrorTemplate{
103108
} else {
104109
m.Nearest = initialNearest
105110
}
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+
}
106116
}
107117
} else if tree, err := sitter.ParseCtx(
108118
context.Background(),
@@ -120,6 +130,8 @@ var IdentifierExpectedError = lib.ErrorTemplate{
120130
switch iCtx.reasonKind {
121131
case identifierExpectedReasonClassInterfaceEnum:
122132
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.")
123135
default:
124136
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.")
125137
}
@@ -152,7 +164,7 @@ var IdentifierExpectedError = lib.ErrorTemplate{
152164
})
153165
case identifierExpectedCorrectTypo:
154166
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).
156168
AddFix(lib.FixSuggestion{
157169
NewText: ctx.wordForTypo,
158170
StartPosition: cd.MainError.Nearest.StartPosition(),

error_templates/java/test_files/identifier_expected_error_complex_2/test.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public clas Main {
2424
```
2525
## Steps to fix
2626
### Correct the typo
27-
Change `clas` to `class` to properly declare the class.
27+
Change `clas` to `class`.
2828
```diff
2929
- public clas Main {
3030
+ public class Main {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
publc class Main {
2+
public static void main(String[] args) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: "Public"
2+
template: "Java.IdentifierExpectedError"
3+
---
4+
Main.java:1: error: class, interface, or enum expected
5+
publc class Main {
6+
^
7+
1 error
8+
===
9+
template: "Java.IdentifierExpectedError"
10+
---
11+
# IdentifierExpectedError
12+
This error indicates there's a typo or misspelled word in your code.
13+
```
14+
publc class Main {
15+
^^^^^
16+
public static void main(String[] args) {}
17+
}
18+
```
19+
## Steps to fix
20+
### Correct the typo
21+
Change `publc` to `public`.
22+
```diff
23+
- publc class Main {
24+
+ public class Main {
25+
public static void main(String[] args) {}
26+
}
27+
```

utils/slice/slice.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package slice
2+
3+
func ContainsString(slice []string, value string) bool {
4+
for _, v := range slice {
5+
if v == value {
6+
return true
7+
}
8+
}
9+
return false
10+
}

0 commit comments

Comments
 (0)