Skip to content

Commit

Permalink
decoder: Implement reference collection for template expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Sep 29, 2023
1 parent 8059773 commit e940874
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
42 changes: 41 additions & 1 deletion decoder/expr_any_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
}

func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO: Support TemplateExpr https://github.com/hashicorp/terraform-ls/issues/522
// TODO: Support splat expression https://github.com/hashicorp/terraform-ls/issues/526
// TODO: Support for-in-if expression https://github.com/hashicorp/terraform-ls/issues/527
// TODO: Support conditional expression https://github.com/hashicorp/terraform-ls/issues/528
Expand All @@ -126,6 +125,10 @@ func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool
return origins
}

if origins, ok := a.refOriginsForTemplateExpr(ctx, allowSelfRefs); ok {
return origins
}

// attempt to get accurate constraint for the origins
// if we recognise the given expression
funcExpr := functionExpr{
Expand Down Expand Up @@ -240,3 +243,40 @@ func (a Any) refOriginsForOperatorExpr(ctx context.Context, allowSelfRefs bool)

return origins, false
}

func (a Any) refOriginsForTemplateExpr(ctx context.Context, allowSelfRefs bool) (reference.Origins, bool) {
origins := make(reference.Origins, 0)

switch eType := a.expr.(type) {
case *hclsyntax.TemplateExpr:
if eType.IsStringLiteral() {
return nil, false
}

for _, partExpr := range eType.Parts {
cons := schema.AnyExpression{
OfType: cty.String,
}
expr := newExpression(a.pathCtx, partExpr, cons)

if e, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, e.ReferenceOrigins(ctx, allowSelfRefs)...)
}
}

return origins, true
case *hclsyntax.TemplateWrapExpr:
cons := schema.AnyExpression{
OfType: cty.String,
}
expr := newExpression(a.pathCtx, eType.Wrapped, cons)

if e, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, e.ReferenceOrigins(ctx, allowSelfRefs)...)
}

return origins, true
}

return origins, false
}
6 changes: 3 additions & 3 deletions decoder/expr_any_ref_origins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestCollectRefOrigins_exprAny_references_hcl(t *testing.T) {
},
Constraints: reference.OriginConstraints{
{
OfType: cty.DynamicPseudoType,
OfType: cty.String,
},
},
},
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCollectRefOrigins_exprAny_references_hcl(t *testing.T) {
},
Constraints: reference.OriginConstraints{
{
OfType: cty.DynamicPseudoType,
OfType: cty.String,
},
},
},
Expand Down Expand Up @@ -508,7 +508,7 @@ func TestCollectRefOrigins_exprAny_functions_hcl(t *testing.T) {
},
Constraints: reference.OriginConstraints{
{
OfType: cty.DynamicPseudoType,
OfType: cty.String,
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions decoder/reference_origins_collect_hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ attr3 = onestep`,
lang.RootStep{Name: "onestep"},
},
Constraints: reference.OriginConstraints{
{OfType: cty.DynamicPseudoType},
{OfType: cty.String},
},
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -183,7 +183,7 @@ attr3 = onestep`,
lang.RootStep{Name: "onestep"},
},
Constraints: reference.OriginConstraints{
{OfType: cty.DynamicPseudoType},
{OfType: cty.String},
},
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -206,7 +206,7 @@ attr3 = onestep`,
lang.AttrStep{Name: "bar"},
},
Constraints: reference.OriginConstraints{
{OfType: cty.DynamicPseudoType},
{OfType: cty.String},
},
Range: hcl.Range{
Filename: "test.tf",
Expand Down

0 comments on commit e940874

Please sign in to comment.