5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 03:59:59 +08:00
wails/v2/pkg/parser/conversion.go
Lea Anthony 62374b9b53
Js package generation (#554)
* WIP

* Generation of index.js

* Add RelativeToCwd

* Add JSDoc comments

* Convert to ES6 syntax

* Fix typo

* Initial generation of typescript declarations

* Typescript improvements

* Improved @returns jsdoc

* Improved declaration files

* Simplified output

* Rename file

* Tidy up

* Revert "Simplified output"

This reverts commit 15cdf7382b.

* Now parsing actual code

* Support Array types

* Reimagined parser

* Wrap parsing in Parser

* Rewritten module generator (TS Only)

* Final touches

* Slight refactor to improve output

* Struct comments. External struct literal binding

* Reworked project parser *working*

* remove debug info

* Refactor of parser

* remove the spew

* Better Ts support

* Better project generation logic

* Support local functions in bind()

* JS Object generation. Linting.

* Support json tags in module generation

* Updated mod files

* Support vscode file generation

* Better global.d.ts

* add ts-check to templates

* Support TS declaration files

* improved 'generate' command for module
2020-11-15 09:25:38 +11:00

157 lines
4.5 KiB
Go

package parser
import (
"fmt"
"strings"
"github.com/leaanthony/slicer"
)
// JSType represents a javascript type
type JSType string
const (
// JsString is a JS string
JsString JSType = "string"
// JsBoolean is a JS bool
JsBoolean = "boolean"
// JsInt is a JS number
JsInt = "number"
// JsFloat is a JS number
JsFloat = "number"
// JsArray is a JS array
JsArray = "Array"
// JsObject is a JS object
JsObject = "Object"
// JsUnsupported represents a type that cannot be converted
JsUnsupported = "*"
)
func goTypeToJS(input *Field) string {
switch input.Type {
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 reflect.Array, reflect.Slice:
// return JsArray
// case reflect.Ptr, reflect.Struct, reflect.Map, reflect.Interface:
// return JsObject
case "struct":
return input.Struct.Name
default:
fmt.Printf("Unsupported input to goTypeToJS: %+v", input)
return "*"
}
}
// goTypeToTS converts the given field into a Typescript type
// The pkgName is the package that the field is being output in.
// This is used to ensure we don't qualify local structs.
func goTypeToTS(input *Field, pkgName string) string {
var result string
switch input.Type {
case "string":
result = "string"
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
result = "number"
case "float32", "float64":
result = "number"
case "bool":
result = "boolean"
case "struct":
if input.Struct.Package.Name != "" {
if input.Struct.Package.Name != pkgName {
result = input.Struct.Package.Name + "."
}
}
result += input.Struct.Name
// case reflect.Array, reflect.Slice:
// return string(JsArray)
// case reflect.Ptr, reflect.Struct:
// fqt := input.Type().String()
// return strings.Split(fqt, ".")[1]
// case reflect.Map, reflect.Interface:
// return string(JsObject)
default:
fmt.Printf("Unsupported input to goTypeToTS: %+v", input)
return JsUnsupported
}
if input.IsArray {
result = result + "[]"
}
return result
}
func goTypeToTSDeclaration(input *Field, pkgName string) string {
var result string
switch input.Type {
case "string":
result = "string"
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
result = "number"
case "float32", "float64":
result = "number"
case "bool":
result = "boolean"
case "struct":
if input.Struct.Package.Name != "" {
if input.Struct.Package.Name != pkgName {
result = `import("./_` + input.Struct.Package.Name + `").`
}
}
result += input.Struct.Name
// case reflect.Array, reflect.Slice:
// return string(JsArray)
// case reflect.Ptr, reflect.Struct:
// fqt := input.Type().String()
// return strings.Split(fqt, ".")[1]
// case reflect.Map, reflect.Interface:
// return string(JsObject)
default:
fmt.Printf("Unsupported input to goTypeToTS: %+v", input)
return JsUnsupported
}
if input.IsArray {
result = result + "[]"
}
return result
}
func isUnresolvedType(typeName string) bool {
switch typeName {
case "string":
return false
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
return false
case "float32", "float64":
return false
case "bool":
return false
case "struct":
return false
default:
return true
}
}
var reservedJSWords []string = []string{"abstract", "arguments", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield", "Array", "Date", "eval", "function", "hasOwnProperty", "Infinity", "isFinite", "isNaN", "isPrototypeOf", "length", "Math", "NaN", "Number", "Object", "prototype", "String", "toString", "undefined", "valueOf"}
var jsReservedWords *slicer.StringSlicer = slicer.String(reservedJSWords)
func isJSReservedWord(input string) bool {
return jsReservedWords.Contains(input)
}
func startsWithLowerCaseLetter(input string) bool {
firstLetter := string(input[0])
return strings.ToLower(firstLetter) == firstLetter
}