Skip to content

Commit 8f8238d

Browse files
committed
feat: add BracketMismatchError (todo)
1 parent d1b69ca commit 8f8238d

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package java
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
lib "github.com/nedpals/errgoengine"
9+
)
10+
11+
var BracketMismatchError = lib.ErrorTemplate{
12+
Name: "ArrayIndexOutOfBoundsException",
13+
Pattern: comptimeErrorPattern(`'(?P<expected>\S+)' expected`),
14+
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
15+
lib.QueryNode(m.Nearest, strings.NewReader("(array_access index: (_) @index)"), func(ctx lib.QueryNodeCtx) bool {
16+
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
17+
for _, c := range match.Captures {
18+
node := lib.WrapNode(m.Nearest.Doc, c.Node)
19+
m.Nearest = node
20+
return false
21+
}
22+
return true
23+
})
24+
},
25+
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
26+
gen.Add("This error occurs because the code is trying to access index %s that is beyond the bounds of the array which only has %s items.", cd.Variables["index"], cd.Variables["length"])
27+
},
28+
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
29+
arrayLen, _ := strconv.Atoi(cd.Variables["length"])
30+
31+
// TODO: add a suggestion to add an if statement if the array length is 0
32+
33+
gen.Add("Accessing Array Index Within Bounds", func(s *lib.BugFixSuggestion) {
34+
sampleIndex := max(0, arrayLen-2)
35+
36+
s.AddStep("The error is caused by trying to access an index that does not exist within the array. Instead of accessing index %s, which is beyond the array's length, change it to a valid index within the array bounds, for example, `nums[%d]`.", cd.Variables["index"], sampleIndex).
37+
AddFix(lib.FixSuggestion{
38+
NewText: fmt.Sprintf("%d", sampleIndex),
39+
StartPosition: cd.MainError.Nearest.StartPosition(),
40+
EndPosition: cd.MainError.Nearest.EndPosition(),
41+
Description: "This adjustment ensures that you're accessing an index that exists within the array bounds, preventing the `ArrayIndexOutOfBoundsException`.",
42+
})
43+
})
44+
},
45+
}

error_templates/java/test_files/bracket_mismatch_error/test.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,19 @@ BracketMismatch.java:7: error: '}' expected
77
===
88
template: "Java.BracketMismatchError"
99
---
10-
aaa
10+
# BracketMismatchError
11+
This error occurs because there is a missing closing bracket for the if statement.
12+
13+
## Steps to fix
14+
### Add Missing Bracket
15+
Add a closing bracket '}' after the `System.out.println` statement.
16+
```diff
17+
System.out.println("x is greater than 5.");
18+
+ }
19+
```
20+
21+
### Remove Excessive Bracket
22+
If you accidentally added an extra closing bracket '}' at the end, remove it.
23+
```diff
24+
} // Remove this line if it's an extra bracket
25+
```

error_templates/java/test_files/not_a_statement_error/test.txt

Whitespace-only changes.

tests/java/BracketMismatch.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// BracketMismatch.java
2+
public class BracketMismatch {
3+
public static void main(String[] args) {
4+
int x = 10;
5+
if (x > 5) {
6+
System.out.println("x is greater than 5.");
7+
// Missing closing bracket for if statement
8+
}
9+
}
10+

0 commit comments

Comments
 (0)