diff --git a/convert.go b/convert.go index 8bfaa3d..09325b7 100644 --- a/convert.go +++ b/convert.go @@ -180,7 +180,14 @@ func (p *GoParser) ToTypescript() (*Typescript, error) { if err != nil { return nil, fmt.Errorf("node %q: %w", key, err) } - typescript.typescriptNodes[key] = &newNode + + // If the Node is nil, then it serves no purpose and can be + // removed from the typescriptNodes map. + if newNode.Node == nil { + delete(typescript.typescriptNodes, key) + } else { + typescript.typescriptNodes[key] = &newNode + } } return typescript, nil diff --git a/node.go b/node.go index 0bd0658..3b19a07 100644 --- a/node.go +++ b/node.go @@ -29,6 +29,11 @@ func (t typescriptNode) applyMutations() (typescriptNode, error) { func (t *typescriptNode) AddEnum(member *bindings.EnumMember) { t.mutations = append(t.mutations, func(v bindings.Node) (bindings.Node, error) { + if v == nil { + // Just delete the enum if the reference type cannot be found. + return nil, nil + } + alias, ok := v.(*bindings.Alias) if ok { // Switch to an enum diff --git a/testdata/enums/enums.go b/testdata/enums/enums.go index 82c7771..39cda22 100644 --- a/testdata/enums/enums.go +++ b/testdata/enums/enums.go @@ -1,5 +1,7 @@ package enums +import "time" + type ( EnumString string EnumSliceType []EnumString @@ -26,3 +28,9 @@ const ( AudienceTenant Audience = "tenant" AudienceTeam Audience = "team" ) + +// EmptyEnum references `time.Duration`, so the constant is considered an enum. +// However, 'time.Duration' is not a referenced type, so the enum does not exist +// in the output. +// For now, this kind of constant is ignored. +const EmptyEnum = 30 * time.Second