Use a switch instead of if-else when printing a JSTypeExpressions
This should be a no-op.
I think this is easier to follow. It also improves the error message if we encounter an unexpected Token type - previously it would fall through into `typeNode.getString()`, which might crash or might accidentally work.
@@ -452,13 +452,16 @@ public final class JSDocInfoPrinter {
sb.append(typeNode.getOriginalName());
return;
}
- if (typeNode.getToken() == Token.BANG) {
+ switch (typeNode.getToken()) {
+ case BANG:
sb.append("!");
appendTypeNode(sb, typeNode.getFirstChild());
- } else if (typeNode.getToken() == Token.EQUALS) {
+ break;
+ case EQUALS:
appendTypeNode(sb, typeNode.getFirstChild());
sb.append("=");
- } else if (typeNode.getToken() == Token.PIPE) {
+ break;
+ case PIPE:
sb.append("(");
Node lastChild = typeNode.getLastChild();
for (Node child = typeNode.getFirstChild(); child != null; child = child.getNext()) {
@@ -468,21 +471,26 @@ public final class JSDocInfoPrinter {
}
}
sb.append(")");
- } else if (typeNode.getToken() == Token.ITER_REST) {
+ break;
+ case ITER_REST:
sb.append("...");
if (typeNode.hasChildren() && !typeNode.getFirstChild().isEmpty()) {
appendTypeNode(sb, typeNode.getFirstChild());
}
- } else if (typeNode.getToken() == Token.STAR) {
+ break;
+ case STAR:
sb.append("*");
- } else if (typeNode.getToken() == Token.QMARK) {
+ break;
+ case QMARK:
sb.append("?");
if (typeNode.hasChildren()) {
appendTypeNode(sb, typeNode.getFirstChild());
}
- } else if (typeNode.isFunction()) {
+ break;
+ case FUNCTION:
appendFunctionNode(sb, typeNode);
- } else if (typeNode.getToken() == Token.LC) {
+ break;
+ case LC:
sb.append("{");
Node lb = typeNode.getFirstChild();
Node lastColon = lb.getLastChild();
@@ -498,27 +506,33 @@ public final class JSDocInfoPrinter {
}
}
sb.append("}");
- } else if (typeNode.isVoid()) {
+ break;
+ case VOID:
sb.append("void");
- } else if (typeNode.isTypeOf()) {
+ break;
+ case TYPEOF:
sb.append("typeof ");
appendTypeNode(sb, typeNode.getFirstChild());
- } else {
- if (typeNode.hasChildren()) {
- sb.append(typeNode.getString())
- .append("<");
- Node child = typeNode.getFirstChild();
- Node last = child.getLastChild();
- for (Node type = child.getFirstChild(); type != null; type = type.getNext()) {
+ break;
+ case BLOCK:
+ sb.append("<");
+ Node last = typeNode.getLastChild();
+ for (Node type = typeNode.getFirstChild(); type != null; type = type.getNext()) {
appendTypeNode(sb, type);
if (type != last) {
sb.append(",");
}
}
sb.append(">");
- } else {
+ break;
+ case STRINGLIT:
sb.append(typeNode.getString());
+ if (typeNode.hasChildren()) {
+ appendTypeNode(sb, typeNode.getOnlyChild());
}
+ break;
+ default:
+ throw new IllegalStateException("Unexpected typeNode: " + typeNode);
}
}
