Skip to content

Commit b150fa5

Browse files
committed
feat: support for Java.InvalidMethodDeclarationError
1 parent c735919 commit b150fa5

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package java
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
lib "github.com/nedpals/errgoengine"
8+
)
9+
10+
type invalidMethodDeclarationErrorCtx struct {
11+
declNode lib.SyntaxNode
12+
returnTypeToAdd lib.Symbol
13+
}
14+
15+
var InvalidMethodDeclarationError = lib.ErrorTemplate{
16+
Name: "InvalidMethodDeclarationError",
17+
Pattern: comptimeErrorPattern("invalid method declaration; return type required"),
18+
StackTracePattern: comptimeStackTracePattern,
19+
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
20+
iCtx := invalidMethodDeclarationErrorCtx{}
21+
pos := m.ErrorNode.StartPos
22+
23+
lib.QueryNode(m.Document.RootNode(), strings.NewReader("(constructor_declaration) @method"), func(ctx lib.QueryNodeCtx) bool {
24+
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
25+
for _, c := range match.Captures {
26+
pointA := c.Node.StartPoint()
27+
pointB := c.Node.EndPoint()
28+
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
29+
node := lib.WrapNode(m.Nearest.Doc, c.Node)
30+
iCtx.declNode = node
31+
m.Nearest = node.ChildByFieldName("name")
32+
return false
33+
}
34+
}
35+
return true
36+
})
37+
38+
iCtx.returnTypeToAdd = lib.UnwrapReturnType(cd.FindSymbol(m.Nearest.Text(), m.Nearest.StartPosition().Index))
39+
m.Context = iCtx
40+
},
41+
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
42+
gen.Add("This error occurs when there is an invalid method declaration, and a return type is missing.")
43+
},
44+
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
45+
ctx := cd.MainError.Context.(invalidMethodDeclarationErrorCtx)
46+
47+
gen.Add("Add the return type to the method declaration", func(s *lib.BugFixSuggestion) {
48+
s.AddStep("Specify the return type of the `%s` method", cd.MainError.Nearest.Text()).
49+
AddFix(lib.FixSuggestion{
50+
NewText: fmt.Sprintf("%s ", ctx.returnTypeToAdd.Name()),
51+
StartPosition: cd.MainError.Nearest.StartPosition(),
52+
EndPosition: cd.MainError.Nearest.StartPosition(),
53+
})
54+
})
55+
},
56+
}

error_templates/java/java.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func LoadErrorTemplates(errorTemplates *lib.ErrorTemplates) {
3535
errorTemplates.MustAdd(java.Language, UnclosedStringLiteralError)
3636
errorTemplates.MustAdd(java.Language, CannotBeAppliedError)
3737
errorTemplates.MustAdd(java.Language, BracketMismatchError)
38+
errorTemplates.MustAdd(java.Language, InvalidMethodDeclarationError)
3839
}
3940

4041
func runtimeErrorPattern(errorName string, pattern string) string {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class InvalidMethodExample {
2+
public static void main(String[] args) {
3+
int result = addNumbers(5, 10);
4+
System.out.println("Sum: " + result);
5+
}
6+
7+
// Invalid method declaration with missing return type
8+
addNumbers(int a, int b) {
9+
return a + b;
10+
}
11+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
template: "Java.InvalidMethodDeclarationError"
2+
---
3+
InvalidMethodExample.java:8: error: invalid method declaration; return type required
4+
addNumbers(int a, int b) {
5+
^
6+
1 error
7+
===
8+
template: "Java.InvalidMethodDeclarationError"
9+
---
10+
# InvalidMethodDeclarationError
11+
This error occurs when there is an invalid method declaration, and a return type is missing.
12+
```
13+
// Invalid method declaration with missing return type
14+
addNumbers(int a, int b) {
15+
^^^^^^^^^^
16+
return a + b;
17+
}
18+
```
19+
## Steps to fix
20+
### Add the return type to the method declaration
21+
Specify the return type of the `addNumbers` method.
22+
```diff
23+
24+
// Invalid method declaration with missing return type
25+
- addNumbers(int a, int b) {
26+
+ int addNumbers(int a, int b) {
27+
return a + b;
28+
}
29+
```

languages/java/symbols.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
declarator: (variable_declarator
1919
name: (identifier) @variable.name
2020
value: (_)? @variable.content) @variable.declaration) @variable
21+
22+
(constructor_declaration
23+
name: (identifier) @method.name
24+
parameters: (formal_parameters
25+
(formal_parameter
26+
type: (_) @parameter.return-type
27+
name: (identifier) @parameter.name)? @parameter) @parameters
28+
body: (constructor_body
29+
(return_statement
30+
(_) @block.content)?) @block) @method
31+
2132
(method_declaration
2233
type: (_) @method.return-type
2334
name: (identifier) @method.name

0 commit comments

Comments
 (0)