diff --git a/error_templates/java/identifier_expected_error.go b/error_templates/java/identifier_expected_error.go index 20bb2c7..33fcab6 100644 --- a/error_templates/java/identifier_expected_error.go +++ b/error_templates/java/identifier_expected_error.go @@ -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" ) @@ -14,6 +15,7 @@ type identifiedExpectedReasonKind int const ( identifierExpectedReasonUnknown identifiedExpectedReasonKind = 0 identifierExpectedReasonClassInterfaceEnum identifiedExpectedReasonKind = iota + identifierExpectedReasonTypo identifiedExpectedReasonKind = iota ) type identifierExpectedFixKind int @@ -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 := "" @@ -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(), @@ -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.") } @@ -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(), diff --git a/error_templates/java/test_files/identifier_expected_error_complex_2/test.txt b/error_templates/java/test_files/identifier_expected_error_complex_2/test.txt index 33d09f7..91b55f3 100644 --- a/error_templates/java/test_files/identifier_expected_error_complex_2/test.txt +++ b/error_templates/java/test_files/identifier_expected_error_complex_2/test.txt @@ -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 { diff --git a/error_templates/java/test_files/identifier_expected_error_public/Main.java b/error_templates/java/test_files/identifier_expected_error_public/Main.java new file mode 100644 index 0000000..1460a12 --- /dev/null +++ b/error_templates/java/test_files/identifier_expected_error_public/Main.java @@ -0,0 +1,3 @@ +publc class Main { + public static void main(String[] args) {} +} diff --git a/error_templates/java/test_files/identifier_expected_error_public/test.txt b/error_templates/java/test_files/identifier_expected_error_public/test.txt new file mode 100644 index 0000000..d97605a --- /dev/null +++ b/error_templates/java/test_files/identifier_expected_error_public/test.txt @@ -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) {} +} +``` diff --git a/utils/slice/slice.go b/utils/slice/slice.go new file mode 100644 index 0000000..81b023c --- /dev/null +++ b/utils/slice/slice.go @@ -0,0 +1,10 @@ +package slice + +func ContainsString(slice []string, value string) bool { + for _, v := range slice { + if v == value { + return true + } + } + return false +}