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]
if !exists {
thisCtx := ctx
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))
for k := range ctx.Variables {
thisCtx = ctx
for thisCtx != nil {
for k := range thisCtx.Variables {
suggestions = append(suggestions, k)
}
thisCtx = thisCtx.parent
}
suggestion := nameSuggestion(name, suggestions)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
@ -100,9 +111,6 @@ func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) {
}
}
return split.Rel.TraverseRel(val)
}
// IsRelative returns true if the receiver is a relative traversal, or false
// otherwise.
func (t Traversal) IsRelative() bool {