Skip to content

Commit

Permalink
fix: handle nil expr for hover
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Feb 20, 2024
1 parent d8bcca3 commit 905e563
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decoder/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (d *PathDecoder) hoverAtPos(ctx context.Context, body *hclsyntax.Body, body
}, nil
}

if attr.Expr.Range().ContainsPos(pos) {
if attr.Expr != nil && attr.Expr.Range().ContainsPos(pos) {
return d.newExpression(attr.Expr, aSchema.Constraint).HoverAtPos(ctx, pos), nil
}
}
Expand Down
29 changes: 29 additions & 0 deletions decoder/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,35 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
}
}

func TestDecoder_HoverAtPos_nil_expr(t *testing.T) {
// provider:: is not a traversal expression, so hcl will return a nil expression which needs to be
// handled gracefully
f, _ := hclsyntax.ParseConfig([]byte(`attr = provider::`), "test.tf", hcl.InitialPos)

d := testPathDecoder(t, &PathContext{
Schema: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr": {Constraint: schema.AnyExpression{OfType: cty.DynamicPseudoType}},
},
},
Files: map[string]*hcl.File{
"test.tf": f,
},
})

ctx := context.Background()
_, err := d.HoverAtPos(ctx, "test.tf", hcl.Pos{Line: 1, Column: 16, Byte: 15})

if err == nil {
t.Fatal("expected error")
}

positionalErr := &PositionalError{}
if !errors.As(err, &positionalErr) {
t.Fatal("expected PositionalError for invalid expression")
}
}

func TestDecoder_HoverAtPos_URL(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
Expand Down

0 comments on commit 905e563

Please sign in to comment.