Skip to content

Commit ec973e8

Browse files
committed
feat: support for Java.NotAStatementError error
1 parent 65e0ae4 commit ec973e8

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

error_templates/java/java.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func LoadErrorTemplates(errorTemplates *lib.ErrorTemplates) {
2424
errorTemplates.MustAdd(java.Language, UnclosedCharacterLiteralError)
2525
errorTemplates.MustAdd(java.Language, OperatorCannotBeAppliedError)
2626
errorTemplates.MustAdd(java.Language, PrecisionLossError)
27+
errorTemplates.MustAdd(java.Language, NotAStatementError)
2728
}
2829

2930
func runtimeErrorPattern(errorName string, pattern string) string {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package java
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
8+
lib "github.com/nedpals/errgoengine"
9+
)
10+
11+
func generateVarName(inputString string) string {
12+
reg := regexp.MustCompile("[^a-zA-Z0-9_]")
13+
processedString := reg.ReplaceAllString(inputString, "")
14+
return strings.TrimSpace(processedString)
15+
}
16+
17+
var NotAStatementError = lib.ErrorTemplate{
18+
Name: "NotAStatementError",
19+
Pattern: comptimeErrorPattern(`not a statement`),
20+
StackTracePattern: comptimeStackTracePattern,
21+
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
22+
if m.Nearest.Type() == "expression_statement" {
23+
m.Nearest = m.Nearest.NamedChild(0)
24+
}
25+
},
26+
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
27+
gen.Add("This error occurs when a line of code is written that is not a valid statement.")
28+
},
29+
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
30+
nodeType := cd.Analyzer.AnalyzeNode(cd.MainError.Nearest)
31+
32+
gen.Add(fmt.Sprintf("Convert the `%s` to a statement", nodeType.Name()), func(s *lib.BugFixSuggestion) {
33+
s.AddStep(
34+
"If you intended to use the `%s` as a statement, you can print it or use it in a valid statement.",
35+
nodeType.Name(),
36+
).AddFix(lib.FixSuggestion{
37+
NewText: fmt.Sprintf("System.out.println(%s)", cd.MainError.Nearest.Text()),
38+
StartPosition: cd.MainError.Nearest.StartPosition(),
39+
EndPosition: cd.MainError.Nearest.EndPosition(),
40+
Description: "This change makes the string part of a valid statement by printing it to the console.",
41+
})
42+
})
43+
44+
gen.Add(fmt.Sprintf("Assign the `%s` to a variable", nodeType.Name()), func(s *lib.BugFixSuggestion) {
45+
s.AddStep("Alternatively, you can assign the `%s` to a variable to make it a valid statement.", nodeType.Name()).
46+
AddFix(lib.FixSuggestion{
47+
NewText: fmt.Sprintf("%s %s = %s", nodeType.Name(), generateVarName(cd.MainError.Nearest.Text()), cd.MainError.Nearest.Text()),
48+
StartPosition: cd.MainError.Nearest.StartPosition(),
49+
EndPosition: cd.MainError.Nearest.EndPosition(),
50+
Description: "This way, the string is now part of a statement and can be used later in your code.",
51+
})
52+
})
53+
},
54+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class NotAStatement {
2+
public static void main(String[] args) {
3+
"test";
4+
}
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
template: "Java.NotAStatementError"
2+
---
3+
NotAStatement.java:3: error: not a statement
4+
"test";
5+
^
6+
1 error
7+
===
8+
template: "Java.NotAStatementError"
9+
---
10+
# NotAStatementError
11+
This error occurs when a line of code is written that is not a valid statement.
12+
```
13+
public static void main(String[] args) {
14+
"test";
15+
^^^^^^
16+
}
17+
}
18+
```
19+
## Steps to fix
20+
### 1. Convert the `String` to a statement
21+
If you intended to use the `String` as a statement, you can print it or use it in a valid statement.
22+
```diff
23+
public class NotAStatement {
24+
public static void main(String[] args) {
25+
- "test";
26+
+ System.out.println("test");
27+
}
28+
}
29+
```
30+
This change makes the string part of a valid statement by printing it to the console.
31+
32+
### 2. Assign the `String` to a variable
33+
Alternatively, you can assign the `String` to a variable to make it a valid statement.
34+
```diff
35+
public class NotAStatement {
36+
public static void main(String[] args) {
37+
- "test";
38+
+ String test = "test";
39+
}
40+
}
41+
```
42+
This way, the string is now part of a statement and can be used later in your code.

0 commit comments

Comments
 (0)