Skip to content

Commit

Permalink
TS generic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raikasdev committed Aug 31, 2024
1 parent 968611f commit f973593
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/main/java/io/github/bensku/tsbind/binding/BindingUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.bensku.tsbind.binding;

public class BindingUtil {
private static boolean allUpper(String input) {
for (char c : input.toCharArray()) {
// don't write in this way: if (!Character.isUpperCase(c))
if (Character.isLowerCase(c)) {
return false;
}
}
return true;
}

public static boolean isGenericType(String name) {
if (name.length() <= 3 && allUpper(name)) {
return true;
}

// Like TCommand
return name.length() >= 2 && name.startsWith("T") && Character.isUpperCase(name.toCharArray()[1]);
}
}
7 changes: 6 additions & 1 deletion src/main/java/io/github/bensku/tsbind/binding/TsClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ private void emitName(String name, TypeRef type, TsEmitter out) {
// Needs specialized handling, because we DON'T (always) want package name here
out.print(name);
if (type instanceof TypeRef.Parametrized) {
out.print("<").print(((TypeRef.Parametrized) type).typeParams(), ", ").print(">");
List<TypeRef> ref = ((TypeRef.Parametrized) type).typeParams();
if (ref.get(0).name().startsWith("T")) {
out.print("<").print(ref.get(0).name()).print(">");
} else {
out.print("<").print(ref, ", ").print(">");
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/github/bensku/tsbind/binding/TsMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class TsMembers {
out.print("%s: %s", name, node.type);
}
};

public static final TsGenerator<Method> METHOD = (node, out) -> {
node.javadoc.ifPresent(out::javadoc);
out.indent();
Expand All @@ -61,10 +61,10 @@ public class TsMembers {
if (!node.typeParams.isEmpty()) {
out.print("<");
TypeRef ref = node.typeParams.get(0);
if (ref.name().length() > 1) {
out.print(ref);
} else {
if (BindingUtil.isGenericType(ref.name())) {
out.print(ref.name());
} else {
out.print(ref);
}
out.print(">");
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/io/github/bensku/tsbind/binding/TsTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ public static Optional<String> primitiveName(TypeRef node) {
return;
}
out.print(base);
if (base.name().length() > 1) {
// TypeScript doesn't support nested generics
out.print("<").print(params, ",").print(">");

// TypeScript doesn't support nested generics
if (BindingUtil.isGenericType(base.name())) {
return;
}

out.print("<").print(params, ",").print(">");
};

public static final TsGenerator<TypeRef.Array> ARRAY = (node, out) -> {
Expand Down

0 comments on commit f973593

Please sign in to comment.