|
39 | 39 | // CLI option types:
|
40 | 40 | "github.com/coder/serpent",
|
41 | 41 | }
|
42 |
| - indent = "\t" |
| 42 | + indent = " " |
43 | 43 | )
|
44 | 44 |
|
45 | 45 | func main() {
|
@@ -646,7 +646,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
|
646 | 646 | // Just append these as fields. We should fix this later.
|
647 | 647 | state.Fields = append(state.Fields, tsType.AboveTypeLine)
|
648 | 648 | }
|
649 |
| - state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s;", indent, jsonName, optional, valueType)) |
| 649 | + state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, valueType)) |
650 | 650 | }
|
651 | 651 |
|
652 | 652 | // This is implemented to ensure the correct order of generics on the
|
@@ -759,8 +759,12 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
|
759 | 759 | // }
|
760 | 760 | // }
|
761 | 761 | return TypescriptType{
|
762 |
| - ValueType: "unknown", |
763 |
| - AboveTypeLine: indentedComment("Embedded anonymous struct, please fix by naming it"), |
| 762 | + ValueType: "any", |
| 763 | + AboveTypeLine: fmt.Sprintf("%s\n%s", |
| 764 | + indentedComment("Embedded anonymous struct, please fix by naming it"), |
| 765 | + // Linter needs to be disabled here, or else it will complain about the "any" type. |
| 766 | + indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Anonymously embedded struct"), |
| 767 | + ), |
764 | 768 | }, nil
|
765 | 769 | case *types.Map:
|
766 | 770 | // map[string][string] -> Record<string, string>
|
@@ -811,11 +815,16 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
|
811 | 815 | }
|
812 | 816 | genValue := ""
|
813 | 817 |
|
| 818 | + // Always wrap in parentheses for proper scoped types. |
| 819 | + // Running prettier on this output will remove redundant parenthesis, |
| 820 | + // so this makes our decision-making easier. |
| 821 | + // The example that breaks without this is: |
| 822 | + // readonly readonly string[][] |
814 | 823 | if underlying.GenericValue != "" {
|
815 |
| - genValue = "Readonly<Array<" + underlying.GenericValue + ">>" |
| 824 | + genValue = "(readonly " + underlying.GenericValue + "[])" |
816 | 825 | }
|
817 | 826 | return TypescriptType{
|
818 |
| - ValueType: "Readonly<Array<" + underlying.ValueType + ">>", |
| 827 | + ValueType: "(readonly " + underlying.ValueType + "[])", |
819 | 828 | GenericValue: genValue,
|
820 | 829 | AboveTypeLine: underlying.AboveTypeLine,
|
821 | 830 | GenericTypes: underlying.GenericTypes,
|
@@ -960,10 +969,11 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
|
960 | 969 | // You can handle your type manually in the switch list above, otherwise "any" will be used.
|
961 | 970 | // An easy way to fix this is to pull your external type into `codersdk` package, then it will
|
962 | 971 | // be known by the generator.
|
963 |
| - return TypescriptType{ |
964 |
| - ValueType: "unknown", |
965 |
| - AboveTypeLine: indentedComment(fmt.Sprintf("Named type %q unknown, using \"unknown\"", n.String())), |
966 |
| - }, nil |
| 972 | + return TypescriptType{ValueType: "any", AboveTypeLine: fmt.Sprintf("%s\n%s", |
| 973 | + indentedComment(fmt.Sprintf("Named type %q unknown, using \"any\"", n.String())), |
| 974 | + // Linter needs to be disabled here, or else it will complain about the "any" type. |
| 975 | + indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- External type"), |
| 976 | + )}, nil |
967 | 977 | }
|
968 | 978 |
|
969 | 979 | // Defer to the underlying type.
|
@@ -992,16 +1002,21 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
|
992 | 1002 | // This field is 'interface{}'. We can't infer any type from 'interface{}'
|
993 | 1003 | // so just use "any" as the type.
|
994 | 1004 | return TypescriptType{
|
995 |
| - ValueType: "unknown", |
996 |
| - AboveTypeLine: indentedComment("Empty interface{} type, cannot resolve the type."), |
| 1005 | + ValueType: "any", |
| 1006 | + AboveTypeLine: fmt.Sprintf("%s\n%s", |
| 1007 | + indentedComment("Empty interface{} type, cannot resolve the type."), |
| 1008 | + // Linter needs to be disabled here, or else it will complain about the "any" type. |
| 1009 | + indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- interface{}"), |
| 1010 | + ), |
997 | 1011 | }, nil
|
998 | 1012 | }
|
999 | 1013 |
|
1000 | 1014 | // Interfaces are difficult to determine the JSON type, so just return
|
1001 |
| - // an 'unknown'. |
| 1015 | + // an 'any'. |
1002 | 1016 | return TypescriptType{
|
1003 |
| - ValueType: "unknown", |
1004 |
| - Optional: false, |
| 1017 | + ValueType: "any", |
| 1018 | + AboveTypeLine: indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Golang interface, unable to resolve type."), |
| 1019 | + Optional: false, |
1005 | 1020 | }, nil
|
1006 | 1021 | case *types.TypeParam:
|
1007 | 1022 | _, ok := ty.Underlying().(*types.Interface)
|
|
0 commit comments