5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-09 18:31:29 +08:00
wails/v3/internal/generator/render/templates/internal.js.tmpl
Fabio Massaioli f01b4b9a21
[v3] Fix binding generator bugs (#4001)
* Add some clarifying comments

* Remove special handling of window parameters

* Improve internal method exclusion

* Add test for internal method exclusion

* Remove useless blank field from app options

This is a leftover from an older version of the static analyser. It should have been removed long ago.

* Remove redundant godebug setting

gotypesalias=1 is the default starting with go1.23

* Use new range for syntax to simplify code

* Remove generator dependency on github.com/samber/lo

* Ensure generator testing tasks do not use the test cache

* Rename cyclic types test

* Test for cyclic imports

* Fix import cycle between model files

* Sort class aliases after their aliased class

* Test class aliases

* Fix length of default value for array types

* Test array initialization

* Add changelog

* Update changelog

* Fix contrived marking technique in model sorting algorithm

* Update binding example

* Update test data

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2025-01-17 18:56:07 +11:00

216 lines
8.1 KiB
Cheetah

{{$module := .}}
{{- $runtime := $module.Runtime}}
{{- $models := (fixext $module.ModelsFile)}}
{{- $useInterfaces := .UseInterfaces}}
{{- $imports := $module.Imports -}}
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
{{if not $useInterfaces}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import {Create as $Create} from "{{js $runtime}}";
{{end -}}
{{range $imports.External}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import * as {{jsimport .}} from "{{js .RelPath}}/{{js $models}}";
{{- end}}{{if $imports.External}}
{{end}}
{{- range $model := .Models}}
{{- $isEnum := $model.Values}}
{{- $isClassAlias := and $model.Type (not $useInterfaces) (isclass $model.Type)}}
{{- $isTypeAlias := and $model.Type (or $useInterfaces (not (isclass $model.Type)))}}
{{- $isClassOrInterface := and (not $model.Type) (not $model.Values)}}
{{- $isInterface := and $isClassOrInterface (or $useInterfaces $model.Alias)}}
{{- /* Build type parameter list */}}
{{- $typeParams := ""}}
{{- $typeParamList := ""}}
{{- $createParamList := ""}}
{{- range $i, $param := $model.TypeParams}}
{{- $param = (typeparam $i $param)}}
{{- if eq $i 0}}
{{- $typeParams = $param}}
{{- $typeParamList = (printf "<%s" $param)}}
{{- $createParamList = (printf "($$createParam%s" $param)}}
{{- else}}
{{- $typeParams = (printf "%s,%s" $typeParams $param)}}
{{- $typeParamList = (printf "%s, %s" $typeParamList $param)}}
{{- $createParamList = (printf "%s, $$createParam%s" $createParamList $param)}}
{{- end}}
{{- end}}
{{- if $typeParamList}}
{{- $typeParamList = (printf "%s>" $typeParamList)}}
{{- $createParamList = (printf "%s)" $createParamList)}}
{{- end}}
{{- if or $typeParamList (hasdoc $model.Decl.Doc) (hasdoc $model.Doc) $isEnum $isTypeAlias $isInterface}}
/**
{{- if hasdoc $model.Decl.Doc}}
{{- jsdoc $model.Decl.Doc.Text ""}}{{if hasdoc $model.Doc}}
*{{end}}
{{- end}}
{{- if hasdoc $model.Doc}}
{{- jsdoc $model.Doc.Text ""}}
{{- end}}
{{- if and $typeParamList (not $isClassAlias)}}
* @template {{$typeParams}}
{{- end}}
{{- if $isEnum}}
* @readonly
* @enum { {{- $module.JSType $model.Type -}} }
{{- else if $isTypeAlias}}
* @typedef { {{- $module.JSType $model.Type -}} } {{jsid $model.Name}}
{{- else if $isInterface}}
{{- if isjsdocobj $model}}
* @typedef {Object} {{jsid $model.Name}}
{{- range $i, $decl := $model.Fields}}{{range $j, $field := $decl}}
* @property { {{- $module.JSFieldType $field.StructField -}} }
{{- if $field.Optional}} [{{else}} {{end}}{{$field.JsonName}}{{if $field.Optional}}]{{end}}
{{- if hasdoc $field.Decl.Doc}} - {{jsdocline $field.Decl.Doc.Text}}{{end}}
{{- end}}{{end}}
{{- else}}
* @typedef { {
{{- range $i, $decl := $model.Fields}}{{range $j, $field := $decl}}
* "{{js $field.JsonName}}"{{if $field.Optional}}?{{end}}: {{$module.JSFieldType $field.StructField}},
{{- end}}{{end}}
* } } {{jsid $model.Name}}
{{- end}}
{{- end}}
*/
{{- end}}
{{- if $isEnum}}
export const {{jsid $model.Name}} = {
/**
* The Go zero value for the underlying type of the enum.
*/
$zero: {{$module.JSDefault $model.Type false}},
{{range $i, $decl := $model.Values}}{{range $j, $spec := $decl}}{{range $k, $value := $spec}}
{{- if and (ne $i 0) (eq $j 0) (eq $k 0)}}
{{end}}
{{- if or (and (eq $j 0) (eq $k 0) (hasdoc $value.Decl.Doc)) (and (eq $k 0) (hasdoc $value.Spec.Doc))}}
{{- if gt $j 0}}
{{end}}
/**
{{- if and (eq $j 0) (eq $k 0) (hasdoc $value.Decl.Doc)}}
{{- jsdoc $value.Decl.Doc.Text " "}}{{if and (eq $k 0) (hasdoc $value.Spec.Doc)}}
*{{end}}
{{- end}}
{{- if and (eq $k 0) (hasdoc $value.Spec.Doc)}}
{{- jsdoc $value.Spec.Doc.Text " "}}
{{- end}}
*/
{{- end}}
{{jsid $value.Name}}: {{jsvalue $value.Value}},
{{- end}}{{end}}{{end}}
};
{{else if $isClassAlias}}
export const {{jsid $model.Name}} = {{$module.JSType $model.Type.Origin}};
/**
{{- if hasdoc $model.Decl.Doc}}
{{- jsdoc $model.Decl.Doc.Text ""}}{{if hasdoc $model.Doc}}
*{{end}}
{{- end}}
{{- if hasdoc $model.Doc}}
{{- jsdoc $model.Doc.Text ""}}
{{- end}}
{{- if $typeParamList}}
* @template {{$typeParams}}
{{- end}}
* @typedef { {{- $module.JSType $model.Type -}} } {{jsid $model.Name}}
*/
{{else if and $isClassOrInterface (not $isInterface)}}
export class {{jsid $model.Name}} {
/**
* Creates a new {{jsid $model.Name}} instance.
* @param {Partial<{{jsid $model.Name}}{{$typeParamList}}>} [$$source = {}] - The source object to create the {{jsid $model.Name}}.
*/
constructor($$source = {}) {
{{- range $decl := $model.Fields}}{{range $j, $field := $decl}}
{{- /*
In JS we need to set all properties explicitly
because JSDoc has no support for arbitrary property names yet.
See https://github.com/jsdoc/jsdoc/issues/1468
For optional fields we make the initialization code unreachable
and cast the false condition to any to prevent any complaint from Typescript.
*/}}
if ({{if $field.Optional}}/** @type {any} */(false){{else}}!("{{js $field.JsonName}}" in $$source){{end}}) {
/**
{{- if and (eq $j 0) (hasdoc $field.Decl.Doc)}}
{{- jsdoc $field.Decl.Doc.Text " "}}
{{- end}}
* @member
* @type { {{- $module.JSFieldType $field.StructField}}{{if .Optional}} | undefined{{end -}} }
*/
this["{{js $field.JsonName}}"] = {{$module.JSDefault $field.Type $field.Quoted}};
}
{{- end}}{{end}}
Object.assign(this, $$source);
}
/**
{{- if $typeParamList}}
* Given creation functions for each type parameter,
* returns a creation function for a concrete instance
* of the generic class {{jsid $model.Name}}.
* @template {{$typeParams}}
{{- range $i, $param := $model.TypeParams}}
{{- $param = (typeparam $i $param)}}
* @param {(source: any) => {{$param -}} } $$createParam{{$param}}
{{- end}}
* @returns {($$source?: any) => {{jsid $model.Name}}{{$typeParamList -}} }
{{- else}}
* Creates a new {{jsid $model.Name}} instance from a string or object.
* @param {any} [$$source = {}]
* @returns { {{- jsid $model.Name -}} }
{{- end}}
*/
static createFrom{{if $typeParamList}}{{$createParamList}}{{else}}($$source = {}){{end}} {
{{- range $i, $spec := $model.Fields}}{{range $j, $field := $spec}}
{{- $create := ($module.JSCreateWithParams $field.Type $createParamList)}}
{{- if ne $create "$Create.Any"}}
const $$createField{{$i}}_{{$j}} = {{$create}};
{{- end}}
{{- end}}{{end}}
{{- $indent := ""}}
{{- if $typeParamList}}
{{- $indent = " "}}
return ($$source = {}) => {
{{- end}}
{{$indent}}let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
{{- range $i, $spec := $model.Fields}}{{range $j, $field := $spec}}
{{- if $module.NeedsCreate $field.Type}}
{{$indent}}if ("{{js $field.JsonName}}" in $$parsedSource) {
{{$indent}} $$parsedSource["{{js $field.JsonName}}"] = $$createField{{$i}}_{{$j}}($$parsedSource["{{js $field.JsonName}}"]);
{{$indent -}} }
{{- end}}
{{- end}}{{end}}
{{$indent}}return new {{jsid $model.Name}}(/** @type {Partial<{{jsid $model.Name}}{{$typeParamList}}>} */($$parsedSource));
{{- if $typeParamList}}
};
{{- end}}
}
}
{{else}}
{{- /* Rendered as a @typedef */}}
{{end}}
{{- end}}
{{- $postponed := $module.PostponedCreates}}
{{- if $postponed}}
// Private type creation functions
{{- range $i, $create := $postponed}}
{{if and (ge (len $create) 54) (eq (slice $create 39 54) "function $$init")}}var {{else}}const {{end -}}
$$createType{{$i}} = {{$create}};
{{- end}}
{{end}}
{{- if $useInterfaces}}
// In interface mode, this file is likely to contain just comments.
// We add a dummy export statement to ensure it is recognised as an ES module.
export {};
{{end -}}