Skip to content

Commit

Permalink
feat: support for Java.NotAStatementError error
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Dec 12, 2023
1 parent 65e0ae4 commit ec973e8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions error_templates/java/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions error_templates/java/not_a_statement_error.go
Original file line number Diff line number Diff line change
@@ -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.",
})
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class NotAStatement {
public static void main(String[] args) {
"test";
}
}
42 changes: 42 additions & 0 deletions error_templates/java/test_files/not_a_statement_error/test.txt
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ec973e8

Please sign in to comment.