-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
astutil.go
109 lines (102 loc) · 2.32 KB
/
astutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tsgen
import (
"fmt"
"go/ast"
"strings"
)
// isExported checks if the given name is exported
func isExported(name string) bool {
return ast.IsExported(name)
}
// exprToString converts an expression to a string
func exprToString(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return toTsTypes(t.Name)
case *ast.SelectorExpr:
return exprToString(t.X) + "." + t.Sel.Name
case *ast.StarExpr:
return exprToString(t.X)
case *ast.ArrayType:
return toTsTypes("[]" + exprToString(t.Elt))
case *ast.InterfaceType:
return "interface{}"
case *ast.MapType:
return "Record<" + toTsTypes(exprToString(t.Key)) + ", " + toTsTypes(exprToString(t.Value)) + ">"
// Add more cases to handle other types
default:
return fmt.Sprintf("%T", expr)
}
}
// toTsTypes converts Go types to TypeScript types
func toTsTypes(t string) string {
if strings.Contains(t, "interface{}") {
return "any"
}
if strings.HasPrefix(t, "map[") {
return convertMaptoRecord(t)
}
switch t {
case "string":
return "string"
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
return "number"
case "float32", "float64":
return "number"
case "bool":
return "boolean"
case "[]byte":
return "Uint8Array"
case "interface{}":
return "any"
case "time.Duration":
return "number"
case "time.Time":
return "Date"
default:
if strings.HasPrefix(t, "[]") {
return toTsTypes(strings.TrimPrefix(t, "[]")) + "[]"
}
return t
}
}
func TsDefaultValue(t string) string {
switch t {
case "string":
return `""`
case "number":
return `0`
case "boolean":
return `false`
case "Uint8Array":
return `new Uint8Array(8)`
case "any":
return `undefined`
case "interface{}":
return `undefined`
default:
if strings.Contains(t, "[]") {
return `[]`
}
return "new " + t + "()"
}
}
// Ternary is a ternary operator for strings
func Ternary(condition bool, trueVal, falseVal string) string {
if condition {
return trueVal
}
return falseVal
}
// checkCanFail checks if a function can fail
func checkCanFail(fn *ast.FuncDecl) bool {
if fn.Type.Results != nil {
for _, result := range fn.Type.Results.List {
// Check if any of the return types is an error
if ident, ok := result.Type.(*ast.Ident); ok && ident.Name == "error" {
return true
}
}
}
return false
}