Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindings/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type EnumMember struct {
Name string
// Value is allowed to be nil, which results in `undefined`.
Value ExpressionType
SupportComments
}

func (*EnumMember) isNode() {}
Expand Down
9 changes: 7 additions & 2 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,15 @@ func (ts *Typescript) parse(obj types.Object) error {
// type. However, the order types are parsed is not guaranteed, so we
// add the enum to the Alias as a post-processing step.
ts.updateNode(enumObjName.Ref(), func(n *typescriptNode) {
n.AddEnum(&bindings.EnumMember{
member := &bindings.EnumMember{
Name: obj.Name(),
Value: constValue,
})
}
if ts.preserveComments {
cmts := ts.parsed.CommentForObject(obj)
member.AppendComments(cmts)
}
n.AddEnum(member)
})
return nil
case *types.Func:
Expand Down
6 changes: 5 additions & 1 deletion testdata/enums/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ type (

const (
// EnumFoo is the "foo" value
// This comment should be preserved
EnumFoo EnumString = "foo"
// EnumBar is the "bar" value
EnumBar EnumString = "bar"
EnumBaz EnumString = "baz"
EnumQux EnumString = "qux"
)

const (
// EnumNumFoo is the number 5
EnumNumFoo EnumInt = 5
EnumNumBar EnumInt = 10
)

const (
AudienceWorld Audience = "world"
AudienceTenant Audience = "tenant"
AudienceTeam Audience = "team"
// AudienceTeam is the "team" value
AudienceTeam Audience = "team"
)

// EmptyEnum references `time.Duration`, so the constant is considered an enum.
Expand Down
13 changes: 13 additions & 0 deletions testdata/enums/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// From enums/enums.go
export enum Audience {
/**
* AudienceTeam is the "team" value
*/
Team = "team",
Tenant = "tenant",
World = "world"
Expand All @@ -10,6 +13,9 @@ export enum Audience {
// From enums/enums.go
export enum EnumInt {
EnumNumBar = 10,
/**
* EnumNumFoo is the number 5
*/
EnumNumFoo = 5
}

Expand All @@ -21,8 +27,15 @@ export type EnumSliceType = readonly EnumString[];

// From enums/enums.go
export enum EnumString {
/**
* EnumBar is the "bar" value
*/
EnumBar = "bar",
EnumBaz = "baz",
/**
* EnumFoo is the "foo" value
* This comment should be preserved
*/
EnumFoo = "foo",
EnumQux = "qux"
}