-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
48 lines (41 loc) · 1.18 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package guts
import (
"fmt"
"github.com/coder/guts/bindings"
)
type typescriptNode struct {
Node bindings.Node
// mutations is a list of functions that need to be applied to the node before
// it can be serialized to typescript. It exists for ensuring consistent ordering
// of execution, regardless of the parsing order.
// These mutations can be anything.
mutations []func(v bindings.Node) (bindings.Node, error)
}
func (t typescriptNode) applyMutations() (typescriptNode, error) {
for i, m := range t.mutations {
var err error
t.Node, err = m(t.Node)
if err != nil {
return t, fmt.Errorf("apply mutation %d: %w", i, err)
}
}
t.mutations = nil
return t, nil
}
func (t *typescriptNode) AddEnum(enum bindings.ExpressionType) {
t.mutations = append(t.mutations, func(v bindings.Node) (bindings.Node, error) {
alias, ok := v.(*bindings.Alias)
if !ok {
return nil, fmt.Errorf("expected alias type, got %T", t.Node)
}
union, ok := alias.Type.(*bindings.UnionType)
if !ok {
// Make it a union, this removes the original type.
union = bindings.Union()
alias.Type = union
}
union.Types = append(union.Types, enum)
alias.Type = union
return alias, nil
})
}