3
3
import java .util .ArrayList ;
4
4
import java .util .HashMap ;
5
5
import java .util .HashSet ;
6
+ import java .util .Iterator ;
6
7
import java .util .List ;
7
8
import java .util .Map ;
8
9
import java .util .Objects ;
12
13
import java .util .stream .Collectors ;
13
14
import java .util .stream .Stream ;
14
15
15
- import io .github .bensku .tsbind .ast .Constructor ;
16
16
import io .github .bensku .tsbind .ast .Getter ;
17
17
import io .github .bensku .tsbind .ast .Member ;
18
18
import io .github .bensku .tsbind .ast .Method ;
@@ -26,7 +26,39 @@ public class TsClass implements TsGenerator<TypeDefinition> {
26
26
27
27
private TsClass () {}
28
28
29
- private class Members {
29
+ private static class MethodId {
30
+ String name ;
31
+ List <String > paramTypes ;
32
+
33
+ MethodId (Method method ) {
34
+ this .name = method .name ();
35
+ this .paramTypes = method .params .stream ().map (param -> param .type )
36
+ .map (type -> TsTypes .primitiveName (type ).orElse (type .name ()))
37
+ .collect (Collectors .toList ());
38
+ }
39
+
40
+ @ Override
41
+ public int hashCode () {
42
+ return Objects .hash (name , paramTypes );
43
+ }
44
+
45
+ @ Override
46
+ public boolean equals (Object obj ) {
47
+ if (this == obj ) {
48
+ return true ;
49
+ }
50
+ if (obj == null ) {
51
+ return false ;
52
+ }
53
+ if (getClass () != obj .getClass ()) {
54
+ return false ;
55
+ }
56
+ MethodId other = (MethodId ) obj ;
57
+ return Objects .equals (name , other .name ) && Objects .equals (paramTypes , other .paramTypes );
58
+ }
59
+ }
60
+
61
+ private static class Members {
30
62
31
63
private final TypeDefinition type ;
32
64
private final List <Member > members ;
@@ -65,36 +97,6 @@ private void visitSupertypes(TypeDefinition type, Consumer<TypeDefinition> visit
65
97
* We do exactly that.
66
98
*/
67
99
public void addMissingOverloads () {
68
- class MethodId {
69
- String name ;
70
- List <TypeRef > paramTypes ;
71
-
72
- MethodId (Method method ) {
73
- this .name = method .name ();
74
- this .paramTypes = method .params .stream ().map (param -> param .type ).collect (Collectors .toList ());
75
- }
76
-
77
- @ Override
78
- public int hashCode () {
79
- return Objects .hash (name , paramTypes );
80
- }
81
-
82
- @ Override
83
- public boolean equals (Object obj ) {
84
- if (this == obj ) {
85
- return true ;
86
- }
87
- if (obj == null ) {
88
- return false ;
89
- }
90
- if (getClass () != obj .getClass ()) {
91
- return false ;
92
- }
93
- MethodId other = (MethodId ) obj ;
94
- return Objects .equals (name , other .name ) && Objects .equals (paramTypes , other .paramTypes );
95
- }
96
- }
97
-
98
100
// Figure out what methods we already have
99
101
Set <MethodId > methods = new HashSet <>();
100
102
for (Member member : members ) {
@@ -190,6 +192,26 @@ public void fixInheritDoc() {
190
192
}
191
193
}
192
194
195
+ /**
196
+ * Many Java types are emitted as 'number', which can cause strange
197
+ * duplicates to appear in TS types. This pass removes them.
198
+ */
199
+ public void removeDuplicates () {
200
+ Set <MethodId > methods = new HashSet <>();
201
+ Iterator <Member > it = members .iterator ();
202
+ while (it .hasNext ()) {
203
+ Member member = it .next ();
204
+ if (member instanceof Method ) {
205
+ MethodId id = new MethodId ((Method ) member );
206
+ if (methods .contains (id )) {
207
+ it .remove (); // Duplicate, remove it
208
+ } else {
209
+ methods .add (id ); // First occurrance
210
+ }
211
+ }
212
+ }
213
+ }
214
+
193
215
/**
194
216
* Transforms a TS getter/setter at given index to a normal method.
195
217
* If the member there is not an accessor, nothing is done.
@@ -282,6 +304,7 @@ public void emit(TypeDefinition node, TsEmitter out) {
282
304
Members members = new Members (node , out );
283
305
members .addMissingOverloads ();
284
306
members .fixInheritDoc ();
307
+ members .removeDuplicates ();
285
308
members .resolveConflicts ();
286
309
287
310
// Emit class members with some indentation
0 commit comments