diff --git a/decoder/expr_any_ref_origins.go b/decoder/expr_any_ref_origins.go index e31433da..57a15df8 100644 --- a/decoder/expr_any_ref_origins.go +++ b/decoder/expr_any_ref_origins.go @@ -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 @@ -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{ @@ -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 +} diff --git a/decoder/expr_any_ref_origins_test.go b/decoder/expr_any_ref_origins_test.go index cb3881bb..6a883320 100644 --- a/decoder/expr_any_ref_origins_test.go +++ b/decoder/expr_any_ref_origins_test.go @@ -61,7 +61,7 @@ func TestCollectRefOrigins_exprAny_references_hcl(t *testing.T) { }, Constraints: reference.OriginConstraints{ { - OfType: cty.DynamicPseudoType, + OfType: cty.String, }, }, }, @@ -90,7 +90,7 @@ func TestCollectRefOrigins_exprAny_references_hcl(t *testing.T) { }, Constraints: reference.OriginConstraints{ { - OfType: cty.DynamicPseudoType, + OfType: cty.String, }, }, }, @@ -508,7 +508,7 @@ func TestCollectRefOrigins_exprAny_functions_hcl(t *testing.T) { }, Constraints: reference.OriginConstraints{ { - OfType: cty.DynamicPseudoType, + OfType: cty.String, }, }, }, diff --git a/decoder/reference_origins_collect_hcl_test.go b/decoder/reference_origins_collect_hcl_test.go index 3516e621..ab470356 100644 --- a/decoder/reference_origins_collect_hcl_test.go +++ b/decoder/reference_origins_collect_hcl_test.go @@ -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", @@ -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", @@ -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",