Skip to content

Commit 9010ce7

Browse files
authored
fix: handle consts with references to external types (#35)
* fix: handle consts with references to external types Consts to external types look like enums, but fail to generate. * add comment
1 parent 641893a commit 9010ce7

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

convert.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ func (p *GoParser) ToTypescript() (*Typescript, error) {
180180
if err != nil {
181181
return nil, fmt.Errorf("node %q: %w", key, err)
182182
}
183-
typescript.typescriptNodes[key] = &newNode
183+
184+
// If the Node is nil, then it serves no purpose and can be
185+
// removed from the typescriptNodes map.
186+
if newNode.Node == nil {
187+
delete(typescript.typescriptNodes, key)
188+
} else {
189+
typescript.typescriptNodes[key] = &newNode
190+
}
184191
}
185192

186193
return typescript, nil

node.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func (t typescriptNode) applyMutations() (typescriptNode, error) {
2929

3030
func (t *typescriptNode) AddEnum(member *bindings.EnumMember) {
3131
t.mutations = append(t.mutations, func(v bindings.Node) (bindings.Node, error) {
32+
if v == nil {
33+
// Just delete the enum if the reference type cannot be found.
34+
return nil, nil
35+
}
36+
3237
alias, ok := v.(*bindings.Alias)
3338
if ok {
3439
// Switch to an enum

testdata/enums/enums.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package enums
22

3+
import "time"
4+
35
type (
46
EnumString string
57
EnumSliceType []EnumString
@@ -26,3 +28,9 @@ const (
2628
AudienceTenant Audience = "tenant"
2729
AudienceTeam Audience = "team"
2830
)
31+
32+
// EmptyEnum references `time.Duration`, so the constant is considered an enum.
33+
// However, 'time.Duration' is not a referenced type, so the enum does not exist
34+
// in the output.
35+
// For now, this kind of constant is ignored.
36+
const EmptyEnum = 30 * time.Second

0 commit comments

Comments
 (0)