Skip to content

Commit

Permalink
fix: avoid crashes on unknown keys
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 26, 2021
1 parent 5198071 commit f044bab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
4 changes: 3 additions & 1 deletion decoder/expression_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func constraintsAtPos(expr hcl.Expression, constraints ExprConstraints, pos hcl.
undeclaredAttributes := oe.Attributes
for _, item := range eType.Items {
key, _ := item.KeyExpr.Value(nil)
if !key.IsWhollyKnown() || key.Type() != cty.String {
if key.IsNull() || !key.IsWhollyKnown() || key.Type() != cty.String {
// skip items keys that can't be interpolated
// without further context
continue
}
attr, ok := oe.Attributes[key.AsString()]
Expand Down
4 changes: 3 additions & 1 deletion decoder/expression_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func (ec ExprConstraints) LiteralValueOfObjectConsExpr(expr *hclsyntax.ObjectCon
exprValues := make(map[string]cty.Value)
for _, item := range expr.Items {
key, _ := item.KeyExpr.Value(nil)
if !key.IsWhollyKnown() || key.Type() != cty.String {
if key.IsNull() || !key.IsWhollyKnown() || key.Type() != cty.String {
// Avoid building incomplete object with keys
// that can't be interpolated without further context
return schema.LiteralValue{}, false
}

Expand Down
4 changes: 3 additions & 1 deletion decoder/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ func hoverDataForExpr(expr hcl.Expression, constraints ExprConstraints, pos hcl.
func hoverDataForObjectExpr(objExpr *hclsyntax.ObjectConsExpr, oe schema.ObjectExpr, pos hcl.Pos) (*lang.HoverData, error) {
for _, item := range objExpr.Items {
key, _ := item.KeyExpr.Value(nil)
if !key.IsWhollyKnown() || key.Type() != cty.String {
if key.IsNull() || !key.IsWhollyKnown() || key.Type() != cty.String {
// skip items keys that can't be interpolated
// without further context
continue
}
attr, ok := oe.Attributes[key.AsString()]
Expand Down
32 changes: 19 additions & 13 deletions decoder/semantic_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ func tokensForExpression(expr hclsyntax.Expression, constraints ExprConstraints)
if ok {
for _, item := range eType.Items {
key, _ := item.KeyExpr.Value(nil)
if !key.IsWhollyKnown() || key.Type() != cty.String {
if key.IsNull() || !key.IsWhollyKnown() || key.Type() != cty.String {
// skip items keys that can't be interpolated
// without further context
continue
}
attr, ok := oe.Attributes[key.AsString()]
Expand Down Expand Up @@ -299,19 +301,23 @@ func tokensForObjectConsExpr(expr *hclsyntax.ObjectConsExpr, exprType cty.Type)
attrTypes := exprType.AttributeTypes()
for _, item := range expr.Items {
key, _ := item.KeyExpr.Value(nil)
if key.IsWhollyKnown() && key.Type() == cty.String {
valType, ok := attrTypes[key.AsString()]
if !ok {
// unknown attribute
continue
}
tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenObjectKey,
Modifiers: []lang.SemanticTokenModifier{},
Range: item.KeyExpr.Range(),
})
tokens = append(tokens, tokenForTypedExpression(item.ValueExpr, valType)...)
if key.IsNull() || !key.IsWhollyKnown() || key.Type() != cty.String {
// skip items keys that can't be interpolated
// without further context
continue
}

valType, ok := attrTypes[key.AsString()]
if !ok {
// unknown attribute
continue
}
tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenObjectKey,
Modifiers: []lang.SemanticTokenModifier{},
Range: item.KeyExpr.Range(),
})
tokens = append(tokens, tokenForTypedExpression(item.ValueExpr, valType)...)
}
}
if exprType.IsMapType() {
Expand Down
2 changes: 1 addition & 1 deletion decoder/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func nestedSymbolsForExpr(expr hcl.Expression) []Symbol {
case *hclsyntax.ObjectConsExpr:
for _, item := range e.Items {
key, _ := item.KeyExpr.Value(nil)
if key.IsNull() || key.Type() != cty.String {
if key.IsNull() || !key.IsWhollyKnown() || key.Type() != cty.String {
// skip items keys that can't be interpolated
// without further context
continue
Expand Down

0 comments on commit f044bab

Please sign in to comment.