| 1 |
package tygoja |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"regexp" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"go/ast" |
| 10 |
"go/token" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
const ( |
| 16 |
optionExtends = "extends" |
| 17 |
optionParenthesis = "parenthesis" |
| 18 |
optionFunctionReturn = "func_return" |
| 19 |
) |
| 20 |
|
| 21 |
func (g *PackageGenerator) writeIndent(s *strings.Builder, depth int) { |
| 22 |
for i := 0; i < depth; i++ { |
| 23 |
s.WriteString(g.conf.Indent) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (g *PackageGenerator) writeStartModifier(s *strings.Builder, depth int) { |
| 28 |
g.writeIndent(s, depth) |
| 29 |
|
| 30 |
if g.conf.StartModifier != "" { |
| 31 |
s.WriteString(g.conf.StartModifier) |
| 32 |
s.WriteString(" ") |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func (g *PackageGenerator) writeType(s *strings.Builder, t ast.Expr, depth int, options ...string) { |
| 37 |
switch t := t.(type) { |
| 38 |
case *ast.StarExpr: |
| 39 |
if hasOption(optionParenthesis, options) { |
| 40 |
s.WriteByte('(') |
| 41 |
} |
| 42 |
|
| 43 |
g.writeType(s, t.X, depth) |
| 44 |
|
| 45 |
|
| 46 |
if !hasOption(optionExtends, options) && !hasOption(optionFunctionReturn, options) { |
| 47 |
s.WriteString(" | undefined") |
| 48 |
} |
| 49 |
|
| 50 |
if hasOption(optionParenthesis, options) { |
| 51 |
s.WriteByte(')') |
| 52 |
} |
| 53 |
case *ast.Ellipsis: |
| 54 |
if v, ok := t.Elt.(*ast.Ident); ok && v.String() == "byte" { |
| 55 |
s.WriteString("string") |
| 56 |
break |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
_, isFunc := t.Elt.(*ast.FuncType) |
| 62 |
if isFunc { |
| 63 |
s.WriteString("(") |
| 64 |
} |
| 65 |
|
| 66 |
g.writeType(s, t.Elt, depth, optionParenthesis) |
| 67 |
|
| 68 |
if isFunc { |
| 69 |
s.WriteString(")") |
| 70 |
} |
| 71 |
|
| 72 |
s.WriteString("[]") |
| 73 |
case *ast.ArrayType: |
| 74 |
if v, ok := t.Elt.(*ast.Ident); ok && v.String() == "byte" && !hasOption(optionExtends, options) { |
| 75 |
|
| 76 |
|
| 77 |
s.WriteString("string|") |
| 78 |
} |
| 79 |
|
| 80 |
s.WriteString("Array<") |
| 81 |
g.writeType(s, t.Elt, depth, optionParenthesis) |
| 82 |
s.WriteString(">") |
| 83 |
case *ast.StructType: |
| 84 |
s.WriteString("{\n") |
| 85 |
g.writeStructFields(s, t.Fields.List, depth+1) |
| 86 |
g.writeIndent(s, depth+1) |
| 87 |
s.WriteByte('}') |
| 88 |
case *ast.Ident: |
| 89 |
v := t.String() |
| 90 |
|
| 91 |
mappedType, ok := g.conf.TypeMappings[v] |
| 92 |
if ok { |
| 93 |
|
| 94 |
v = mappedType |
| 95 |
} else { |
| 96 |
|
| 97 |
switch v { |
| 98 |
case "string": |
| 99 |
v = "string" |
| 100 |
case "bool": |
| 101 |
v = "boolean" |
| 102 |
case "int", "int8", "int16", "int32", "int64", |
| 103 |
"uint", "uint8", "uint16", "uint32", "uint64", |
| 104 |
"float32", "float64", |
| 105 |
"complex64", "complex128", |
| 106 |
"uintptr", "byte", "rune": |
| 107 |
v = "number" |
| 108 |
case "error": |
| 109 |
v = "Error" |
| 110 |
default: |
| 111 |
g.unknownTypes[v] = struct{}{} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
s.WriteString(v) |
| 116 |
case *ast.SelectorExpr: |
| 117 |
|
| 118 |
fullType := fmt.Sprintf("%s.%s", t.X, t.Sel) |
| 119 |
fullTypeWildcard := fmt.Sprintf("%s.*", t.X) |
| 120 |
|
| 121 |
if v, ok := g.conf.TypeMappings[fullType]; ok { |
| 122 |
s.WriteString(v) |
| 123 |
} else if v, ok := g.conf.TypeMappings[fullTypeWildcard]; ok { |
| 124 |
s.WriteString(v) |
| 125 |
} else { |
| 126 |
g.unknownTypes[fullType] = struct{}{} |
| 127 |
s.WriteString(fullType) |
| 128 |
} |
| 129 |
case *ast.MapType: |
| 130 |
s.WriteString("_TygojaDict") |
| 131 |
case *ast.BasicLit: |
| 132 |
s.WriteString(t.Value) |
| 133 |
case *ast.ParenExpr: |
| 134 |
s.WriteByte('(') |
| 135 |
g.writeType(s, t.X, depth) |
| 136 |
s.WriteByte(')') |
| 137 |
case *ast.BinaryExpr: |
| 138 |
g.writeType(s, t.X, depth) |
| 139 |
s.WriteByte(' ') |
| 140 |
s.WriteString(t.Op.String()) |
| 141 |
s.WriteByte(' ') |
| 142 |
g.writeType(s, t.Y, depth) |
| 143 |
case *ast.InterfaceType: |
| 144 |
s.WriteString("{\n") |
| 145 |
g.writeInterfaceFields(s, t.Methods.List, depth) |
| 146 |
g.writeIndent(s, depth+1) |
| 147 |
s.WriteByte('}') |
| 148 |
case *ast.FuncType: |
| 149 |
g.writeFuncType(s, t, depth, hasOption(optionParenthesis, options)) |
| 150 |
case *ast.UnaryExpr: |
| 151 |
if t.Op == token.TILDE { |
| 152 |
|
| 153 |
|
| 154 |
g.writeType(s, t.X, depth) |
| 155 |
} else { |
| 156 |
|
| 157 |
log.Printf("unhandled unary expr: %v\n %T\n", t, t) |
| 158 |
} |
| 159 |
case *ast.IndexListExpr: |
| 160 |
g.writeType(s, t.X, depth) |
| 161 |
s.WriteByte('<') |
| 162 |
for i, index := range t.Indices { |
| 163 |
g.writeType(s, index, depth) |
| 164 |
if i != len(t.Indices)-1 { |
| 165 |
s.WriteString(", ") |
| 166 |
} |
| 167 |
} |
| 168 |
s.WriteByte('>') |
| 169 |
case *ast.IndexExpr: |
| 170 |
g.writeType(s, t.X, depth) |
| 171 |
s.WriteByte('<') |
| 172 |
g.writeType(s, t.Index, depth) |
| 173 |
s.WriteByte('>') |
| 174 |
case *ast.CallExpr, *ast.ChanType, *ast.CompositeLit: |
| 175 |
s.WriteString("undefined") |
| 176 |
default: |
| 177 |
s.WriteString("any") |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func (g *PackageGenerator) writeTypeParamsFields(s *strings.Builder, fields []*ast.Field) { |
| 182 |
|
| 183 |
names := []string{} |
| 184 |
for _, f := range fields { |
| 185 |
for _, ident := range f.Names { |
| 186 |
names = append(names, ident.Name) |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if len(names) == 0 { |
| 199 |
return |
| 200 |
} |
| 201 |
|
| 202 |
s.WriteByte('<') |
| 203 |
|
| 204 |
for i, name := range names { |
| 205 |
if i > 0 { |
| 206 |
s.WriteString(",") |
| 207 |
} |
| 208 |
s.WriteString(name) |
| 209 |
} |
| 210 |
|
| 211 |
s.WriteByte('>') |
| 212 |
} |
| 213 |
|
| 214 |
func (g *PackageGenerator) writeInterfaceFields(s *strings.Builder, fields []*ast.Field, depth int) { |
| 215 |
for _, f := range fields { |
| 216 |
g.writeCommentGroup(s, f.Doc, depth+1) |
| 217 |
|
| 218 |
var methodName string |
| 219 |
if len(f.Names) != 0 && f.Names[0] != nil && len(f.Names[0].Name) != 0 { |
| 220 |
methodName = f.Names[0].Name |
| 221 |
} |
| 222 |
if len(methodName) == 0 || 'A' > methodName[0] || methodName[0] > 'Z' { |
| 223 |
continue |
| 224 |
} |
| 225 |
|
| 226 |
if g.conf.MethodNameFormatter != nil { |
| 227 |
methodName = g.conf.MethodNameFormatter(methodName) |
| 228 |
} |
| 229 |
|
| 230 |
g.writeIndent(s, depth+1) |
| 231 |
s.WriteString(methodName) |
| 232 |
g.writeType(s, f.Type, depth) |
| 233 |
|
| 234 |
if f.Comment != nil { |
| 235 |
s.WriteString(" // ") |
| 236 |
s.WriteString(f.Comment.Text()) |
| 237 |
} else { |
| 238 |
s.WriteByte('\n') |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
func (g *PackageGenerator) writeStructFields(s *strings.Builder, fields []*ast.Field, depth int) { |
| 244 |
for _, f := range fields { |
| 245 |
var fieldName string |
| 246 |
if len(f.Names) != 0 && f.Names[0] != nil && len(f.Names[0].Name) != 0 { |
| 247 |
fieldName = f.Names[0].Name |
| 248 |
} |
| 249 |
if len(fieldName) == 0 || 'A' > fieldName[0] || fieldName[0] > 'Z' { |
| 250 |
continue |
| 251 |
} |
| 252 |
|
| 253 |
if g.conf.FieldNameFormatter != nil { |
| 254 |
fieldName = g.conf.FieldNameFormatter(fieldName) |
| 255 |
} |
| 256 |
|
| 257 |
g.writeCommentGroup(s, f.Doc, depth+1) |
| 258 |
|
| 259 |
g.writeIndent(s, depth+1) |
| 260 |
quoted := !isValidJSName(fieldName) |
| 261 |
if quoted { |
| 262 |
s.WriteByte('\'') |
| 263 |
} |
| 264 |
s.WriteString(fieldName) |
| 265 |
if quoted { |
| 266 |
s.WriteByte('\'') |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
switch t := f.Type.(type) { |
| 271 |
case *ast.StarExpr: |
| 272 |
f.Type = t.X |
| 273 |
s.WriteByte('?') |
| 274 |
} |
| 275 |
|
| 276 |
s.WriteString(": ") |
| 277 |
g.writeType(s, f.Type, depth, optionParenthesis) |
| 278 |
|
| 279 |
if f.Comment != nil { |
| 280 |
|
| 281 |
s.WriteString(" // ") |
| 282 |
s.WriteString(f.Comment.Text()) |
| 283 |
} else { |
| 284 |
s.WriteByte('\n') |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func (g *PackageGenerator) writeFuncType(s *strings.Builder, t *ast.FuncType, depth int, returnAsProp bool) { |
| 290 |
s.WriteString("(") |
| 291 |
|
| 292 |
if t.Params != nil { |
| 293 |
g.writeFuncParams(s, t.Params.List, depth) |
| 294 |
} |
| 295 |
|
| 296 |
if returnAsProp { |
| 297 |
s.WriteString(") => ") |
| 298 |
} else { |
| 299 |
s.WriteString("): ") |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if t.Results == nil || len(t.Results.List) == 0 { |
| 309 |
s.WriteString("void") |
| 310 |
} else { |
| 311 |
|
| 312 |
lastReturn, ok := t.Results.List[len(t.Results.List)-1].Type.(*ast.Ident) |
| 313 |
if ok && lastReturn.Name == "error" { |
| 314 |
t.Results.List = t.Results.List[:len(t.Results.List)-1] |
| 315 |
} |
| 316 |
|
| 317 |
if len(t.Results.List) == 0 { |
| 318 |
s.WriteString("void") |
| 319 |
} else { |
| 320 |
|
| 321 |
|
| 322 |
hasMultipleReturnValues := len(t.Results.List) > 1 || len(t.Results.List[0].Names) > 1 |
| 323 |
if hasMultipleReturnValues { |
| 324 |
s.WriteRune('[') |
| 325 |
} |
| 326 |
|
| 327 |
for i, f := range t.Results.List { |
| 328 |
totalNames := max(len(f.Names), 1) |
| 329 |
for j := range totalNames { |
| 330 |
if i > 0 || j > 0 { |
| 331 |
s.WriteString(", ") |
| 332 |
} |
| 333 |
|
| 334 |
g.writeType(s, f.Type, 0, optionParenthesis, optionFunctionReturn) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if hasMultipleReturnValues { |
| 339 |
s.WriteRune(']') |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
func (g *PackageGenerator) writeFuncParams(s *strings.Builder, params []*ast.Field, depth int) { |
| 346 |
for i, f := range params { |
| 347 |
|
| 348 |
|
| 349 |
names := make([]string, 0, len(f.Names)) |
| 350 |
for j, ident := range f.Names { |
| 351 |
name := ident.Name |
| 352 |
if name == "" || isReservedIdentifier(name) { |
| 353 |
name = fmt.Sprintf("_arg%d%d", i, j) |
| 354 |
} |
| 355 |
names = append(names, name) |
| 356 |
} |
| 357 |
if len(names) == 0 { |
| 358 |
|
| 359 |
names = append(names, fmt.Sprintf("_arg%d", i)) |
| 360 |
} |
| 361 |
|
| 362 |
for j, fieldName := range names { |
| 363 |
if i > 0 || j > 0 { |
| 364 |
s.WriteString(", ") |
| 365 |
} |
| 366 |
|
| 367 |
var isVariadic bool |
| 368 |
|
| 369 |
switch t := f.Type.(type) { |
| 370 |
case *ast.StarExpr: |
| 371 |
f.Type = t.X |
| 372 |
case *ast.Ellipsis: |
| 373 |
isVariadic = true |
| 374 |
} |
| 375 |
|
| 376 |
g.writeCommentGroup(s, f.Doc, depth+2) |
| 377 |
if isVariadic { |
| 378 |
s.WriteString("...") |
| 379 |
} |
| 380 |
s.WriteString(fieldName) |
| 381 |
|
| 382 |
s.WriteString(": ") |
| 383 |
|
| 384 |
g.writeType(s, f.Type, depth, optionParenthesis) |
| 385 |
|
| 386 |
if f.Comment != nil { |
| 387 |
|
| 388 |
s.WriteString(" /* ") |
| 389 |
s.WriteString(f.Comment.Text()) |
| 390 |
s.WriteString(" */ ") |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
var reservedIdentifiers = map[string]struct{}{ |
| 398 |
"break": {}, |
| 399 |
"do": {}, |
| 400 |
"instanceof": {}, |
| 401 |
"typeof": {}, |
| 402 |
"case": {}, |
| 403 |
"else": {}, |
| 404 |
"new": {}, |
| 405 |
"var": {}, |
| 406 |
"catch": {}, |
| 407 |
"finally": {}, |
| 408 |
"return": {}, |
| 409 |
"void": {}, |
| 410 |
"continue": {}, |
| 411 |
"for": {}, |
| 412 |
"switch": {}, |
| 413 |
"while": {}, |
| 414 |
"debugger": {}, |
| 415 |
"function": {}, |
| 416 |
"this": {}, |
| 417 |
"with": {}, |
| 418 |
"default": {}, |
| 419 |
"if": {}, |
| 420 |
"throw": {}, |
| 421 |
"delete": {}, |
| 422 |
"in": {}, |
| 423 |
"try": {}, |
| 424 |
"class": {}, |
| 425 |
"enum": {}, |
| 426 |
"extends": {}, |
| 427 |
"super": {}, |
| 428 |
"const": {}, |
| 429 |
"export": {}, |
| 430 |
"import": {}, |
| 431 |
"implements": {}, |
| 432 |
"let": {}, |
| 433 |
"private": {}, |
| 434 |
"public": {}, |
| 435 |
"yield": {}, |
| 436 |
"interface": {}, |
| 437 |
"package": {}, |
| 438 |
"protected": {}, |
| 439 |
"static": {}, |
| 440 |
} |
| 441 |
|
| 442 |
func isReservedIdentifier(name string) bool { |
| 443 |
_, ok := reservedIdentifiers[name] |
| 444 |
return ok |
| 445 |
} |
| 446 |
|
| 447 |
var isValidJSNameRegexp = regexp.MustCompile(`(?m)^[\pL_][\pL\pN_]*$`) |
| 448 |
|
| 449 |
func isValidJSName(name string) bool { |
| 450 |
return isReservedIdentifier(name) || isValidJSNameRegexp.MatchString(name) |
| 451 |
} |
| 452 |
|
| 453 |
func hasOption(opt string, options []string) bool { |
| 454 |
for _, o := range options { |
| 455 |
if o == opt { |
| 456 |
return true |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
return false |
| 461 |
} |