| 1 | package tygoja |
| 2 | |
| 3 | const ( |
| 4 | defaultIndent = " " |
| 5 | |
| 6 | // custom base types that every package has access to |
| 7 | BaseTypeDict = "_TygojaDict" // Record type alternative as a more generic map-like type |
| 8 | BaseTypeAny = "_TygojaAny" // any type alias to allow easier extends generation |
| 9 | ) |
| 10 | |
| 11 | // FieldNameFormatterFunc defines a function for formatting a field name. |
| 12 | type FieldNameFormatterFunc func(string) string |
| 13 | |
| 14 | // MethodNameFormatterFunc defines a function for formatting a method name. |
| 15 | type MethodNameFormatterFunc func(string) string |
| 16 | |
| 17 | type Config struct { |
| 18 | // Packages is a list of package paths just like you would import them in Go. |
| 19 | // Use "*" to generate all package types. |
| 20 | // |
| 21 | // Example: |
| 22 | // |
| 23 | // Packages: map[string][]string{ |
| 24 | // "time": {"Time"}, |
| 25 | // "github.com/pocketbase/pocketbase/core": {"*"}, |
| 26 | // } |
| 27 | Packages map[string][]string |
| 28 | |
| 29 | // Heading specifies a content that will be put at the top of the output declaration file. |
| 30 | // |
| 31 | // You would generally use this to import custom types or some custom TS declarations. |
| 32 | Heading string |
| 33 | |
| 34 | // TypeMappings specifies custom type translations. |
| 35 | // |
| 36 | // Useful for for mapping 3rd party package types, eg "unsafe.Pointer" => "CustomType". |
| 37 | // |
| 38 | // Be default unrecognized types will be recursively generated by |
| 39 | // traversing their import package (when possible). |
| 40 | TypeMappings map[string]string |
| 41 | |
| 42 | // WithConstants indicates whether to generate types for constants |
| 43 | // ("false" by default). |
| 44 | WithConstants bool |
| 45 | |
| 46 | // WithPackageFunctions indicates whether to generate types |
| 47 | // for package level functions ("false" by default). |
| 48 | WithPackageFunctions bool |
| 49 | |
| 50 | // FieldNameFormatter allows specifying a custom struct field name formatter. |
| 51 | FieldNameFormatter FieldNameFormatterFunc |
| 52 | |
| 53 | // MethodNameFormatter allows specifying a custom method name formatter. |
| 54 | MethodNameFormatter MethodNameFormatterFunc |
| 55 | |
| 56 | // StartModifier usually should be "export" or declare but as of now prevents |
| 57 | // the LSP autocompletion so we keep it empty. |
| 58 | // |
| 59 | // See also: |
| 60 | // https://github.com/microsoft/TypeScript/issues/54330 |
| 61 | // https://github.com/microsoft/TypeScript/pull/49644 |
| 62 | StartModifier string |
| 63 | |
| 64 | // Indent allow customizing the default indentation (use \t if you want tabs). |
| 65 | Indent string |
| 66 | } |
| 67 | |
| 68 | // Initializes the defaults (if not already) of the current config. |
| 69 | func (c *Config) InitDefaults() { |
| 70 | if c.Indent == "" { |
| 71 | c.Indent = defaultIndent |
| 72 | } |
| 73 | |
| 74 | if c.TypeMappings == nil { |
| 75 | c.TypeMappings = make(map[string]string) |
| 76 | } |
| 77 | |
| 78 | // special case for the unsafe package because it doesn't return its types in pkg.Syntax |
| 79 | if _, ok := c.TypeMappings["unsafe.Pointer"]; !ok { |
| 80 | c.TypeMappings["unsafe.Pointer"] = "number" |
| 81 | } |
| 82 | } |