@@ -41,6 +41,11 @@ type FileData struct {
41
41
Functions []c.Function
42
42
}
43
43
44
+ type FunctionAnnotationNode struct {
45
+ Node * sitter.Node
46
+ Function c.FunctionAnnotation
47
+ }
48
+
44
49
type hasNoTests bool
45
50
46
51
func ExtractInfo (file c.TestFile , ctx context.Context , fileID c.FileID ) (* FileData , hasNoTests , error ) {
@@ -98,7 +103,7 @@ func GetExportedFunctions(ctx context.Context, filePath string) ([]c.FunctionAnn
98
103
}
99
104
100
105
cursor := sitter .NewTreeCursor (tree .RootNode ())
101
- fnsAnno , err := parseContentForPublicFunctions (content , cursor )
106
+ fnsAnno , err := parseContentForFunctions (content , cursor )
102
107
if err != nil {
103
108
return nil , err
104
109
}
@@ -130,15 +135,15 @@ func parseContent(content string, treeCursor *sitter.TreeCursor, filePath string
130
135
fileData .Metadata = getMetadata (content , treeCursor , & annotationParser )
131
136
funcNodes := getFunctionNodes (content , treeCursor , & annotationParser )
132
137
133
- for _ , function := range funcNodes {
138
+ for _ , funcNode := range funcNodes {
134
139
135
- behaviors := findBehaviorsFromNode (content , function .Node )
140
+ behaviors := findBehaviorsFromNode (content , funcNode .Node )
136
141
var callExpressions []string = nil
137
142
if len (behaviors ) > 0 {
138
- callExpressions = findCallExprFromNode (content , function .Node )
143
+ callExpressions = findCallExprFromNode (content , funcNode .Node )
139
144
}
140
145
141
- fileData .Functions = append (fileData .Functions , makeCollectorScenario (filePath , function .Function .Name , behaviors , callExpressions ))
146
+ fileData .Functions = append (fileData .Functions , makeCollectorScenario (filePath , funcNode .Function .Name , behaviors , callExpressions ))
142
147
143
148
}
144
149
@@ -188,13 +193,11 @@ func getMetadata(content string, treeCursor *sitter.TreeCursor, parser *a.Parser
188
193
return & meta
189
194
}
190
195
191
- // this method extracts functions and methods.
196
+ // getFunctionNodes method extracts functions and methods.
192
197
// Function can have 2 types: function_declaration (for example contructor)
193
- // and method_declaration (can be exported and unexported)
194
- func getFunctionNodes (content string , treeCursor * sitter.TreeCursor , parser * a.Parser ) (funcAnnoPair []struct {
195
- Node * sitter.Node
196
- Function c.FunctionAnnotation
197
- }) {
198
+ // and method_declaration (can be exported and unexported).
199
+ // Return value is a slice of FunctionAnnotationNode, where each node holds function's annotation.
200
+ func getFunctionNodes (content string , treeCursor * sitter.TreeCursor , parser * a.Parser ) (funcAnnoPair []FunctionAnnotationNode ) {
198
201
199
202
numChildsRootNode := treeCursor .CurrentNode ().ChildCount ()
200
203
node := & sitter.Node {}
@@ -231,10 +234,7 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
231
234
continue
232
235
}
233
236
234
- funcAnnoPair = append (funcAnnoPair , struct {
235
- Node * sitter.Node
236
- Function c.FunctionAnnotation
237
- }{
237
+ funcAnnoPair = append (funcAnnoPair , FunctionAnnotationNode {
238
238
Node : node ,
239
239
Function : c.FunctionAnnotation {
240
240
Name : funcName ,
@@ -245,6 +245,23 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
245
245
funcName = ""
246
246
isIgnored = false
247
247
}
248
+ /*
249
+ Take a look at this function for example:
250
+ *********
251
+ func (e *ProofEventStream) ListenProofEvent(
252
+ ctx context.Context,
253
+ policy *types2.ProofRegisterPolicy)
254
+ (<-chan *types2.RequestEvent, error){...}
255
+ *********
256
+ First iteration through children nodes:
257
+ Child.Type = method_declaration
258
+ Child.Value = whole function body func(*x)(a,b)c{...}
259
+
260
+ child.Child(1) is a first part of function declaration, and has type of parameter_list: (e *ProofEventStream)
261
+ child.Child(2): has type of field_identifier and represents function's name: ListenProofEvent
262
+ child.Child(3): has type parameter_list and represents input parameters: (ctx context.Context, policy *types2.ProofRegisterPolicy)
263
+ child.Child(4): has type parameter_list and represent return parameters: (<-chan *types2.RequestEvent, error)
264
+ */
248
265
if child .Type () == string (METHOD_DECLARATION ) {
249
266
funcName = content [child .Child (2 ).StartByte ():child .Child (2 ).EndByte ()]
250
267
public := unicode .IsUpper (rune (funcName [0 ]))
@@ -254,10 +271,7 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
254
271
params = content [child .Child (3 ).StartByte ():child .Child (3 ).EndByte ()]
255
272
returnValues = content [child .Child (4 ).StartByte ():child .Child (4 ).EndByte ()]
256
273
}
257
- funcAnnoPair = append (funcAnnoPair , struct {
258
- Node * sitter.Node
259
- Function c.FunctionAnnotation
260
- }{
274
+ funcAnnoPair = append (funcAnnoPair , FunctionAnnotationNode {
261
275
Node : child ,
262
276
Function : c.FunctionAnnotation {
263
277
Name : funcName ,
@@ -354,13 +368,15 @@ func makeID(filePath string, funcName string, behavior string) string {
354
368
return string (hex .EncodeToString (hash [:]))
355
369
}
356
370
357
- func parseContentForPublicFunctions (content string , cursor * sitter.TreeCursor ) ([]c.FunctionAnnotation , error ) {
371
+ // parseContentForFunctions accepts a content which represents whole golang file as a string,
372
+ // parses it and returns a slice of function annotations (including exported and unexported ones).
373
+ func parseContentForFunctions (content string , cursor * sitter.TreeCursor ) ([]c.FunctionAnnotation , error ) {
358
374
var annotationParser a.Parser
359
375
360
376
var fnsAnno []c.FunctionAnnotation
361
- functions := getFunctionNodes (content , cursor , & annotationParser )
362
- for _ , f := range functions {
363
- fnsAnno = append (fnsAnno , f .Function )
377
+ funcNodes := getFunctionNodes (content , cursor , & annotationParser )
378
+ for _ , funcNode := range funcNodes {
379
+ fnsAnno = append (fnsAnno , funcNode .Function )
364
380
}
365
381
366
382
return fnsAnno , nil
0 commit comments