Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder: refactor SelfRefs use context #394

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions decoder/expr_any_conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (a Any) hoverConditionalExprAtPos(ctx context.Context, pos hcl.Pos) (*lang.
return nil, false
}

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

// There is currently no way of decoding conditional expressions in JSON
Expand All @@ -84,23 +84,23 @@ func (a Any) refOriginsForConditionalExpr(ctx context.Context, allowSelfRefs boo
OfType: cty.Bool,
})
if expr, ok := condExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

trueExpr := newExpression(a.pathCtx, eType.TrueResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
})
if expr, ok := trueExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

falseExpr := newExpression(a.pathCtx, eType.FalseResult, schema.AnyExpression{
OfType: a.cons.OfType,
SkipLiteralComplexTypes: a.cons.SkipLiteralComplexTypes,
})
if expr, ok := falseExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

return origins, true
Expand Down
10 changes: 5 additions & 5 deletions decoder/expr_any_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (a Any) semanticTokensForForExpr(ctx context.Context) ([]lang.SemanticToken
return tokens, false
}

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

// There is currently no way of decoding for expressions in JSON
Expand All @@ -184,7 +184,7 @@ func (a Any) refOriginsForForExpr(ctx context.Context, allowSelfRefs bool) (refe
schema.AnyExpression{OfType: cty.EmptyObject},
}
if collExpr, ok := newExpression(a.pathCtx, eType.CollExpr, collCons).(ReferenceOriginsExpression); ok {
origins = append(origins, collExpr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, collExpr.ReferenceOrigins(ctx)...)
}

if eType.KeyExpr != nil {
Expand All @@ -196,7 +196,7 @@ func (a Any) refOriginsForForExpr(ctx context.Context, allowSelfRefs bool) (refe
OfType: typ,
}
if keyExpr, ok := newExpression(a.pathCtx, eType.KeyExpr, cons).(ReferenceOriginsExpression); ok {
origins = append(origins, keyExpr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, keyExpr.ReferenceOrigins(ctx)...)
}
}

Expand All @@ -208,7 +208,7 @@ func (a Any) refOriginsForForExpr(ctx context.Context, allowSelfRefs bool) (refe
OfType: typ,
}
if valExpr, ok := newExpression(a.pathCtx, eType.ValExpr, cons).(ReferenceOriginsExpression); ok {
origins = append(origins, valExpr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, valExpr.ReferenceOrigins(ctx)...)
}

if eType.CondExpr != nil {
Expand All @@ -217,7 +217,7 @@ func (a Any) refOriginsForForExpr(ctx context.Context, allowSelfRefs bool) (refe
}

if condExpr, ok := newExpression(a.pathCtx, eType.CondExpr, cons).(ReferenceOriginsExpression); ok {
origins = append(origins, condExpr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, condExpr.ReferenceOrigins(ctx)...)
}
}

Expand Down
10 changes: 5 additions & 5 deletions decoder/expr_any_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (a Any) hoverOperatorExprAtPos(ctx context.Context, pos hcl.Pos) (*lang.Hov
return nil, false
}

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

// There is currently no way of decoding operator expressions in JSON
Expand All @@ -186,14 +186,14 @@ func (a Any) refOriginsForOperatorExpr(ctx context.Context, allowSelfRefs bool)
OfType: opFuncParams[0].Type,
})
if expr, ok := leftExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

rightExpr := newExpression(a.pathCtx, eType.RHS, schema.AnyExpression{
OfType: opFuncParams[1].Type,
})
if expr, ok := rightExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

return origins, true
Expand All @@ -216,14 +216,14 @@ func (a Any) refOriginsForOperatorExpr(ctx context.Context, allowSelfRefs bool)
OfType: opFuncParams[0].Type,
})
if expr, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

return origins, true
case *hclsyntax.ParenthesesExpr:
expr := newExpression(a.pathCtx, eType.Expression, a.cons)
if expr, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

return origins, true
Expand Down
37 changes: 19 additions & 18 deletions decoder/expr_any_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"github.com/zclconf/go-cty/cty"
)

func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (a Any) ReferenceOrigins(ctx context.Context) reference.Origins {
typ := a.cons.OfType

if typ.IsListType() {
_, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.refOriginsForNonComplexExpr(ctx, allowSelfRefs)
return a.refOriginsForNonComplexExpr(ctx)
}

list := List{
Expand All @@ -31,13 +31,13 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
},
},
}
return list.ReferenceOrigins(ctx, allowSelfRefs)
return list.ReferenceOrigins(ctx)
}

if typ.IsSetType() {
_, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.refOriginsForNonComplexExpr(ctx, allowSelfRefs)
return a.refOriginsForNonComplexExpr(ctx)
}

set := Set{
Expand All @@ -49,13 +49,13 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
},
},
}
return set.ReferenceOrigins(ctx, allowSelfRefs)
return set.ReferenceOrigins(ctx)
}

if typ.IsTupleType() {
_, ok := a.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return a.refOriginsForNonComplexExpr(ctx, allowSelfRefs)
return a.refOriginsForNonComplexExpr(ctx)
}

elemTypes := typ.TupleElementTypes()
Expand All @@ -73,13 +73,13 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
pathCtx: a.pathCtx,
cons: cons,
}
return tuple.ReferenceOrigins(ctx, allowSelfRefs)
return tuple.ReferenceOrigins(ctx)
}

if typ.IsMapType() {
_, ok := a.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return a.refOriginsForNonComplexExpr(ctx, allowSelfRefs)
return a.refOriginsForNonComplexExpr(ctx)
}

m := Map{
Expand All @@ -92,13 +92,13 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
AllowInterpolatedKeys: true,
},
}
return m.ReferenceOrigins(ctx, allowSelfRefs)
return m.ReferenceOrigins(ctx)
}

if typ.IsObjectType() {
_, ok := a.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return a.refOriginsForNonComplexExpr(ctx, allowSelfRefs)
return a.refOriginsForNonComplexExpr(ctx)
}

obj := Object{
Expand All @@ -109,29 +109,29 @@ func (a Any) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
AllowInterpolatedKeys: true,
},
}
return obj.ReferenceOrigins(ctx, allowSelfRefs)
return obj.ReferenceOrigins(ctx)
}

return a.refOriginsForNonComplexExpr(ctx, allowSelfRefs)
return a.refOriginsForNonComplexExpr(ctx)
}

func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (a Any) refOriginsForNonComplexExpr(ctx context.Context) reference.Origins {
// TODO: Support splat expression https://github.com/hashicorp/terraform-ls/issues/526
// TODO: Support relative traversals https://github.com/hashicorp/terraform-ls/issues/532

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

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

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

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

Expand All @@ -142,7 +142,7 @@ func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool
returnType: a.cons.OfType,
pathCtx: a.pathCtx,
}
origins := funcExpr.ReferenceOrigins(ctx, allowSelfRefs)
origins := funcExpr.ReferenceOrigins(ctx)
if len(origins) > 0 {
return origins
}
Expand All @@ -155,6 +155,7 @@ func (a Any) refOriginsForNonComplexExpr(ctx context.Context, allowSelfRefs bool
return origins
}

allowSelfRefs := schema.ActiveSelfRefsFromContext(ctx)
te, ok := a.expr.(*hclsyntax.ScopeTraversalExpr)
if ok {
oCons := reference.OriginConstraints{
Expand Down
6 changes: 3 additions & 3 deletions decoder/expr_any_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (a Any) hoverTemplateExprAtPos(ctx context.Context, pos hcl.Pos) (*lang.Hov
return nil, false
}

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

switch eType := a.expr.(type) {
Expand All @@ -129,7 +129,7 @@ func (a Any) refOriginsForTemplateExpr(ctx context.Context, allowSelfRefs bool)
expr := newExpression(a.pathCtx, partExpr, cons)

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

Expand All @@ -141,7 +141,7 @@ func (a Any) refOriginsForTemplateExpr(ctx context.Context, allowSelfRefs bool)
expr := newExpression(a.pathCtx, eType.Wrapped, cons)

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

return origins, true
Expand Down
4 changes: 2 additions & 2 deletions decoder/expr_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (fe functionExpr) SemanticTokens(ctx context.Context) []lang.SemanticToken
return tokens
}

func (fe functionExpr) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (fe functionExpr) ReferenceOrigins(ctx context.Context) reference.Origins {
funcExpr, diags := hcl.ExprCall(fe.expr)
if diags.HasErrors() {
return reference.Origins{}
Expand Down Expand Up @@ -309,7 +309,7 @@ func (fe functionExpr) ReferenceOrigins(ctx context.Context, allowSelfRefs bool)
OfType: param.Type,
},
}
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}

return origins
Expand Down
4 changes: 2 additions & 2 deletions decoder/expr_list_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/hcl/v2"
)

func (list List) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (list List) ReferenceOrigins(ctx context.Context) reference.Origins {
elems, diags := hcl.ExprList(list.expr)
if diags.HasErrors() {
return reference.Origins{}
Expand All @@ -25,7 +25,7 @@ func (list List) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) refer
for _, elemExpr := range elems {
expr := newExpression(list.pathCtx, elemExpr, list.cons.Elem)
if e, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, e.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, e.ReferenceOrigins(ctx)...)
}
}

Expand Down
6 changes: 3 additions & 3 deletions decoder/expr_map_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

func (m Map) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (m Map) ReferenceOrigins(ctx context.Context) reference.Origins {
items, diags := hcl.ExprMap(m.expr)
if diags.HasErrors() {
return reference.Origins{}
Expand All @@ -35,14 +35,14 @@ func (m Map) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference
}
kExpr := newExpression(m.pathCtx, parensExpr, keyCons)
if expr, ok := kExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}
}
}

valExpr := newExpression(m.pathCtx, item.Value, m.cons.Elem)
if expr, ok := valExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}
}

Expand Down
6 changes: 3 additions & 3 deletions decoder/expr_object_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

func (obj Object) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (obj Object) ReferenceOrigins(ctx context.Context) reference.Origins {
items, diags := hcl.ExprMap(obj.expr)
if diags.HasErrors() {
return reference.Origins{}
Expand Down Expand Up @@ -43,15 +43,15 @@ func (obj Object) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) refe
}
kExpr := newExpression(obj.pathCtx, parensExpr, keyCons)
if expr, ok := kExpr.(ReferenceOriginsExpression); ok {
origins = append(origins, expr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, expr.ReferenceOrigins(ctx)...)
}
}
}

if isKnownAttr {
expr := newExpression(obj.pathCtx, item.Value, aSchema.Constraint)
if elemExpr, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, elemExpr.ReferenceOrigins(ctx, allowSelfRefs)...)
origins = append(origins, elemExpr.ReferenceOrigins(ctx)...)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions decoder/expr_one_of_ref_origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/hcl-lang/reference"
)

func (oo OneOf) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
func (oo OneOf) ReferenceOrigins(ctx context.Context) reference.Origins {
origins := make(reference.Origins, 0)

for _, con := range oo.cons {
Expand All @@ -19,7 +19,7 @@ func (oo OneOf) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) refere
continue
}

origins = appendOrigins(origins, e.ReferenceOrigins(ctx, allowSelfRefs))
origins = appendOrigins(origins, e.ReferenceOrigins(ctx))
}

return origins
Expand Down
Loading