Skip to content

Commit 939909a

Browse files
committed
Fix rendering of generic named class aliases
1 parent d81656c commit 939909a

File tree

6 files changed

+187
-36
lines changed
  • v3/internal/parser
    • collect
    • render
    • testdata
      • lang=JS/UseInterfaces=false
        • UseNames=false/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here
        • UseNames=true/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here
      • lang=TS/UseInterfaces=false
        • UseNames=false/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here
        • UseNames=true/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here

6 files changed

+187
-36
lines changed

v3/internal/parser/collect/model.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ func (info *ModelInfo) Collect() *ModelInfo {
145145
// Type marshals to a custom string of unknown shape.
146146
info.Type = types.Typ[types.String]
147147
return
148+
} else if isGeneric && !collector.options.UseInterfaces && IsClass(typ) {
149+
// Generic classes cannot be defined in terms of other generic classes.
150+
// That would break class creation code,
151+
// and I (@fbbdev) couldn't find any other satisfying workaround.
152+
def = typ.Underlying()
148153
}
149154

150155
// Test for enums (excluding generic types).
@@ -162,11 +167,6 @@ func (info *ModelInfo) Collect() *ModelInfo {
162167
}
163168
}
164169

165-
if IsClass(typ) {
166-
// Skip alias chains for classes.
167-
def = types.Unalias(def)
168-
}
169-
170170
// Record required imports.
171171
info.Imports.AddType(def)
172172

v3/internal/parser/render/create.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ func (m *module) JSCreateWithParams(typ types.Type, params string) string {
8686

8787
switch t := typ.(type) {
8888
case *types.Alias:
89-
if t.Obj().Pkg() == nil {
90-
// Builtin alias: render underlying type.
91-
return m.JSCreateWithParams(t.Underlying(), params)
92-
}
93-
9489
return m.JSCreateWithParams(types.Unalias(typ), params)
9590

9691
case *types.Array:

v3/internal/parser/testdata/lang=JS/UseInterfaces=false/UseNames=false/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here/models.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,56 @@ import * as other$0 from "./other/models.js";
88

99
import * as $internal from "./internal.js";
1010

11-
/**
12-
* HowDifferent is a curious kind of person
13-
* that lets other people decide how they are different.
14-
*/
15-
export const HowDifferent = other$0.OtherPerson;
16-
1711
/**
1812
* HowDifferent is a curious kind of person
1913
* that lets other people decide how they are different.
2014
* @template How
21-
* @typedef {other$0.OtherPerson<{ [_: string]: How | null }>} HowDifferent
2215
*/
16+
export class HowDifferent {
17+
/**
18+
* Creates a new HowDifferent instance.
19+
* @param {Partial<HowDifferent<How>>} [$$source = {}] - The source object to create the HowDifferent.
20+
*/
21+
constructor($$source = {}) {
22+
if (!("Name" in $$source)) {
23+
/**
24+
* They have a name as well.
25+
* @member
26+
* @type {string}
27+
*/
28+
this["Name"] = "";
29+
}
30+
if (!("Differences" in $$source)) {
31+
/**
32+
* But they may have many differences.
33+
* @member
34+
* @type {{ [_: string]: How | null }[]}
35+
*/
36+
this["Differences"] = [];
37+
}
38+
39+
Object.assign(this, $$source);
40+
}
41+
42+
/**
43+
* Given creation functions for each type parameter,
44+
* returns a creation function for a concrete instance
45+
* of the generic class HowDifferent.
46+
* @template How
47+
* @param {(any) => How} $$createParamHow
48+
* @returns {($$source?: any) => HowDifferent<How>}
49+
*/
50+
static createFrom($$createParamHow) {
51+
const $$createField1_0 = $$createType1($$createParamHow);
52+
return ($$source = {}) => {
53+
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
54+
if ("Differences" in $$parsedSource) {
55+
$$parsedSource["Differences"] = $$createField1_0($$parsedSource["Differences"]);
56+
}
57+
return new HowDifferent(/** @type {Partial<HowDifferent<How>>} */($$parsedSource));
58+
};
59+
}
60+
}
2361

2462
/**
2563
* Impersonator gets their fields from other people.
@@ -72,3 +110,7 @@ export const PrivatePerson = $internal.personImpl;
72110
* PrivatePerson gets their fields from hidden sources.
73111
* @typedef {$internal.personImpl} PrivatePerson
74112
*/
113+
114+
// Private type creation functions
115+
const $$createType0 = ($$createParamHow) => $Create.Map($Create.Any, $$createParamHow);
116+
const $$createType1 = ($$createParamHow) => $Create.Array($$createType0($$createParamHow));

v3/internal/parser/testdata/lang=JS/UseInterfaces=false/UseNames=true/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here/models.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,56 @@ import * as other$0 from "./other/models.js";
88

99
import * as $internal from "./internal.js";
1010

11-
/**
12-
* HowDifferent is a curious kind of person
13-
* that lets other people decide how they are different.
14-
*/
15-
export const HowDifferent = other$0.OtherPerson;
16-
1711
/**
1812
* HowDifferent is a curious kind of person
1913
* that lets other people decide how they are different.
2014
* @template How
21-
* @typedef {other$0.OtherPerson<{ [_: string]: How | null }>} HowDifferent
2215
*/
16+
export class HowDifferent {
17+
/**
18+
* Creates a new HowDifferent instance.
19+
* @param {Partial<HowDifferent<How>>} [$$source = {}] - The source object to create the HowDifferent.
20+
*/
21+
constructor($$source = {}) {
22+
if (!("Name" in $$source)) {
23+
/**
24+
* They have a name as well.
25+
* @member
26+
* @type {string}
27+
*/
28+
this["Name"] = "";
29+
}
30+
if (!("Differences" in $$source)) {
31+
/**
32+
* But they may have many differences.
33+
* @member
34+
* @type {{ [_: string]: How | null }[]}
35+
*/
36+
this["Differences"] = [];
37+
}
38+
39+
Object.assign(this, $$source);
40+
}
41+
42+
/**
43+
* Given creation functions for each type parameter,
44+
* returns a creation function for a concrete instance
45+
* of the generic class HowDifferent.
46+
* @template How
47+
* @param {(any) => How} $$createParamHow
48+
* @returns {($$source?: any) => HowDifferent<How>}
49+
*/
50+
static createFrom($$createParamHow) {
51+
const $$createField1_0 = $$createType1($$createParamHow);
52+
return ($$source = {}) => {
53+
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
54+
if ("Differences" in $$parsedSource) {
55+
$$parsedSource["Differences"] = $$createField1_0($$parsedSource["Differences"]);
56+
}
57+
return new HowDifferent(/** @type {Partial<HowDifferent<How>>} */($$parsedSource));
58+
};
59+
}
60+
}
2361

2462
/**
2563
* Impersonator gets their fields from other people.
@@ -72,3 +110,7 @@ export const PrivatePerson = $internal.personImpl;
72110
* PrivatePerson gets their fields from hidden sources.
73111
* @typedef {$internal.personImpl} PrivatePerson
74112
*/
113+
114+
// Private type creation functions
115+
const $$createType0 = ($$createParamHow) => $Create.Map($Create.Any, $$createParamHow);
116+
const $$createType1 = ($$createParamHow) => $Create.Array($$createType0($$createParamHow));

v3/internal/parser/testdata/lang=TS/UseInterfaces=false/UseNames=false/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here/models.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,45 @@ import * as $internal from "./internal.ts";
1111
* HowDifferent is a curious kind of person
1212
* that lets other people decide how they are different.
1313
*/
14-
export const HowDifferent = other$0.OtherPerson;
14+
export class HowDifferent<How> {
15+
/**
16+
* They have a name as well.
17+
*/
18+
"Name": string;
1519

16-
/**
17-
* HowDifferent is a curious kind of person
18-
* that lets other people decide how they are different.
19-
*/
20-
export type HowDifferent<How> = other$0.OtherPerson<{ [_: string]: How | null }>;
20+
/**
21+
* But they may have many differences.
22+
*/
23+
"Differences": { [_: string]: How | null }[];
24+
25+
/** Creates a new HowDifferent instance. */
26+
constructor($$source: Partial<HowDifferent<How>> = {}) {
27+
if (!("Name" in $$source)) {
28+
this["Name"] = "";
29+
}
30+
if (!("Differences" in $$source)) {
31+
this["Differences"] = [];
32+
}
33+
34+
Object.assign(this, $$source);
35+
}
36+
37+
/**
38+
* Given creation functions for each type parameter,
39+
* returns a creation function for a concrete instance
40+
* of the generic class HowDifferent.
41+
*/
42+
static createFrom<How>($$createParamHow: (any) => How): ($$source?: any) => HowDifferent<How> {
43+
const $$createField1_0 = $$createType1($$createParamHow);
44+
return ($$source: any = {}) => {
45+
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
46+
if ("Differences" in $$parsedSource) {
47+
$$parsedSource["Differences"] = $$createField1_0($$parsedSource["Differences"]);
48+
}
49+
return new HowDifferent<How>($$parsedSource as Partial<HowDifferent<How>>);
50+
};
51+
}
52+
}
2153

2254
/**
2355
* Impersonator gets their fields from other people.
@@ -65,3 +97,7 @@ export const PrivatePerson = $internal.personImpl;
6597
* PrivatePerson gets their fields from hidden sources.
6698
*/
6799
export type PrivatePerson = $internal.personImpl;
100+
101+
// Private type creation functions
102+
const $$createType0 = ($$createParamHow) => $Create.Map($Create.Any, $$createParamHow);
103+
const $$createType1 = ($$createParamHow) => $Create.Array($$createType0($$createParamHow));

v3/internal/parser/testdata/lang=TS/UseInterfaces=false/UseNames=true/github.com/wailsapp/wails/v3/internal/parser/testcases/no_bindings_here/models.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,45 @@ import * as $internal from "./internal.ts";
1111
* HowDifferent is a curious kind of person
1212
* that lets other people decide how they are different.
1313
*/
14-
export const HowDifferent = other$0.OtherPerson;
14+
export class HowDifferent<How> {
15+
/**
16+
* They have a name as well.
17+
*/
18+
"Name": string;
1519

16-
/**
17-
* HowDifferent is a curious kind of person
18-
* that lets other people decide how they are different.
19-
*/
20-
export type HowDifferent<How> = other$0.OtherPerson<{ [_: string]: How | null }>;
20+
/**
21+
* But they may have many differences.
22+
*/
23+
"Differences": { [_: string]: How | null }[];
24+
25+
/** Creates a new HowDifferent instance. */
26+
constructor($$source: Partial<HowDifferent<How>> = {}) {
27+
if (!("Name" in $$source)) {
28+
this["Name"] = "";
29+
}
30+
if (!("Differences" in $$source)) {
31+
this["Differences"] = [];
32+
}
33+
34+
Object.assign(this, $$source);
35+
}
36+
37+
/**
38+
* Given creation functions for each type parameter,
39+
* returns a creation function for a concrete instance
40+
* of the generic class HowDifferent.
41+
*/
42+
static createFrom<How>($$createParamHow: (any) => How): ($$source?: any) => HowDifferent<How> {
43+
const $$createField1_0 = $$createType1($$createParamHow);
44+
return ($$source: any = {}) => {
45+
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
46+
if ("Differences" in $$parsedSource) {
47+
$$parsedSource["Differences"] = $$createField1_0($$parsedSource["Differences"]);
48+
}
49+
return new HowDifferent<How>($$parsedSource as Partial<HowDifferent<How>>);
50+
};
51+
}
52+
}
2153

2254
/**
2355
* Impersonator gets their fields from other people.
@@ -65,3 +97,7 @@ export const PrivatePerson = $internal.personImpl;
6597
* PrivatePerson gets their fields from hidden sources.
6698
*/
6799
export type PrivatePerson = $internal.personImpl;
100+
101+
// Private type creation functions
102+
const $$createType0 = ($$createParamHow) => $Create.Map($Create.Any, $$createParamHow);
103+
const $$createType1 = ($$createParamHow) => $Create.Array($$createType0($$createParamHow));

0 commit comments

Comments
 (0)