Skip to content

Commit

Permalink
decoder: Skip unknown tuple element (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Aug 3, 2021
1 parent f403a86 commit 476b73b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion decoder/semantic_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,12 @@ func tokensForTupleConsExpr(expr *hclsyntax.TupleConsExpr, exprType cty.Type) []
elemType = *exprType.SetElementType()
}
if exprType.IsTupleType() {
elemType = exprType.TupleElementType(i)
elTypes := exprType.TupleElementTypes()
if len(elTypes) < i+1 {
// skip unknown tuple element
continue
}
elemType = elTypes[i]
}

tokens = append(tokens, tokenForTypedExpression(e, elemType)...)
Expand Down
62 changes: 62 additions & 0 deletions decoder/semantic_tokens_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,68 @@ EOT
},
},
},
{
"undefined tuple expression",
map[string]*schema.AttributeSchema{
"attr": {
Expr: schema.ExprConstraints{
schema.TupleExpr{
Elems: []schema.ExprConstraints{},
},
},
},
},
`attr = [ "one" ]
`,
[]lang.SemanticToken{
{ // attr
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 5,
Byte: 4,
},
},
},
},
},
{
"undefined tuple type",
map[string]*schema.AttributeSchema{
"attr": {
Expr: schema.LiteralTypeOnly(cty.Tuple([]cty.Type{})),
},
},
`attr = [ "one" ]
`,
[]lang.SemanticToken{
{ // attr
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 5,
Byte: 4,
},
},
},
},
},
{
"tuple as list",
map[string]*schema.AttributeSchema{
Expand Down

0 comments on commit 476b73b

Please sign in to comment.