|
| 1 | +package java |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + lib "github.com/nedpals/errgoengine" |
| 8 | +) |
| 9 | + |
| 10 | +type missingReturnErrorCtx struct { |
| 11 | + NearestMethod lib.SyntaxNode |
| 12 | +} |
| 13 | + |
| 14 | +var MissingReturnError = lib.ErrorTemplate{ |
| 15 | + Name: "MissingReturnError", |
| 16 | + Pattern: comptimeErrorPattern(`missing return statement`), |
| 17 | + StackTracePattern: comptimeStackTracePattern, |
| 18 | + OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) { |
| 19 | + // get nearest method declaration |
| 20 | + mCtx := missingReturnErrorCtx{} |
| 21 | + rootNode := lib.WrapNode(m.Document, m.Document.Tree.RootNode()) |
| 22 | + pos := m.ErrorNode.StartPos |
| 23 | + lib.QueryNode(rootNode, strings.NewReader("(method_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 | + mCtx.NearestMethod = node |
| 31 | + return false |
| 32 | + } |
| 33 | + } |
| 34 | + return true |
| 35 | + }) |
| 36 | + fmt.Println(mCtx.NearestMethod.Text()) |
| 37 | + m.Context = mCtx |
| 38 | + }, |
| 39 | + OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) { |
| 40 | + gen.Add("This error occurs when a method is declared to return a value, but there is no return statement within the method.") |
| 41 | + }, |
| 42 | + OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) { |
| 43 | + ctx := cd.MainError.Context.(missingReturnErrorCtx) |
| 44 | + |
| 45 | + // TODO |
| 46 | + gen.Add("Provide a return statement", func(s *lib.BugFixSuggestion) { |
| 47 | + bodyNode := ctx.NearestMethod.ChildByFieldName("body") |
| 48 | + lastStartPosInBlock := bodyNode.EndPosition() |
| 49 | + lastEndPosInBlock := bodyNode.EndPosition() |
| 50 | + if bodyNode.NamedChildCount() > 0 { |
| 51 | + lastStartPosInBlock = bodyNode.LastNamedChild().StartPosition() |
| 52 | + lastEndPosInBlock = bodyNode.LastNamedChild().EndPosition() |
| 53 | + } |
| 54 | + |
| 55 | + s.AddStep( |
| 56 | + "Since the `%s` method is declared to return an `%s`, you need to provide a return statement with the result", |
| 57 | + ctx.NearestMethod.ChildByFieldName("name").Text(), |
| 58 | + ctx.NearestMethod.ChildByFieldName("type").Text(), |
| 59 | + ).AddFix(lib.FixSuggestion{ |
| 60 | + NewText: "\n" + cd.MainError.Document.LineAt(lastStartPosInBlock.Line)[:lastStartPosInBlock.Column] + fmt.Sprintf("return %s;", ctx.NearestMethod.ChildByFieldName("type").Text()), |
| 61 | + StartPosition: lastEndPosInBlock, |
| 62 | + EndPosition: lastEndPosInBlock, |
| 63 | + Description: "This ensures that the method returns the sum of the two input numbers.", |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + gen.Add("Set the method return type to void", func(s *lib.BugFixSuggestion) { |
| 68 | + s.AddStep( |
| 69 | + "If you don't intend to return a value from the `%s` method, you can change its return type to `void`.", |
| 70 | + ctx.NearestMethod.ChildByFieldName("name").Text(), |
| 71 | + ).AddFix(lib.FixSuggestion{ |
| 72 | + NewText: "void", |
| 73 | + StartPosition: ctx.NearestMethod.ChildByFieldName("type").StartPosition(), |
| 74 | + EndPosition: ctx.NearestMethod.ChildByFieldName("type").EndPosition(), |
| 75 | + Description: "This is appropriate if you're using the method for side effects rather than returning a value.", |
| 76 | + }) |
| 77 | + }) |
| 78 | + }, |
| 79 | +} |
0 commit comments