JS: don't write single return statement
@@ -32,6 +32,8 @@ import org.teavm.ast.AsyncMethodPart;
import org.teavm.ast.MethodNode;
import org.teavm.ast.MethodNodeVisitor;
import org.teavm.ast.RegularMethodNode;
+import org.teavm.ast.ReturnStatement;
+import org.teavm.ast.Statement;
import org.teavm.ast.VariableNode;
import org.teavm.backend.javascript.codegen.NamingOrderer;
import org.teavm.backend.javascript.codegen.NamingStrategy;
@@ -870,12 +872,16 @@ public class Renderer implements RenderingManager {
}
writer.append(statementRenderer.variableName(i));
}
- writer.append(")").ws().append("{").softNewLine().indent();
+ writer.append(")").ws().append("{").indent();
MethodBodyRenderer renderer = new MethodBodyRenderer(statementRenderer);
if (method.node != null) {
+ if (!isTrivialBody(method.node)) {
+ writer.softNewLine();
method.node.acceptVisitor(renderer);
+ }
} else {
+ writer.softNewLine();
renderer.renderNative(method);
}
@@ -890,6 +896,14 @@ public class Renderer implements RenderingManager {
longLibraryUsed |= statementRenderer.isLongLibraryUsed();
}
+ private static boolean isTrivialBody(MethodNode node) {
+ if (!(node instanceof RegularMethodNode)) {
+ return false;
+ }
+ Statement body = ((RegularMethodNode) node).getBody();
+ return body instanceof ReturnStatement && ((ReturnStatement) body).getResult() == null;
+ }
+
private void renderFunctionDeclaration(ScopedName name) throws IOException {
if (name.scoped) {
writer.append(naming.getScopeName()).append(".").append(name.value).ws().append("=").ws();
