Skip to content

Commit

Permalink
fix: illegal expression start not showing
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Apr 1, 2024
1 parent bbb6627 commit 66b671a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
30 changes: 25 additions & 5 deletions error_templates/java/illegal_expression_start_error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package java

import (
"strings"

lib "github.com/nedpals/errgoengine"
)

Expand All @@ -9,11 +11,20 @@ var IllegalExpressionStartError = lib.ErrorTemplate{
Pattern: comptimeErrorPattern(`illegal start of expression`),
StackTracePattern: comptimeStackTracePattern,
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
for q := m.Nearest.Query("(ERROR) @error"); q.Next(); {
node := q.CurrentNode()
m.Nearest = node
// aCtx.NearestClass = node
break
for nearest := m.Nearest; !nearest.IsNull(); nearest = nearest.Parent() {
found := false

for q := nearest.Query("(ERROR) @error"); q.Next(); {
node := q.CurrentNode()
m.Nearest = node
found = true
// aCtx.NearestClass = node
break
}

if found {
break
}
}
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
Expand Down Expand Up @@ -44,6 +55,15 @@ var IllegalExpressionStartError = lib.ErrorTemplate{
EndPosition: parent.EndPosition(),
})
})
} else if errNodeText := cd.MainError.Nearest.Text(); !strings.HasPrefix(errNodeText, "}") && strings.HasSuffix(errNodeText, "else") {
gen.Add("Use the right closing bracket", func(s *lib.BugFixSuggestion) {
s.AddStep("Ensure that the right closing bracket for the else branch of your if statement is used").
AddFix(lib.FixSuggestion{
NewText: "} else",
StartPosition: cd.MainError.Nearest.StartPosition(),
EndPosition: cd.MainError.Nearest.EndPosition(),
})
})
}
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Main {
public static void main(String[] args) {
int inventoryCount = 10;
String item = "a";
System.out.println(item);

if (true) {
System.out.println("b");
) else {
System.out.println("c");
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
template: "Java.IllegalExpressionStartError"
name: "If"
---
Main.java:9: error: illegal start of expression
) else {
^
1 errors
===
template: "Java.IllegalExpressionStartError"
---
# IllegalExpressionStartError
This error occurs when the compiler encounters an expression that is not valid.
```
System.out.println("b");
) else {
^^^^^^
System.out.println("c");
}
```
## Steps to fix
### Use the right closing bracket
Ensure that the right closing bracket for the else branch of your if statement is used.
```diff
if (true) {
System.out.println("b");
- ) else {
+ } else {
System.out.println("c");
}
```

0 comments on commit 66b671a

Please sign in to comment.