Skip to content

Commit 6d556a1

Browse files
committed
fix: make Java.ArrayRequiredTypeError work
1 parent 76c02d4 commit 6d556a1

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

error_templates/java/array_required_type_error.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ var ArrayRequiredTypeError = lib.ErrorTemplate{
4141
// indexNode := cd.MainError.Nearest
4242
tree := cd.InitOrGetSymbolTree(cd.MainDocumentPath())
4343

44-
gen.Add("Change variable type to an array", func(s *lib.BugFixSuggestion) {
45-
fmt.Printf("%v\n", tree.GetNearestScopedTree(varNode.StartPosition().Index).Symbols["number"])
44+
gen.Add("Convert variable to an array", func(s *lib.BugFixSuggestion) {
4645
declSym := tree.GetSymbolByNode(getIdentifierNode(varNode))
4746
declNode := lib.WrapNode(
4847
cd.MainError.Document,
@@ -56,20 +55,10 @@ var ArrayRequiredTypeError = lib.ErrorTemplate{
5655

5756
s.AddStep("Declare the variable `%s` as an array of `%s`.", varNode.Text(), cd.Variables["foundType"]).
5857
AddFix(lib.FixSuggestion{
59-
NewText: fmt.Sprintf("%s[] %s = {%s}", cd.Variables["foundType"], varNode.Text(), valueNode.Text()),
58+
NewText: fmt.Sprintf("%s[] %s = {%s};", cd.Variables["foundType"], varNode.Text(), valueNode.Text()),
6059
StartPosition: declNode.StartPosition(),
6160
EndPosition: declNode.EndPosition(),
6261
})
6362
})
64-
65-
gen.Add("Initialize an array and access its index", func(s *lib.BugFixSuggestion) {
66-
s.AddStep("").
67-
AddFix(lib.FixSuggestion{
68-
NewText: "number[0] = 5a",
69-
StartPosition: cd.MainError.Nearest.StartPosition(),
70-
EndPosition: cd.MainError.Nearest.EndPosition(),
71-
Description: "These changes will rectify the error by ensuring the variable is treated as an array when accessing elements by index.",
72-
})
73-
})
7463
},
7564
}

error_templates/java/test_files/array_required_type_error/test.txt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,22 @@ NotArray.java:4: error: array required, but int found
88
template: "Java.ArrayRequiredTypeError"
99
---
1010
# ArrayRequiredTypeError
11-
This error occurs because the variable `number` is declared as an `int` rather than an array. You're attempting to access an index (`[0]`) on a variable that's not an array.
11+
This error occurs because the variable `number` is declared as an `int` rather than an array. You're attempting to access an index (`0`) on a variable that's not an array.
1212
```
1313
int number = 5;
1414
int value = number[0];
15-
^
15+
^
1616
}
1717
}
1818
```
1919
## Steps to fix
20-
### 1. Change variable type to an array
20+
### Convert variable to an array
21+
Declare the variable `number` as an array of `int`.
2122
```diff
2223
public class NotArray {
2324
public static void main(String[] args) {
24-
- int number = 5;
25-
+ int[] number = {5};
25+
- int number = 5;
26+
+ int[] number = {5};
2627
int value = number[0];
2728
}
28-
}
29-
```
30-
### 2. Initialize an array and access its index
31-
```diff
32-
public class NotArray {
33-
public static void main(String[] args) {
34-
int[] number = {5};
35-
+ number[0] = 5;
36-
int value = number[0];
37-
}
38-
}
3929
```
40-
These changes will rectify the error by ensuring the variable is treated as an array when accessing elements by index.

0 commit comments

Comments
 (0)