clutz
clutz copied to clipboard
Types like A|function(B) don't have enough parens in resulting TS
/** @param {string|function(string)} foo */ gets changed to foo: string|(p1: string) => AnyDuringMigration, but it actually needs an extra set of parens around the function type, to parse correctly.
I have a fix for this, and can submit a PR later, but don't have time to write tests right now. I'll post the diff here and you're welcome to use it or not.
+import static com.google.javascript.rhino.Token.FUNCTION_TYPE;
+
/** Code generator for gents to add TypeScript specific code generation. */
public class GentsCodeGenerator extends CodeGenerator {
private final NodeComments nodeComments;
@@ -84,6 +86,22 @@ public class GentsCodeGenerator extends CodeGenerator {
return prev != null && prev.isEmpty() && nodeComments.hasComment(prev);
}
+ void addUnionType(Node firstInList) {
+ for (Node n = firstInList; n != null; n = n.getNext()) {
+ boolean isFirst = n == firstInList;
+ if (!isFirst) {
+ add("|");
+ }
+ if(n.getToken() == FUNCTION_TYPE) {
+ add("(");
+ add(n);
+ add(")");
+ } else {
+ add(n);
+ }
+ }
+ }
+
/**
* Attempts to seize control of code generation if necessary.
*
@@ -150,6 +168,9 @@ public class GentsCodeGenerator extends CodeGenerator {
return true;
}
return false;
+ case UNION_TYPE:
+ addUnionType(n.getFirstChild());
+ return true;