Skip to content

Commit

Permalink
fix(error_templates/java): include access modifiers in typo
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Mar 14, 2024
1 parent 93cc92e commit 5f2d310
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
16 changes: 14 additions & 2 deletions error_templates/java/identifier_expected_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

lib "github.com/nedpals/errgoengine"
"github.com/nedpals/errgoengine/utils/levenshtein"
"github.com/nedpals/errgoengine/utils/slice"
sitter "github.com/smacker/go-tree-sitter"
)

Expand All @@ -14,6 +15,7 @@ type identifiedExpectedReasonKind int
const (
identifierExpectedReasonUnknown identifiedExpectedReasonKind = 0
identifierExpectedReasonClassInterfaceEnum identifiedExpectedReasonKind = iota
identifierExpectedReasonTypo identifiedExpectedReasonKind = iota
)

type identifierExpectedFixKind int
Expand Down Expand Up @@ -48,8 +50,11 @@ var IdentifierExpectedError = lib.ErrorTemplate{

// TODO: check if node is parsable
if iCtx.reasonKind == identifierExpectedReasonClassInterfaceEnum {
accessTokens := []string{"public"}
statementTokens := []string{"class", "interface", "enum"}

// use levenstein distance to check if the word is a typo
tokens := []string{"class", "interface", "enum"}
tokens := append(accessTokens, statementTokens...)

// get the nearest word
nearestWord := ""
Expand Down Expand Up @@ -103,6 +108,11 @@ var IdentifierExpectedError = lib.ErrorTemplate{
} else {
m.Nearest = initialNearest
}

// if nearestword is not a statement token, then it's a typo
if !slice.ContainsString(statementTokens, nearestWord) {
iCtx.reasonKind = identifierExpectedReasonTypo
}
}
} else if tree, err := sitter.ParseCtx(
context.Background(),
Expand All @@ -120,6 +130,8 @@ var IdentifierExpectedError = lib.ErrorTemplate{
switch iCtx.reasonKind {
case identifierExpectedReasonClassInterfaceEnum:
gen.Add("This error occurs when there's a typo or the keyword `class`, `interface`, or `enum` is missing.")
case identifierExpectedReasonTypo:
gen.Add("This error indicates there's a typo or misspelled word in your code.")
default:
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.")
}
Expand Down Expand Up @@ -152,7 +164,7 @@ var IdentifierExpectedError = lib.ErrorTemplate{
})
case identifierExpectedCorrectTypo:
gen.Add("Correct the typo", func(s *lib.BugFixSuggestion) {
s.AddStep("Change `%s` to `%s` to properly declare the %s.", ctx.typoWord, ctx.wordForTypo, ctx.wordForTypo).
s.AddStep("Change `%s` to `%s`.", ctx.typoWord, ctx.wordForTypo).
AddFix(lib.FixSuggestion{
NewText: ctx.wordForTypo,
StartPosition: cd.MainError.Nearest.StartPosition(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public clas Main {
```
## Steps to fix
### Correct the typo
Change `clas` to `class` to properly declare the class.
Change `clas` to `class`.
```diff
- public clas Main {
+ public class Main {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
publc class Main {
public static void main(String[] args) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: "Public"
template: "Java.IdentifierExpectedError"
---
Main.java:1: error: class, interface, or enum expected
publc class Main {
^
1 error
===
template: "Java.IdentifierExpectedError"
---
# IdentifierExpectedError
This error indicates there's a typo or misspelled word in your code.
```
publc class Main {
^^^^^
public static void main(String[] args) {}
}
```
## Steps to fix
### Correct the typo
Change `publc` to `public`.
```diff
- publc class Main {
+ public class Main {
public static void main(String[] args) {}
}
```
10 changes: 10 additions & 0 deletions utils/slice/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package slice

func ContainsString(slice []string, value string) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}

0 comments on commit 5f2d310

Please sign in to comment.