@@ -119,10 +119,25 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
119
119
return ranges , nil
120
120
}
121
121
122
- // Unpack binary nodes. A field could be either in the left or right side of the binary
122
+ // Unpack:
123
+ // - Binary nodes. A field could be either in the left or right side of the binary
124
+ // - Self nodes. We want the object self refers to, not the self node itself
123
125
var fieldNodes []ast.Node
124
126
for _ , foundField := range foundFields {
125
127
switch fieldNode := foundField .Body .(type ) {
128
+ case * ast.Self :
129
+ filename := fieldNode .LocRange .FileName
130
+ rootNode , _ , _ := vm .ImportAST ("" , filename )
131
+ tmpStack , err := FindNodeByPosition (rootNode , fieldNode .LocRange .Begin )
132
+ if err != nil {
133
+ return nil , err
134
+ }
135
+ for ! tmpStack .IsEmpty () {
136
+ _ , node := tmpStack .Pop ()
137
+ if _ , ok := node .(* ast.DesugaredObject ); ok {
138
+ fieldNodes = append (fieldNodes , node )
139
+ }
140
+ }
126
141
case * ast.Binary :
127
142
fieldNodes = append (fieldNodes , fieldNode .Right )
128
143
fieldNodes = append (fieldNodes , fieldNode .Left )
@@ -134,18 +149,11 @@ func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm
134
149
for _ , fieldNode := range fieldNodes {
135
150
switch fieldNode := fieldNode .(type ) {
136
151
case * ast.Var :
137
- // If the field is a var, we need to find the value of the var
138
- // To do so, we get the stack where the var is used and search that stack for the var's definition
139
- varFileNode , _ , _ := vm .ImportAST ("" , fieldNode .LocRange .FileName )
140
- varStack , err := FindNodeByPosition (varFileNode , fieldNode .Loc ().Begin )
152
+ varReference , err := findVarReference (fieldNode , vm )
141
153
if err != nil {
142
- return nil , fmt . Errorf ( "got the following error when finding the bind for %s: %w" , fieldNode . Id , err )
154
+ return nil , err
143
155
}
144
- bind := FindBindByIdViaStack (varStack , fieldNode .Id )
145
- if bind == nil {
146
- return nil , fmt .Errorf ("could not find bind for %s" , fieldNode .Id )
147
- }
148
- foundDesugaredObjects = append (foundDesugaredObjects , bind .Body .(* ast.DesugaredObject ))
156
+ foundDesugaredObjects = append (foundDesugaredObjects , varReference .(* ast.DesugaredObject ))
149
157
case * ast.DesugaredObject :
150
158
stack = stack .Push (fieldNode )
151
159
foundDesugaredObjects = append (foundDesugaredObjects , findDesugaredObjectFromStack (stack ))
@@ -237,11 +245,33 @@ func findTopLevelObjects(stack *nodestack.NodeStack, vm *jsonnet.VM) []*ast.Desu
237
245
stack .Push (obj .Body )
238
246
}
239
247
}
248
+ case * ast.Var :
249
+ varReference , err := findVarReference (curr , vm )
250
+ if err != nil {
251
+ log .WithError (err ).Errorf ("Error finding var reference, ignoring this node" )
252
+ continue
253
+ }
254
+ stack .Push (varReference )
240
255
}
241
256
}
242
257
return objects
243
258
}
244
259
260
+ // findVarReference finds the object that the variable is referencing
261
+ // To do so, we get the stack where the var is used and search that stack for the var's definition
262
+ func findVarReference (varNode * ast.Var , vm * jsonnet.VM ) (ast.Node , error ) {
263
+ varFileNode , _ , _ := vm .ImportAST ("" , varNode .LocRange .FileName )
264
+ varStack , err := FindNodeByPosition (varFileNode , varNode .Loc ().Begin )
265
+ if err != nil {
266
+ return nil , fmt .Errorf ("got the following error when finding the bind for %s: %w" , varNode .Id , err )
267
+ }
268
+ bind := FindBindByIdViaStack (varStack , varNode .Id )
269
+ if bind == nil {
270
+ return nil , fmt .Errorf ("could not find bind for %s" , varNode .Id )
271
+ }
272
+ return bind .Body , nil
273
+ }
274
+
245
275
func findLhsDesugaredObject (stack * nodestack.NodeStack ) (* ast.DesugaredObject , error ) {
246
276
for ! stack .IsEmpty () {
247
277
_ , curr := stack .Pop ()
0 commit comments