Skip to content

Commit 2f848c1

Browse files
committed
refactor: simplify QueryNode usage; use iterator approach
1 parent b150fa5 commit 2f848c1

26 files changed

+302
-319
lines changed

error_templates/java/already_defined_error.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,21 @@ var AlreadyDefinedError = lib.ErrorTemplate{
2424
pos := m.ErrorNode.StartPos
2525

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

4137
// get the nearest method declaration based on symbol signature
42-
lib.QueryNode(aCtx.NearestClass, strings.NewReader(rawQuery), func(ctx lib.QueryNodeCtx) bool {
43-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
44-
for _, c := range match.Captures {
45-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
46-
aCtx.NearestMethod = node
47-
return false
48-
}
49-
return true
50-
})
38+
for q := aCtx.NearestClass.Query(rawQuery); q.Next(); {
39+
aCtx.NearestMethod = q.CurrentNode()
40+
break
41+
}
5142

5243
m.Context = aCtx
5344
},

error_templates/java/arithmetic_exception.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package java
22

33
import (
4-
"strings"
5-
64
lib "github.com/nedpals/errgoengine"
75
)
86

@@ -38,15 +36,10 @@ var ArithmeticException = lib.ErrorTemplate{
3836
}
3937

4038
if len(query) != 0 {
41-
lib.QueryNode(cd.MainError.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
42-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(cd.MainError.Nearest.Doc.Contents))
43-
for _, c := range match.Captures {
44-
node := lib.WrapNode(cd.MainError.Nearest.Doc, c.Node)
45-
err.Nearest = node
46-
return false
47-
}
48-
return true
49-
})
39+
for q := err.Nearest.Query(query); q.Next(); {
40+
err.Nearest = q.CurrentNode()
41+
break
42+
}
5043
}
5144

5245
err.Context = ctx

error_templates/java/array_index_out_of_bounds_exception.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package java
33
import (
44
"fmt"
55
"strconv"
6-
"strings"
76

87
lib "github.com/nedpals/errgoengine"
98
)
@@ -12,15 +11,11 @@ var ArrayIndexOutOfBoundsException = lib.ErrorTemplate{
1211
Name: "ArrayIndexOutOfBoundsException",
1312
Pattern: runtimeErrorPattern("java.lang.ArrayIndexOutOfBoundsException", `Index (?P<index>\d+) out of bounds for length (?P<length>\d+)`),
1413
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
15-
lib.QueryNode(m.Nearest, strings.NewReader("(array_access index: (_) @index)"), func(ctx lib.QueryNodeCtx) bool {
16-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
17-
for _, c := range match.Captures {
18-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
19-
m.Nearest = node
20-
return false
21-
}
22-
return true
23-
})
14+
for q := m.Nearest.Query(`(array_access index: (_) @index)`); q.Next(); {
15+
node := q.CurrentNode()
16+
m.Nearest = node
17+
break
18+
}
2419
},
2520
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
2621
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"])

error_templates/java/array_required_type_error.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package java
22

33
import (
44
"fmt"
5-
"strings"
65

76
lib "github.com/nedpals/errgoengine"
87
)
@@ -12,16 +11,10 @@ var ArrayRequiredTypeError = lib.ErrorTemplate{
1211
Pattern: comptimeErrorPattern(`array required, but (?P<foundType>\S+) found`),
1312
StackTracePattern: comptimeStackTracePattern,
1413
OnAnalyzeErrorFn: func(cd *lib.ContextData, err *lib.MainError) {
15-
query := strings.NewReader("(array_access array: (identifier) index: ((_) @index (#eq? @index \"0\")))")
16-
lib.QueryNode(cd.MainError.Nearest, query, func(ctx lib.QueryNodeCtx) bool {
17-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(cd.MainError.Nearest.Doc.Contents))
18-
for _, c := range match.Captures {
19-
node := lib.WrapNode(cd.MainError.Nearest.Doc, c.Node)
20-
err.Nearest = node
21-
return false
22-
}
23-
return true
24-
})
14+
for q := cd.MainError.Nearest.Query("(array_access array: (identifier) index: ((_) @index (#eq? @index \"0\")))"); q.Next(); {
15+
err.Nearest = q.CurrentNode()
16+
break
17+
}
2518
},
2619
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
2720
parent := cd.MainError.Nearest.Parent()

error_templates/java/bracket_mismatch_error.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package java
33
import (
44
"fmt"
55
"strconv"
6-
"strings"
76

87
lib "github.com/nedpals/errgoengine"
98
)
@@ -12,15 +11,11 @@ var BracketMismatchError = lib.ErrorTemplate{
1211
Name: "ArrayIndexOutOfBoundsException",
1312
Pattern: comptimeErrorPattern(`'(?P<expected>\S+)' expected`),
1413
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
15-
lib.QueryNode(m.Nearest, strings.NewReader("(array_access index: (_) @index)"), func(ctx lib.QueryNodeCtx) bool {
16-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
17-
for _, c := range match.Captures {
18-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
19-
m.Nearest = node
20-
return false
21-
}
22-
return true
23-
})
14+
for q := m.Nearest.Query(`(array_access index: (_) @index)`); q.Next(); {
15+
node := q.CurrentNode()
16+
m.Nearest = node
17+
break
18+
}
2419
},
2520
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
2621
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"])

error_templates/java/cannot_be_applied_error.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,15 @@ var CannotBeAppliedError = lib.ErrorTemplate{
8585
argumentNodeTypesToLook += nTypesStr
8686
}
8787

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

10398
m.Context = cCtx
10499
},

error_templates/java/illegal_expression_start_error.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package java
22

33
import (
4-
"strings"
5-
64
lib "github.com/nedpals/errgoengine"
75
)
86

@@ -11,16 +9,12 @@ var IllegalExpressionStartError = lib.ErrorTemplate{
119
Pattern: comptimeErrorPattern(`illegal start of expression`),
1210
StackTracePattern: comptimeStackTracePattern,
1311
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
14-
lib.QueryNode(m.Nearest, strings.NewReader("(ERROR) @error"), func(ctx lib.QueryNodeCtx) bool {
15-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
16-
for _, c := range match.Captures {
17-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
18-
m.Nearest = node
19-
// aCtx.NearestClass = node
20-
return false
21-
}
22-
return true
23-
})
12+
for q := m.Nearest.Query("(ERROR) @error"); q.Next(); {
13+
node := q.CurrentNode()
14+
m.Nearest = node
15+
// aCtx.NearestClass = node
16+
break
17+
}
2418
},
2519
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
2620
gen.Add("This error occurs when the compiler encounters an expression that is not valid.")

error_templates/java/invalid_method_declaration_error.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package java
22

33
import (
44
"fmt"
5-
"strings"
65

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

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-
}
22+
for q := m.Document.RootNode().Query("(constructor_declaration) @method"); q.Next(); {
23+
node := q.CurrentNode()
24+
pointA := node.StartPoint()
25+
pointB := node.EndPoint()
26+
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
27+
iCtx.declNode = node
28+
m.Nearest = node.ChildByFieldName("name")
29+
break
3430
}
35-
return true
36-
})
31+
}
3732

3833
iCtx.returnTypeToAdd = lib.UnwrapReturnType(cd.FindSymbol(m.Nearest.Text(), m.Nearest.StartPosition().Index))
3934
m.Context = iCtx

error_templates/java/missing_return_error.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package java
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
lib "github.com/nedpals/errgoengine"
98
)
@@ -21,19 +20,17 @@ var MissingReturnError = lib.ErrorTemplate{
2120
mCtx := missingReturnErrorCtx{}
2221
rootNode := m.Document.RootNode()
2322
pos := m.ErrorNode.StartPos
24-
lib.QueryNode(rootNode, strings.NewReader("(method_declaration) @method"), func(ctx lib.QueryNodeCtx) bool {
25-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
26-
for _, c := range match.Captures {
27-
pointA := c.Node.StartPoint()
28-
pointB := c.Node.EndPoint()
29-
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
30-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
31-
mCtx.NearestMethod = node
32-
return false
33-
}
23+
24+
for q := rootNode.Query("(method_declaration) @method"); q.Next(); {
25+
node := q.CurrentNode()
26+
pointA := node.StartPoint()
27+
pointB := node.EndPoint()
28+
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
29+
mCtx.NearestMethod = node
30+
break
3431
}
35-
return true
36-
})
32+
}
33+
3734
m.Context = mCtx
3835
},
3936
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {

error_templates/java/negative_array_size_exception.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package java
22

33
import (
4-
"strings"
5-
64
lib "github.com/nedpals/errgoengine"
75
)
86

@@ -16,16 +14,13 @@ var NegativeArraySizeException = lib.ErrorTemplate{
1614
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
1715
nCtx := negativeArraySizeExceptionCtx{}
1816
query := "(array_creation_expression dimensions: (dimensions_expr (unary_expression operand: (decimal_integer_literal)))) @array"
19-
lib.QueryNode(m.Nearest, strings.NewReader(query), func(ctx lib.QueryNodeCtx) bool {
20-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
21-
for _, c := range match.Captures {
22-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
23-
nCtx.ArrayExprNode = node
24-
m.Nearest = node.ChildByFieldName("dimensions").NamedChild(0)
25-
return false
26-
}
27-
return true
28-
})
17+
for q := m.Nearest.Query(query); q.Next(); {
18+
node := q.CurrentNode()
19+
nCtx.ArrayExprNode = node
20+
m.Nearest = node.ChildByFieldName("dimensions").NamedChild(0)
21+
break
22+
}
23+
2924
m.Context = nCtx
3025
},
3126
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {

error_templates/java/non_static_method_access_error.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package java
22

33
import (
44
"fmt"
5-
"strings"
65

76
lib "github.com/nedpals/errgoengine"
87
)
@@ -29,18 +28,14 @@ var NonStaticMethodAccessError = lib.ErrorTemplate{
2928
}
3029
}
3130

32-
m.Context = nCtx
31+
for q := m.Nearest.Query(`(method_invocation name: (identifier) @method arguments: (argument_list))`); q.Next(); {
32+
node := q.CurrentNode()
33+
m.Nearest = node
34+
nCtx.method = node.Text()
35+
break
36+
}
3337

34-
lib.QueryNode(m.Nearest, strings.NewReader("(method_invocation name: (identifier) @method arguments: (argument_list))"), func(ctx lib.QueryNodeCtx) bool {
35-
match := ctx.Cursor.FilterPredicates(ctx.Match, []byte(m.Nearest.Doc.Contents))
36-
for _, c := range match.Captures {
37-
node := lib.WrapNode(m.Nearest.Doc, c.Node)
38-
m.Nearest = node
39-
nCtx.method = node.Text()
40-
return false
41-
}
42-
return true
43-
})
38+
m.Context = nCtx
4439
},
4540
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
4641
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.")

error_templates/java/null_pointer_exception.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ var NullPointerException = lib.ErrorTemplate{
107107
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. ")
108108
},
109109
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
110+
gen.Add("Wrap with an if statement", func(s *lib.BugFixSuggestion) {
111+
s.AddDescription("Check for the variable that is being used as `null`.")
112+
})
110113

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

0 commit comments

Comments
 (0)