zcl: TraverseAbs through parent scopes

An EvalContext can have a parent, so it's necessary to walk up until
the root is reached in case a parent scope defines the name we're
looking for.
This commit is contained in:
Martin Atkins 2017-06-14 08:54:12 -07:00
parent 51945b4e0c
commit 9340e6da4e

View File

@ -79,12 +79,23 @@ func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) {
} }
} }
val, exists := ctx.Variables[name] thisCtx := ctx
if !exists { for thisCtx != nil {
val, exists := thisCtx.Variables[name]
if exists {
return split.Rel.TraverseRel(val)
}
thisCtx = thisCtx.parent
}
suggestions := make([]string, 0, len(ctx.Variables)) suggestions := make([]string, 0, len(ctx.Variables))
for k := range ctx.Variables { thisCtx = ctx
for thisCtx != nil {
for k := range thisCtx.Variables {
suggestions = append(suggestions, k) suggestions = append(suggestions, k)
} }
thisCtx = thisCtx.parent
}
suggestion := nameSuggestion(name, suggestions) suggestion := nameSuggestion(name, suggestions)
if suggestion != "" { if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion) suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
@ -98,9 +109,6 @@ func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) {
Subject: &root.SrcRange, Subject: &root.SrcRange,
}, },
} }
}
return split.Rel.TraverseRel(val)
} }
// IsRelative returns true if the receiver is a relative traversal, or false // IsRelative returns true if the receiver is a relative traversal, or false