Skip to content

Commit

Permalink
feat(typecheck): support chain nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Dec 21, 2024
1 parent 9f11bc5 commit ff6bdbd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/check/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,31 @@ func (s *scope) checkNode(tree *parse.Tree, dot types.Type, node parse.Node) (ty
return nil, nil
case *parse.WithNode:
return nil, s.checkWithNode(tree, dot, n)
case *parse.CommentNode:
return nil, nil
case *parse.NilNode:
return types.Typ[types.UntypedNil], nil
case *parse.ChainNode:
return s.checkChainNode(tree, dot, n)
case *parse.BranchNode:
return nil, nil
case *parse.BreakNode:
return nil, nil
case *parse.ContinueNode:
return nil, nil
default:
return nil, fmt.Errorf("missing node type check %T", n)
}
}

func (s *scope) checkChainNode(tree *parse.Tree, dot types.Type, n *parse.ChainNode) (types.Type, error) {
x, err := s.checkNode(tree, dot, n.Node)
if err != nil {
return nil, err
}
return s.checkIdentifiers(tree, x, n, n.Field)
}

func (s *scope) checkVariableNode(tree *parse.Tree, n *parse.VariableNode) (types.Type, error) {
tp, ok := s.variables[n.Ident[0]]
if !ok {
Expand Down
33 changes: 33 additions & 0 deletions internal/check/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ func TestTree(t *testing.T) {
require.ErrorContains(t, checkErr, "expectComplex64 argument 0 has type complex128 expected complex64")
},
},
{
Name: "chain node",
Template: `{{(.).A.B.C.D}}`,
Data: LetterChainA{},
Error: func(t *testing.T, checkErr, execErr error, tp types.Type) {
require.NoError(t, execErr)
require.NoError(t, checkErr)
},
},
{
Name: "chain node with type change in term",
Template: `{{(.A).B.C.D}}`,
Data: LetterChainA{},
Error: func(t *testing.T, checkErr, execErr error, tp types.Type) {
require.NoError(t, execErr)
require.NoError(t, checkErr)
},
},
// not sure if I should be downgrading bool, it should be fine to let it be since there is only one basic bool type
} {
t.Run(tt.Name, func(t *testing.T) {
Expand Down Expand Up @@ -580,6 +598,21 @@ func expectFloat32(n float32) float32 { return n }

func expectComplex64(n complex64) complex64 { return n }

type (
LetterChainA struct {
A LetterChainB
}
LetterChainB struct {
B LetterChainC
}
LetterChainC struct {
C LetterChainD
}
LetterChainD struct {
D T
}
)

func TestExampleTemplate(t *testing.T) {
packageList, loadErr := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedDeps | packages.NeedTypes,
Expand Down

0 comments on commit ff6bdbd

Please sign in to comment.