Skip to content

Commit

Permalink
refactor: simplify QueryNode usage; use iterator approach
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Dec 30, 2023
1 parent b150fa5 commit 2f848c1
Show file tree
Hide file tree
Showing 26 changed files with 302 additions and 319 deletions.
33 changes: 12 additions & 21 deletions error_templates/java/already_defined_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,21 @@ var AlreadyDefinedError = lib.ErrorTemplate{
pos := m.ErrorNode.StartPos

// get the nearest class declaration first based on error location
lib.QueryNode(rootNode, strings.NewReader("(class_declaration) @class"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
pointA := c.Node.StartPoint()
pointB := c.Node.EndPoint()
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
aCtx.NearestClass = node
return false
}
for q := rootNode.Query("(class_declaration) @class"); q.Next(); {
classNode := q.CurrentNode()
pointA := classNode.StartPoint()
pointB := classNode.EndPoint()
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
aCtx.NearestClass = classNode
break
}
return true
})
}

// get the nearest method declaration based on symbol signature
lib.QueryNode(aCtx.NearestClass, strings.NewReader(rawQuery), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
aCtx.NearestMethod = node
return false
}
return true
})
for q := aCtx.NearestClass.Query(rawQuery); q.Next(); {
aCtx.NearestMethod = q.CurrentNode()
break
}

m.Context = aCtx
},
Expand Down
15 changes: 4 additions & 11 deletions error_templates/java/arithmetic_exception.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package java

import (
"strings"

lib "github.com/nedpals/errgoengine"
)

Expand Down Expand Up @@ -38,15 +36,10 @@ var ArithmeticException = lib.ErrorTemplate{
}

if len(query) != 0 {
lib.QueryNode(cd.MainError.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(cd.MainError.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(cd.MainError.Nearest.Doc, c.Node)
err.Nearest = node
return false
}
return true
})
for q := err.Nearest.Query(query); q.Next(); {
err.Nearest = q.CurrentNode()
break
}
}

err.Context = ctx
Expand Down
15 changes: 5 additions & 10 deletions error_templates/java/array_index_out_of_bounds_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package java
import (
"fmt"
"strconv"
"strings"

lib "github.com/nedpals/errgoengine"
)
Expand All @@ -12,15 +11,11 @@ var ArrayIndexOutOfBoundsException = lib.ErrorTemplate{
Name: "ArrayIndexOutOfBoundsException",
Pattern: runtimeErrorPattern("java.lang.ArrayIndexOutOfBoundsException", `Index (?P<index>\d+) out of bounds for length (?P<length>\d+)`),
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
lib.QueryNode(m.Nearest, strings.NewReader("(array_access index: (_) @index)"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
m.Nearest = node
return false
}
return true
})
for q := m.Nearest.Query(`(array_access index: (_) @index)`); q.Next(); {
node := q.CurrentNode()
m.Nearest = node
break
}
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
gen.Add("This error occurs because the code is trying to access index %s that is beyond the bounds of the array which only has %s items.", cd.Variables["index"], cd.Variables["length"])
Expand Down
15 changes: 4 additions & 11 deletions error_templates/java/array_required_type_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package java

import (
"fmt"
"strings"

lib "github.com/nedpals/errgoengine"
)
Expand All @@ -12,16 +11,10 @@ var ArrayRequiredTypeError = lib.ErrorTemplate{
Pattern: comptimeErrorPattern(`array required, but (?P<foundType>\S+) found`),
StackTracePattern: comptimeStackTracePattern,
OnAnalyzeErrorFn: func(cd *lib.ContextData, err *lib.MainError) {
query := strings.NewReader("(array_access array: (identifier) index: ((_) @index (#eq? @index \"0\")))")
lib.QueryNode(cd.MainError.Nearest, query, func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(cd.MainError.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(cd.MainError.Nearest.Doc, c.Node)
err.Nearest = node
return false
}
return true
})
for q := cd.MainError.Nearest.Query("(array_access array: (identifier) index: ((_) @index (#eq? @index \"0\")))"); q.Next(); {
err.Nearest = q.CurrentNode()
break
}
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
parent := cd.MainError.Nearest.Parent()
Expand Down
15 changes: 5 additions & 10 deletions error_templates/java/bracket_mismatch_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package java
import (
"fmt"
"strconv"
"strings"

lib "github.com/nedpals/errgoengine"
)
Expand All @@ -12,15 +11,11 @@ var BracketMismatchError = lib.ErrorTemplate{
Name: "ArrayIndexOutOfBoundsException",
Pattern: comptimeErrorPattern(`'(?P<expected>\S+)' expected`),
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
lib.QueryNode(m.Nearest, strings.NewReader("(array_access index: (_) @index)"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
m.Nearest = node
return false
}
return true
})
for q := m.Nearest.Query(`(array_access index: (_) @index)`); q.Next(); {
node := q.CurrentNode()
m.Nearest = node
break
}
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
gen.Add("This error occurs because the code is trying to access index %s that is beyond the bounds of the array which only has %s items.", cd.Variables["index"], cd.Variables["length"])
Expand Down
23 changes: 9 additions & 14 deletions error_templates/java/cannot_be_applied_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,15 @@ var CannotBeAppliedError = lib.ErrorTemplate{
argumentNodeTypesToLook += nTypesStr
}

rawQuery := fmt.Sprintf(`((method_invocation name: (identifier) @name arguments: (argument_list %s)) @call (#eq? @name "%s"))`, argumentNodeTypesToLook, cd.Variables["method"])

lib.QueryNode(m.Nearest, strings.NewReader(rawQuery), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Document, c.Node)
fmt.Println(node.Text())
cCtx.callExprNode = node
argNode := node.ChildByFieldName("arguments").NamedChild(cCtx.invalidIdx)
m.Nearest = argNode
return false
}
return true
})
for q := cd.MainError.Nearest.Query(
`((method_invocation name: (identifier) @name arguments: (argument_list %s)) @call (#eq? @name "%s"))`,
argumentNodeTypesToLook, cd.Variables["method"],
); q.Next(); {
node := q.CurrentNode()
cCtx.callExprNode = node
m.Nearest = node.ChildByFieldName("arguments").NamedChild(cCtx.invalidIdx)
break
}

m.Context = cCtx
},
Expand Down
18 changes: 6 additions & 12 deletions error_templates/java/illegal_expression_start_error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package java

import (
"strings"

lib "github.com/nedpals/errgoengine"
)

Expand All @@ -11,16 +9,12 @@ var IllegalExpressionStartError = lib.ErrorTemplate{
Pattern: comptimeErrorPattern(`illegal start of expression`),
StackTracePattern: comptimeStackTracePattern,
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
lib.QueryNode(m.Nearest, strings.NewReader("(ERROR) @error"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
m.Nearest = node
// aCtx.NearestClass = node
return false
}
return true
})
for q := m.Nearest.Query("(ERROR) @error"); q.Next(); {
node := q.CurrentNode()
m.Nearest = node
// aCtx.NearestClass = node
break
}
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
gen.Add("This error occurs when the compiler encounters an expression that is not valid.")
Expand Down
23 changes: 9 additions & 14 deletions error_templates/java/invalid_method_declaration_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package java

import (
"fmt"
"strings"

lib "github.com/nedpals/errgoengine"
)
Expand All @@ -20,20 +19,16 @@ var InvalidMethodDeclarationError = lib.ErrorTemplate{
iCtx := invalidMethodDeclarationErrorCtx{}
pos := m.ErrorNode.StartPos

lib.QueryNode(m.Document.RootNode(), strings.NewReader("(constructor_declaration) @method"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
pointA := c.Node.StartPoint()
pointB := c.Node.EndPoint()
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
iCtx.declNode = node
m.Nearest = node.ChildByFieldName("name")
return false
}
for q := m.Document.RootNode().Query("(constructor_declaration) @method"); q.Next(); {
node := q.CurrentNode()
pointA := node.StartPoint()
pointB := node.EndPoint()
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
iCtx.declNode = node
m.Nearest = node.ChildByFieldName("name")
break
}
return true
})
}

iCtx.returnTypeToAdd = lib.UnwrapReturnType(cd.FindSymbol(m.Nearest.Text(), m.Nearest.StartPosition().Index))
m.Context = iCtx
Expand Down
23 changes: 10 additions & 13 deletions error_templates/java/missing_return_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package java
import (
"context"
"fmt"
"strings"

lib "github.com/nedpals/errgoengine"
)
Expand All @@ -21,19 +20,17 @@ var MissingReturnError = lib.ErrorTemplate{
mCtx := missingReturnErrorCtx{}
rootNode := m.Document.RootNode()
pos := m.ErrorNode.StartPos
lib.QueryNode(rootNode, strings.NewReader("(method_declaration) @method"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
pointA := c.Node.StartPoint()
pointB := c.Node.EndPoint()
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
mCtx.NearestMethod = node
return false
}

for q := rootNode.Query("(method_declaration) @method"); q.Next(); {
node := q.CurrentNode()
pointA := node.StartPoint()
pointB := node.EndPoint()
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
mCtx.NearestMethod = node
break
}
return true
})
}

m.Context = mCtx
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
Expand Down
19 changes: 7 additions & 12 deletions error_templates/java/negative_array_size_exception.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package java

import (
"strings"

lib "github.com/nedpals/errgoengine"
)

Expand All @@ -16,16 +14,13 @@ var NegativeArraySizeException = lib.ErrorTemplate{
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
nCtx := negativeArraySizeExceptionCtx{}
query := "(array_creation_expression dimensions: (dimensions_expr (unary_expression operand: (decimal_integer_literal)))) @array"
lib.QueryNode(m.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
nCtx.ArrayExprNode = node
m.Nearest = node.ChildByFieldName("dimensions").NamedChild(0)
return false
}
return true
})
for q := m.Nearest.Query(query); q.Next(); {
node := q.CurrentNode()
nCtx.ArrayExprNode = node
m.Nearest = node.ChildByFieldName("dimensions").NamedChild(0)
break
}

m.Context = nCtx
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
Expand Down
19 changes: 7 additions & 12 deletions error_templates/java/non_static_method_access_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package java

import (
"fmt"
"strings"

lib "github.com/nedpals/errgoengine"
)
Expand All @@ -29,18 +28,14 @@ var NonStaticMethodAccessError = lib.ErrorTemplate{
}
}

m.Context = nCtx
for q := m.Nearest.Query(`(method_invocation name: (identifier) @method arguments: (argument_list))`); q.Next(); {
node := q.CurrentNode()
m.Nearest = node
nCtx.method = node.Text()
break
}

lib.QueryNode(m.Nearest, strings.NewReader("(method_invocation name: (identifier) @method arguments: (argument_list))"), func(ctx lib.QueryNodeCtx) bool {
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
for _, c := range match.Captures {
node := lib.WrapNode(m.Nearest.Doc, c.Node)
m.Nearest = node
nCtx.method = node.Text()
return false
}
return true
})
m.Context = nCtx
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
gen.Add("This error occurs when trying to access a non-static method from a static context. In Java, a non-static method belongs to an instance of the class and needs an object to be called upon.")
Expand Down
6 changes: 6 additions & 0 deletions error_templates/java/null_pointer_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ var NullPointerException = lib.ErrorTemplate{
gen.Add("Your program try to access or manipulate an object reference that is currently pointing to `null`, meaning it doesn't refer to any actual object in memory. This typically happens when you forget to initialize an object before using it, or when you try to access an object that hasn't been properly assigned a value. ")
},
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
gen.Add("Wrap with an if statement", func(s *lib.BugFixSuggestion) {
s.AddDescription("Check for the variable that is being used as `null`.")
})

gen.Add("Initialize the variable", func(s *lib.BugFixSuggestion) {
s.AddDescription("An alternative fix is to initialize the `test` variable with a non-null value before calling the method.")
})
},
}
Loading

0 comments on commit 2f848c1

Please sign in to comment.