Skip to content

Commit 6e8804b

Browse files
committed
feat: support Java.CannotBeAppliedError too many arguments
1 parent 9b64ab8 commit 6e8804b

File tree

4 files changed

+108
-24
lines changed

4 files changed

+108
-24
lines changed

error_templates/java/cannot_be_applied_error.go

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ import (
55
"strings"
66

77
lib "github.com/nedpals/errgoengine"
8+
"github.com/nedpals/errgoengine/utils/numbers"
9+
)
10+
11+
type cannotBeAppliedErrorKind int
12+
13+
const (
14+
cannotBeAppliedMismatchedArgType cannotBeAppliedErrorKind = 0
15+
cannotBeAppliedMismatchedArgCount cannotBeAppliedErrorKind = iota
816
)
917

1018
type cannotBeAppliedErrorCtx struct {
11-
rawRequiredTypes []string
12-
rawFoundTypes []string
13-
requiredTypes []lib.Symbol
14-
foundTypes []lib.Symbol
15-
callExprNode lib.SyntaxNode
16-
firstUnmatchedIdx int
19+
rawRequiredTypes []string
20+
rawFoundTypes []string
21+
requiredTypes []lib.Symbol
22+
foundTypes []lib.Symbol
23+
callExprNode lib.SyntaxNode
24+
kind cannotBeAppliedErrorKind
25+
invalidIdx int
1726
}
1827

1928
var CannotBeAppliedError = lib.ErrorTemplate{
@@ -43,15 +52,22 @@ var CannotBeAppliedError = lib.ErrorTemplate{
4352
cCtx.foundTypes = append(cCtx.foundTypes, sym)
4453
}
4554

46-
// get first unmatched idx
47-
for i := 0; i < len(cCtx.requiredTypes); i++ {
48-
if i >= len(cCtx.foundTypes) {
49-
break
50-
}
55+
// get invalid idx
56+
if len(cCtx.rawFoundTypes) > len(cCtx.rawRequiredTypes) {
57+
cCtx.kind = cannotBeAppliedMismatchedArgCount
58+
cCtx.invalidIdx = len(cCtx.rawFoundTypes) - 1
59+
} else {
60+
cCtx.kind = cannotBeAppliedMismatchedArgType
61+
62+
for i := 0; i < len(cCtx.requiredTypes); i++ {
63+
if i >= len(cCtx.foundTypes) {
64+
break
65+
}
5166

52-
if cCtx.requiredTypes[i] != cCtx.foundTypes[i] {
53-
cCtx.firstUnmatchedIdx = i
54-
break
67+
if cCtx.requiredTypes[i] != cCtx.foundTypes[i] {
68+
cCtx.invalidIdx = i
69+
break
70+
}
5571
}
5672
}
5773

@@ -77,7 +93,7 @@ var CannotBeAppliedError = lib.ErrorTemplate{
7793
node := lib.WrapNode(m.Document, c.Node)
7894
fmt.Println(node.Text())
7995
cCtx.callExprNode = node
80-
argNode := node.ChildByFieldName("arguments").NamedChild(cCtx.firstUnmatchedIdx)
96+
argNode := node.ChildByFieldName("arguments").NamedChild(cCtx.invalidIdx)
8197
m.Nearest = argNode
8298
return false
8399
}
@@ -87,18 +103,40 @@ var CannotBeAppliedError = lib.ErrorTemplate{
87103
m.Context = cCtx
88104
},
89105
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
90-
gen.Add("This error occurs when there is an attempt to apply a method with arguments that do not match the method signature.")
106+
ctx := cd.MainError.Context.(cannotBeAppliedErrorCtx)
107+
108+
switch ctx.kind {
109+
case cannotBeAppliedMismatchedArgCount:
110+
gen.Add("This error occurs when there is an attempt to apply a method with an incorrect number of arguments.")
111+
case cannotBeAppliedMismatchedArgType:
112+
gen.Add("This error occurs when there is an attempt to apply a method with arguments that do not match the method signature.")
113+
default:
114+
gen.Add("unable to determine.")
115+
}
91116
},
92117
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
93118
ctx := cd.MainError.Context.(cannotBeAppliedErrorCtx)
94119

95-
gen.Add("Use the correct argument types", func(s *lib.BugFixSuggestion) {
96-
s.AddStep("Provide the correct argument types when calling the `%s` method", cd.Variables["method"]).
97-
AddFix(lib.FixSuggestion{
98-
NewText: castValueNode(cd.MainError.Nearest, ctx.requiredTypes[ctx.firstUnmatchedIdx]),
99-
StartPosition: cd.MainError.Nearest.StartPosition(),
100-
EndPosition: cd.MainError.Nearest.EndPosition(),
101-
})
102-
})
120+
switch ctx.kind {
121+
case cannotBeAppliedMismatchedArgCount:
122+
gen.Add("Use the correct number of arguments", func(s *lib.BugFixSuggestion) {
123+
s.AddStep("Modify the `%s` method call to use only %s argument.", cd.Variables["method"], numbers.ToWords(len(ctx.rawRequiredTypes))).
124+
AddFix(lib.FixSuggestion{
125+
NewText: "",
126+
StartPosition: cd.MainError.Nearest.PrevSibling().StartPosition(),
127+
EndPosition: cd.MainError.Nearest.EndPosition(),
128+
})
129+
})
130+
case cannotBeAppliedMismatchedArgType:
131+
gen.Add("Use the correct argument types", func(s *lib.BugFixSuggestion) {
132+
s.AddStep("Provide the correct argument types when calling the `%s` method", cd.Variables["method"]).
133+
AddFix(lib.FixSuggestion{
134+
NewText: castValueNode(cd.MainError.Nearest, ctx.requiredTypes[ctx.invalidIdx]),
135+
StartPosition: cd.MainError.Nearest.StartPosition(),
136+
EndPosition: cd.MainError.Nearest.EndPosition(),
137+
})
138+
})
139+
}
140+
103141
},
104142
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class CannotBeApplied {
2+
public static void main(String[] args) {
3+
String text = "Hello";
4+
5+
// Method 'charAt' cannot be applied to String with arguments
6+
char firstChar = text.charAt(0, 1);
7+
}
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: "TooManyArguments"
2+
template: "Java.CannotBeAppliedError"
3+
---
4+
CannotBeApplied.java:6: error: method charAt in class String cannot be applied to given types;
5+
char firstChar = text.charAt(0, 1);
6+
^
7+
required: int
8+
found: int,int
9+
reason: actual and formal argument lists differ in length
10+
1 error
11+
===
12+
template: "Java.CannotBeAppliedError"
13+
---
14+
# CannotBeAppliedError
15+
This error occurs when there is an attempt to apply a method with an incorrect number of arguments.
16+
```
17+
// Method 'charAt' cannot be applied to String with arguments
18+
char firstChar = text.charAt(0, 1);
19+
^
20+
}
21+
}
22+
```
23+
## Steps to fix
24+
### Use the correct number of arguments
25+
Modify the `charAt` method call to use only one argument.
26+
```diff
27+
28+
// Method 'charAt' cannot be applied to String with arguments
29+
- char firstChar = text.charAt(0, 1);
30+
+ char firstChar = text.charAt(0);
31+
}
32+
}
33+
```

node.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func (n SyntaxNode) Child(idx int) SyntaxNode {
4444
return WrapNode(n.Doc, cNode)
4545
}
4646

47+
func (n SyntaxNode) PrevSibling() SyntaxNode {
48+
cNode := n.Node.PrevSibling()
49+
return WrapNode(n.Doc, cNode)
50+
}
51+
4752
func (n SyntaxNode) NamedDescendantForPointRange(posRange Location) SyntaxNode {
4853
sRange := posRange.Range()
4954
cNode := n.Node.NamedDescendantForPointRange(sRange.StartPoint, sRange.EndPoint)

0 commit comments

Comments
 (0)