-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for Java.NotAStatementError error
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
}) | ||
}) | ||
}, | ||
} |
5 changes: 5 additions & 0 deletions
5
error_templates/java/test_files/not_a_statement_error/NotAStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class NotAStatement { | ||
public static void main(String[] args) { | ||
"test"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |