From ec973e82d367735017dbcda32ce2df0b95aa853f Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Wed, 13 Dec 2023 00:52:56 +0800 Subject: [PATCH] feat: support for Java.NotAStatementError error --- error_templates/java/java.go | 1 + error_templates/java/not_a_statement_error.go | 54 +++++++++++++++++++ .../not_a_statement_error/NotAStatement.java | 5 ++ .../test_files/not_a_statement_error/test.txt | 42 +++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 error_templates/java/not_a_statement_error.go create mode 100644 error_templates/java/test_files/not_a_statement_error/NotAStatement.java diff --git a/error_templates/java/java.go b/error_templates/java/java.go index 7a25715..453416c 100644 --- a/error_templates/java/java.go +++ b/error_templates/java/java.go @@ -24,6 +24,7 @@ func LoadErrorTemplates(errorTemplates *lib.ErrorTemplates) { errorTemplates.MustAdd(java.Language, UnclosedCharacterLiteralError) errorTemplates.MustAdd(java.Language, OperatorCannotBeAppliedError) errorTemplates.MustAdd(java.Language, PrecisionLossError) + errorTemplates.MustAdd(java.Language, NotAStatementError) } func runtimeErrorPattern(errorName string, pattern string) string { diff --git a/error_templates/java/not_a_statement_error.go b/error_templates/java/not_a_statement_error.go new file mode 100644 index 0000000..7ee0fd7 --- /dev/null +++ b/error_templates/java/not_a_statement_error.go @@ -0,0 +1,54 @@ +package java + +import ( + "fmt" + "regexp" + "strings" + + lib "github.com/nedpals/errgoengine" +) + +func generateVarName(inputString string) string { + reg := regexp.MustCompile("[^a-zA-Z0-9_]") + processedString := reg.ReplaceAllString(inputString, "") + return strings.TrimSpace(processedString) +} + +var NotAStatementError = lib.ErrorTemplate{ + Name: "NotAStatementError", + Pattern: comptimeErrorPattern(`not a statement`), + StackTracePattern: comptimeStackTracePattern, + OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { + if m.Nearest.Type() == "expression_statement" { + m.Nearest = m.Nearest.NamedChild(0) + } + }, + OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { + gen.Add("This error occurs when a line of code is written that is not a valid statement.") + }, + OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { + nodeType := cd.Analyzer.AnalyzeNode(cd.MainError.Nearest) + + gen.Add(fmt.Sprintf("Convert the `%s` to a statement", nodeType.Name()), func(s *lib.BugFixSuggestion) { + s.AddStep( + "If you intended to use the `%s` as a statement, you can print it or use it in a valid statement.", + nodeType.Name(), + ).AddFix(lib.FixSuggestion{ + NewText: fmt.Sprintf("System.out.println(%s)", cd.MainError.Nearest.Text()), + StartPosition: cd.MainError.Nearest.StartPosition(), + EndPosition: cd.MainError.Nearest.EndPosition(), + Description: "This change makes the string part of a valid statement by printing it to the console.", + }) + }) + + gen.Add(fmt.Sprintf("Assign the `%s` to a variable", nodeType.Name()), func(s *lib.BugFixSuggestion) { + s.AddStep("Alternatively, you can assign the `%s` to a variable to make it a valid statement.", nodeType.Name()). + AddFix(lib.FixSuggestion{ + NewText: fmt.Sprintf("%s %s = %s", nodeType.Name(), generateVarName(cd.MainError.Nearest.Text()), cd.MainError.Nearest.Text()), + StartPosition: cd.MainError.Nearest.StartPosition(), + EndPosition: cd.MainError.Nearest.EndPosition(), + Description: "This way, the string is now part of a statement and can be used later in your code.", + }) + }) + }, +} diff --git a/error_templates/java/test_files/not_a_statement_error/NotAStatement.java b/error_templates/java/test_files/not_a_statement_error/NotAStatement.java new file mode 100644 index 0000000..c68dc8b --- /dev/null +++ b/error_templates/java/test_files/not_a_statement_error/NotAStatement.java @@ -0,0 +1,5 @@ +public class NotAStatement { + public static void main(String[] args) { + "test"; + } +} diff --git a/error_templates/java/test_files/not_a_statement_error/test.txt b/error_templates/java/test_files/not_a_statement_error/test.txt index e69de29..d868bf0 100644 --- a/error_templates/java/test_files/not_a_statement_error/test.txt +++ b/error_templates/java/test_files/not_a_statement_error/test.txt @@ -0,0 +1,42 @@ +template: "Java.NotAStatementError" +--- +NotAStatement.java:3: error: not a statement + "test"; + ^ +1 error +=== +template: "Java.NotAStatementError" +--- +# NotAStatementError +This error occurs when a line of code is written that is not a valid statement. +``` + public static void main(String[] args) { + "test"; + ^^^^^^ + } +} +``` +## Steps to fix +### 1. Convert the `String` to a statement +If you intended to use the `String` as a statement, you can print it or use it in a valid statement. +```diff +public class NotAStatement { + public static void main(String[] args) { +- "test"; ++ System.out.println("test"); + } +} +``` +This change makes the string part of a valid statement by printing it to the console. + +### 2. Assign the `String` to a variable +Alternatively, you can assign the `String` to a variable to make it a valid statement. +```diff +public class NotAStatement { + public static void main(String[] args) { +- "test"; ++ String test = "test"; + } +} +``` +This way, the string is now part of a statement and can be used later in your code.