Skip to content

Commit c33dc2d

Browse files
authoredMay 16, 2025··
Support builder pattern with imports (#203)
* Support builder pattern with imports Weird case that we use at Grafana Labs. Lots of indirection with imports in the mix * Fix errors
1 parent c9d1485 commit c33dc2d

File tree

1,253 files changed

+125710
-58115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,253 files changed

+125710
-58115
lines changed
 

‎pkg/ast/processing/find_field.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ func (p *Processor) extractObjectRangesFromDesugaredObjs(desugaredObjs []*ast.De
117117
case *ast.Apply:
118118
// Add the target of the Apply to the list of field nodes to look for
119119
// The target is a function and will be found by FindVarReference on the next loop
120+
if idx, ok := fieldNode.Target.(*ast.Index); ok { // Builder pattern, run the function within the index
121+
if importNode, ok := idx.Target.(*ast.Import); ok {
122+
// If the index is a builder pattern, we need to run the function within the index
123+
// We need to import the file first
124+
objs := p.FindTopLevelObjectsInFile(importNode.File.Value, string(importNode.Loc().File.DiagnosticFileName))
125+
for _, obj := range objs {
126+
fieldString, ok := idx.Index.(*ast.LiteralString)
127+
if !ok {
128+
continue
129+
}
130+
if idxField := findObjectFieldsInObject(obj, fieldString.Value, false); len(idxField) > 0 {
131+
fieldNodes = append(fieldNodes, idxField[0].Body)
132+
}
133+
}
134+
i++
135+
continue
136+
}
137+
}
120138
fieldNodes = append(fieldNodes, fieldNode.Target)
121139
case *ast.Var:
122140
varReference, err := p.FindVarReference(fieldNode)

‎pkg/server/definition_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -929,17 +929,17 @@ var definitionTestCases = []definitionTestCase{
929929
},
930930
{
931931
name: "grafonnet: eval variable selection options",
932-
filename: "./testdata/grafonnet/eval-variable-options.jsonnet",
932+
filename: "./testdata/grafonnet-eval-variable-options.jsonnet",
933933
position: protocol.Position{Line: 5, Character: 54},
934934
results: []definitionResult{{
935-
targetFilename: "testdata/grafonnet/vendor/github.com/grafana/grafonnet/gen/grafonnet-v10.0.0/custom/dashboard/variable.libsonnet",
935+
targetFilename: "testdata/vendor/github.com/grafana/grafonnet/gen/grafonnet-v11.4.0/custom/dashboard/variable.libsonnet",
936936
targetRange: protocol.Range{
937-
Start: protocol.Position{Line: 101, Character: 12},
938-
End: protocol.Position{Line: 103, Character: 13},
937+
Start: protocol.Position{Line: 122, Character: 12},
938+
End: protocol.Position{Line: 124, Character: 13},
939939
},
940940
targetSelectionRange: protocol.Range{
941-
Start: protocol.Position{Line: 101, Character: 12},
942-
End: protocol.Position{Line: 101, Character: 21},
941+
Start: protocol.Position{Line: 122, Character: 12},
942+
End: protocol.Position{Line: 122, Character: 21},
943943
},
944944
}},
945945
},
@@ -988,6 +988,22 @@ var definitionTestCases = []definitionTestCase{
988988
},
989989
}},
990990
},
991+
{
992+
name: "goto ksonnet util",
993+
filename: "testdata/use-ksonnet-util.jsonnet",
994+
position: protocol.Position{Line: 11, Character: 30},
995+
results: []definitionResult{{
996+
targetFilename: "testdata/vendor/ksonnet-util/util.libsonnet",
997+
targetRange: protocol.Range{
998+
Start: protocol.Position{Line: 243, Character: 2},
999+
End: protocol.Position{Line: 251, Character: 5},
1000+
},
1001+
targetSelectionRange: protocol.Range{
1002+
Start: protocol.Position{Line: 243, Character: 2},
1003+
End: protocol.Position{Line: 243, Character: 24},
1004+
},
1005+
}},
1006+
},
9911007
}
9921008

9931009
func TestDefinition(t *testing.T) {

0 commit comments

Comments
 (0)
Please sign in to comment.