Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: enums using alias types #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
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
43 changes: 29 additions & 14 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,26 +494,41 @@ func (ts *Typescript) parse(obj types.Object) error {
// TODO: Are any enums var declarations? This is also codersdk.Me.
return nil // Maybe we should treat these like consts?
case *types.Const:
// Names are very likely enums
named, ok := obj.Type().(*types.Named)
if !ok {
// It could be a raw const value to generate.
if _, ok := obj.Type().(*types.Basic); ok {
cnst, err := ts.constantDeclaration(obj)
if err != nil {
return xerrors.Errorf("basic const %q: %w", objectIdentifier.Ref(), err)
type constMethods interface {
Obj() *types.TypeName
Underlying() types.Type
}

var use constMethods
{ // TODO: This block could be cleaned up
// Names & aliases are very likely enums
named, namedOk := obj.Type().(*types.Named)
aliased, aliasOk := obj.Type().(*types.Alias)

if !namedOk && !aliasOk {
// It could be a raw const value to generate.
if _, ok := obj.Type().(*types.Basic); ok {
cnst, err := ts.constantDeclaration(obj)
if err != nil {
return xerrors.Errorf("basic const %q: %w", objectIdentifier.Ref(), err)
}
return ts.setNode(objectIdentifier.Ref(), typescriptNode{
Node: cnst,
})
}
return ts.setNode(objectIdentifier.Ref(), typescriptNode{
Node: cnst,
})
return xerrors.Errorf("const %q is not a named type", objectIdentifier.Ref())
}
if namedOk {
use = named
} else {
use = aliased
}
return xerrors.Errorf("const %q is not a named type", objectIdentifier.Ref())
}

// Treat it as an enum.
enumObjName := ts.parsed.Identifier(named.Obj())
enumObjName := ts.parsed.Identifier(use.Obj())

switch named.Underlying().(type) {
switch use.Underlying().(type) {
case *types.Basic:
default:
return xerrors.Errorf("const %q is not a basic type, enums only support basic", objectIdentifier.Ref())
Expand Down
9 changes: 9 additions & 0 deletions testdata/enumtypes/enumtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ const (
AudienceTenant Audience = "tenant"
AudienceTeam Audience = "team"
)

type EnumAlias = string

const (
EnumAliasString EnumAlias = "string"
EnumAliasNumber EnumAlias = "number"
EnumAliasBoolean EnumAlias = "bool"
EnumAliasListString EnumAlias = "list(string)"
)
5 changes: 5 additions & 0 deletions testdata/enumtypes/enumtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export type Audience = "team" | "tenant" | "world";

export const Audiences: Audience[] = ["team", "tenant", "world"];

// From enumtypes/enumtypes.go
export type EnumAlias = "bool" | "list(string)" | "number" | "string";

export const EnumAliases: EnumAlias[] = ["bool", "list(string)", "number", "string"];

// From enumtypes/enumtypes.go
export type EnumInt = 10 | 5;

Expand Down