5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 03:52:41 +08:00

Do not duplicate imports (#1307)

* Do not duplicate imports

* Fix warning as first element
This commit is contained in:
Ariel 2022-04-03 12:17:20 +02:00 committed by GitHub
parent 55cec7af17
commit d5a8ee0132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,10 +30,12 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
`) `)
var tsoutput bytes.Buffer var tsBody bytes.Buffer
tsoutput.WriteString(`// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL var tsContent bytes.Buffer
tsContent.WriteString(`// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
`) `)
var importClasses []string
for methodName, methodDetails := range methods { for methodName, methodDetails := range methods {
// Generate JS // Generate JS
@ -53,17 +55,18 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
// Generate TS // Generate TS
if len(methodDetails.StructNames) > 0 { if len(methodDetails.StructNames) > 0 {
classes := strings.Join(methodDetails.StructNames, ", ") for _, structName := range methodDetails.StructNames {
tsoutput.WriteString("import {" + classes + "} from '../models';") importClasses = append(importClasses, structName)
}
} }
tsoutput.WriteString(fmt.Sprintf("\nexport function %s(", methodName)) tsBody.WriteString(fmt.Sprintf("\nexport function %s(", methodName))
args.Clear() args.Clear()
for count, input := range methodDetails.Inputs { for count, input := range methodDetails.Inputs {
arg := fmt.Sprintf("arg%d", count+1) arg := fmt.Sprintf("arg%d", count+1)
args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName, false)) args.Add(arg + ":" + goTypeToTypescriptType(input.TypeName, false))
} }
tsoutput.WriteString(args.Join(",") + "):") tsBody.WriteString(args.Join(",") + "):")
returnType := "Promise" returnType := "Promise"
if methodDetails.OutputCount() > 0 { if methodDetails.OutputCount() > 0 {
firstType := goTypeToTypescriptType(methodDetails.Outputs[0].TypeName, false) firstType := goTypeToTypescriptType(methodDetails.Outputs[0].TypeName, false)
@ -76,15 +79,22 @@ func (b *Bindings) GenerateGoBindings(baseDir string) error {
} else { } else {
returnType = "Promise<void>" returnType = "Promise<void>"
} }
tsoutput.WriteString(returnType + ";\n") tsBody.WriteString(returnType + ";\n")
} }
importClasses = removeDuplicates(importClasses)
for _, class := range importClasses {
tsContent.WriteString("import {" + class + "} from '../models';\n")
}
tsContent.WriteString(tsBody.String())
jsfilename := filepath.Join(packageDir, structName+".js") jsfilename := filepath.Join(packageDir, structName+".js")
err = os.WriteFile(jsfilename, jsoutput.Bytes(), 0755) err = os.WriteFile(jsfilename, jsoutput.Bytes(), 0755)
if err != nil { if err != nil {
return err return err
} }
tsfilename := filepath.Join(packageDir, structName+".d.ts") tsfilename := filepath.Join(packageDir, structName+".d.ts")
err = os.WriteFile(tsfilename, tsoutput.Bytes(), 0755) err = os.WriteFile(tsfilename, tsContent.Bytes(), 0755)
if err != nil { if err != nil {
return err return err
} }
@ -323,3 +333,15 @@ func goTypeToTypescriptType(input string, useModelsNamespace bool) string {
} }
return goTypeToJSDocType(input, useModelsNamespace) return goTypeToJSDocType(input, useModelsNamespace)
} }
func removeDuplicates(input []string) []string {
seen := make(map[string]bool)
var result []string
for _, val := range input {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = true
}
}
return result
}