TODO: Return comments?
(ty types.Type)
| 985 | |
| 986 | // TODO: Return comments? |
| 987 | func (ts *Typescript) typescriptType(ty types.Type) (parsedType, error) { |
| 988 | // No matter what the type is, if we have some custom override, always use that. |
| 989 | custom, ok := ts.parsed.typeOverrides[ty.String()] |
| 990 | if ok { |
| 991 | return parsedType{ |
| 992 | Value: custom(), |
| 993 | }, nil |
| 994 | } |
| 995 | |
| 996 | switch ty := ty.(type) { |
| 997 | case *types.Signature: |
| 998 | // TODO: Handle functions better |
| 999 | return simpleParsedType(ptr(bindings.KeywordUnknown)). |
| 1000 | WithComments("Function type detected, and unsupported. Leaving the type as unknown"), nil |
| 1001 | case *types.Basic: |
| 1002 | bs := ty |
| 1003 | // All basic literals (string, bool, int, etc). |
| 1004 | switch { |
| 1005 | case bs.Info()&types.IsNumeric > 0: |
| 1006 | return simpleParsedType(ptr(bindings.KeywordNumber)), nil |
| 1007 | case bs.Info()&types.IsBoolean > 0: |
| 1008 | return simpleParsedType(ptr(bindings.KeywordBoolean)), nil |
| 1009 | case bs.Kind() == types.Byte: |
| 1010 | // TODO: @emyrk What is a byte for typescript? A string? A uint8? |
| 1011 | // TODO: Comment |
| 1012 | //return bindings.PrependComment("This is a byte in golang", bindings.Literal(bindings.KeywordNumber)), nil |
| 1013 | return simpleParsedType(ptr(bindings.KeywordNumber)), nil |
| 1014 | case bs.Kind() == types.String, bs.Kind() == types.Rune: |
| 1015 | return simpleParsedType(ptr(bindings.KeywordString)), nil |
| 1016 | case bs.Kind() == types.Invalid: |
| 1017 | // TODO: Investigate why this happens |
| 1018 | return simpleParsedType(ptr(bindings.KeywordAny)).WithComments("Invalid type, using 'any'. Might be a reference to any external package"), nil |
| 1019 | default: |
| 1020 | return parsedType{}, xerrors.Errorf("unsupported basic type %q", bs.String()) |
| 1021 | } |
| 1022 | case *types.Struct: |
| 1023 | // This handles anonymous structs. This should never happen really. |
| 1024 | // If you require this, either change your datastructures, or implement |
| 1025 | // anonymous structs here. |
| 1026 | // Such as: |
| 1027 | // type Name struct { |
| 1028 | // Embedded struct { |
| 1029 | // Field string `json:"field"` |
| 1030 | // } |
| 1031 | // } |
| 1032 | // TODO: Comment: indentedComment("Embedded anonymous struct, please fix by naming it"), |
| 1033 | parsed := simpleParsedType(ptr(bindings.KeywordUnknown)) |
| 1034 | parsed.RaisedComments = append(parsed.RaisedComments, "embedded anonymous struct, please fix by naming it") |
| 1035 | return parsed, nil |
| 1036 | case *types.Map: |
| 1037 | // Record is reference type with 2 type parameters. |
| 1038 | // map[string][string] -> Record<string, string> |
| 1039 | |
| 1040 | m := ty |
| 1041 | keyType, err := ts.typescriptType(m.Key()) |
| 1042 | if err != nil { |
| 1043 | return parsedType{}, xerrors.Errorf("map key: %w", err) |
| 1044 | } |
no test coverage detected