Skip to content

Commit

Permalink
fix: make Java.ArrayRequiredTypeError work
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Nov 28, 2023
1 parent 76c02d4 commit 6d556a1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
15 changes: 2 additions & 13 deletions error_templates/java/array_required_type_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ var ArrayRequiredTypeError = lib.ErrorTemplate{
// indexNode := cd.MainError.Nearest
tree := cd.InitOrGetSymbolTree(cd.MainDocumentPath())

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

s.AddStep("Declare the variable `%s` as an array of `%s`.", varNode.Text(), cd.Variables["foundType"]).
AddFix(lib.FixSuggestion{
NewText: fmt.Sprintf("%s[] %s = {%s}", cd.Variables["foundType"], varNode.Text(), valueNode.Text()),
NewText: fmt.Sprintf("%s[] %s = {%s};", cd.Variables["foundType"], varNode.Text(), valueNode.Text()),
StartPosition: declNode.StartPosition(),
EndPosition: declNode.EndPosition(),
})
})

gen.Add("Initialize an array and access its index", func(s *lib.BugFixSuggestion) {
s.AddStep("").
AddFix(lib.FixSuggestion{
NewText: "number[0] = 5a",
StartPosition: cd.MainError.Nearest.StartPosition(),
EndPosition: cd.MainError.Nearest.EndPosition(),
Description: "These changes will rectify the error by ensuring the variable is treated as an array when accessing elements by index.",
})
})
},
}
23 changes: 6 additions & 17 deletions error_templates/java/test_files/array_required_type_error/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,22 @@ NotArray.java:4: error: array required, but int found
template: "Java.ArrayRequiredTypeError"
---
# ArrayRequiredTypeError
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.
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.
```
int number = 5;
int value = number[0];
^
^
}
}
```
## Steps to fix
### 1. Change variable type to an array
### Convert variable to an array
Declare the variable `number` as an array of `int`.
```diff
public class NotArray {
public static void main(String[] args) {
- int number = 5;
+ int[] number = {5};
- int number = 5;
+ int[] number = {5};
int value = number[0];
}
}
```
### 2. Initialize an array and access its index
```diff
public class NotArray {
public static void main(String[] args) {
int[] number = {5};
+ number[0] = 5;
int value = number[0];
}
}
```
These changes will rectify the error by ensuring the variable is treated as an array when accessing elements by index.

0 comments on commit 6d556a1

Please sign in to comment.