From 6e8804b1a02377153d9f4fbc64c81b59f39776ff Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Mon, 25 Dec 2023 12:03:52 +0800 Subject: [PATCH] feat: support Java.CannotBeAppliedError too many arguments --- .../java/cannot_be_applied_error.go | 86 +++++++++++++------ .../CannotBeApplied.java | 8 ++ .../test.txt | 33 +++++++ node.go | 5 ++ 4 files changed, 108 insertions(+), 24 deletions(-) create mode 100644 error_templates/java/test_files/cannot_be_applied_too_many_arguments/CannotBeApplied.java create mode 100644 error_templates/java/test_files/cannot_be_applied_too_many_arguments/test.txt diff --git a/error_templates/java/cannot_be_applied_error.go b/error_templates/java/cannot_be_applied_error.go index d1a7c3c..cf2c561 100644 --- a/error_templates/java/cannot_be_applied_error.go +++ b/error_templates/java/cannot_be_applied_error.go @@ -5,15 +5,24 @@ import ( "strings" lib "github.com/nedpals/errgoengine" + "github.com/nedpals/errgoengine/utils/numbers" +) + +type cannotBeAppliedErrorKind int + +const ( + cannotBeAppliedMismatchedArgType cannotBeAppliedErrorKind = 0 + cannotBeAppliedMismatchedArgCount cannotBeAppliedErrorKind = iota ) type cannotBeAppliedErrorCtx struct { - rawRequiredTypes []string - rawFoundTypes []string - requiredTypes []lib.Symbol - foundTypes []lib.Symbol - callExprNode lib.SyntaxNode - firstUnmatchedIdx int + rawRequiredTypes []string + rawFoundTypes []string + requiredTypes []lib.Symbol + foundTypes []lib.Symbol + callExprNode lib.SyntaxNode + kind cannotBeAppliedErrorKind + invalidIdx int } var CannotBeAppliedError = lib.ErrorTemplate{ @@ -43,15 +52,22 @@ var CannotBeAppliedError = lib.ErrorTemplate{ cCtx.foundTypes = append(cCtx.foundTypes, sym) } - // get first unmatched idx - for i := 0; i < len(cCtx.requiredTypes); i++ { - if i >= len(cCtx.foundTypes) { - break - } + // get invalid idx + if len(cCtx.rawFoundTypes) > len(cCtx.rawRequiredTypes) { + cCtx.kind = cannotBeAppliedMismatchedArgCount + cCtx.invalidIdx = len(cCtx.rawFoundTypes) - 1 + } else { + cCtx.kind = cannotBeAppliedMismatchedArgType + + for i := 0; i < len(cCtx.requiredTypes); i++ { + if i >= len(cCtx.foundTypes) { + break + } - if cCtx.requiredTypes[i] != cCtx.foundTypes[i] { - cCtx.firstUnmatchedIdx = i - break + if cCtx.requiredTypes[i] != cCtx.foundTypes[i] { + cCtx.invalidIdx = i + break + } } } @@ -77,7 +93,7 @@ var CannotBeAppliedError = lib.ErrorTemplate{ node := lib.WrapNode(m.Document, c.Node) fmt.Println(node.Text()) cCtx.callExprNode = node - argNode := node.ChildByFieldName("arguments").NamedChild(cCtx.firstUnmatchedIdx) + argNode := node.ChildByFieldName("arguments").NamedChild(cCtx.invalidIdx) m.Nearest = argNode return false } @@ -87,18 +103,40 @@ var CannotBeAppliedError = lib.ErrorTemplate{ m.Context = cCtx }, OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { - gen.Add("This error occurs when there is an attempt to apply a method with arguments that do not match the method signature.") + ctx := cd.MainError.Context.(cannotBeAppliedErrorCtx) + + switch ctx.kind { + case cannotBeAppliedMismatchedArgCount: + gen.Add("This error occurs when there is an attempt to apply a method with an incorrect number of arguments.") + case cannotBeAppliedMismatchedArgType: + gen.Add("This error occurs when there is an attempt to apply a method with arguments that do not match the method signature.") + default: + gen.Add("unable to determine.") + } }, OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { ctx := cd.MainError.Context.(cannotBeAppliedErrorCtx) - gen.Add("Use the correct argument types", func(s *lib.BugFixSuggestion) { - s.AddStep("Provide the correct argument types when calling the `%s` method", cd.Variables["method"]). - AddFix(lib.FixSuggestion{ - NewText: castValueNode(cd.MainError.Nearest, ctx.requiredTypes[ctx.firstUnmatchedIdx]), - StartPosition: cd.MainError.Nearest.StartPosition(), - EndPosition: cd.MainError.Nearest.EndPosition(), - }) - }) + switch ctx.kind { + case cannotBeAppliedMismatchedArgCount: + gen.Add("Use the correct number of arguments", func(s *lib.BugFixSuggestion) { + s.AddStep("Modify the `%s` method call to use only %s argument.", cd.Variables["method"], numbers.ToWords(len(ctx.rawRequiredTypes))). + AddFix(lib.FixSuggestion{ + NewText: "", + StartPosition: cd.MainError.Nearest.PrevSibling().StartPosition(), + EndPosition: cd.MainError.Nearest.EndPosition(), + }) + }) + case cannotBeAppliedMismatchedArgType: + gen.Add("Use the correct argument types", func(s *lib.BugFixSuggestion) { + s.AddStep("Provide the correct argument types when calling the `%s` method", cd.Variables["method"]). + AddFix(lib.FixSuggestion{ + NewText: castValueNode(cd.MainError.Nearest, ctx.requiredTypes[ctx.invalidIdx]), + StartPosition: cd.MainError.Nearest.StartPosition(), + EndPosition: cd.MainError.Nearest.EndPosition(), + }) + }) + } + }, } diff --git a/error_templates/java/test_files/cannot_be_applied_too_many_arguments/CannotBeApplied.java b/error_templates/java/test_files/cannot_be_applied_too_many_arguments/CannotBeApplied.java new file mode 100644 index 0000000..d6f9b32 --- /dev/null +++ b/error_templates/java/test_files/cannot_be_applied_too_many_arguments/CannotBeApplied.java @@ -0,0 +1,8 @@ +public class CannotBeApplied { + public static void main(String[] args) { + String text = "Hello"; + + // Method 'charAt' cannot be applied to String with arguments + char firstChar = text.charAt(0, 1); + } +} \ No newline at end of file diff --git a/error_templates/java/test_files/cannot_be_applied_too_many_arguments/test.txt b/error_templates/java/test_files/cannot_be_applied_too_many_arguments/test.txt new file mode 100644 index 0000000..1d28b11 --- /dev/null +++ b/error_templates/java/test_files/cannot_be_applied_too_many_arguments/test.txt @@ -0,0 +1,33 @@ +name: "TooManyArguments" +template: "Java.CannotBeAppliedError" +--- +CannotBeApplied.java:6: error: method charAt in class String cannot be applied to given types; + char firstChar = text.charAt(0, 1); + ^ + required: int + found: int,int + reason: actual and formal argument lists differ in length +1 error +=== +template: "Java.CannotBeAppliedError" +--- +# CannotBeAppliedError +This error occurs when there is an attempt to apply a method with an incorrect number of arguments. +``` + // Method 'charAt' cannot be applied to String with arguments + char firstChar = text.charAt(0, 1); + ^ + } +} +``` +## Steps to fix +### Use the correct number of arguments +Modify the `charAt` method call to use only one argument. +```diff + + // Method 'charAt' cannot be applied to String with arguments +- char firstChar = text.charAt(0, 1); ++ char firstChar = text.charAt(0); + } +} +``` \ No newline at end of file diff --git a/node.go b/node.go index 96f37a6..188d650 100644 --- a/node.go +++ b/node.go @@ -44,6 +44,11 @@ func (n SyntaxNode) Child(idx int) SyntaxNode { return WrapNode(n.Doc, cNode) } +func (n SyntaxNode) PrevSibling() SyntaxNode { + cNode := n.Node.PrevSibling() + return WrapNode(n.Doc, cNode) +} + func (n SyntaxNode) NamedDescendantForPointRange(posRange Location) SyntaxNode { sRange := posRange.Range() cNode := n.Node.NamedDescendantForPointRange(sRange.StartPoint, sRange.EndPoint)