IfEmitter: improved handling of if, else if, and else bodies that are just a semicolon
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IfEmitter.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IfEmitter.java
index a5c987b..f437c53 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IfEmitter.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IfEmitter.java
@@ -68,14 +68,6 @@
{
emitElse(elseNode);
}
- // if no actual work is done in the if clause, and there are no else/elseif causes
- // emit an empty block. Closure doesn't like a plain semicolon.
- if (nodes.length == 0 && elseNode == null && conditional.getChild(1).getChildCount() == 0)
- {
- write(ASEmitterTokens.BLOCK_OPEN);
- writeNewline(ASEmitterTokens.BLOCK_CLOSE);
- }
-
}
protected void emitConditional(IConditionalNode node, boolean isElseIf)
@@ -96,7 +88,17 @@
write(ASEmitterTokens.PAREN_CLOSE);
IContainerNode xnode = (IContainerNode) node.getStatementContentsNode();
if (!EmitterUtils.isImplicit(xnode))
+ {
write(ASEmitterTokens.SPACE);
+ }
+ else if (xnode.getChildCount() == 0)
+ {
+ // if no actual work is done in the if body, emit an empty block.
+ // Closure doesn't like a plain semicolon.
+ write(ASEmitterTokens.SPACE);
+ write(ASEmitterTokens.BLOCK_OPEN);
+ write(ASEmitterTokens.BLOCK_CLOSE);
+ }
endMapping(node);
getWalker().walk(node.getChild(1)); // BlockNode
@@ -116,7 +118,18 @@
startMapping(node);
write(ASEmitterTokens.ELSE);
if (!isImplicit)
+ {
write(ASEmitterTokens.SPACE);
+ }
+ else if (cnode.getChildCount() == 0)
+ {
+ // if no actual work is done in the if body, emit an empty block.
+ // Closure doesn't like a plain semicolon.
+ write(ASEmitterTokens.SPACE);
+ write(ASEmitterTokens.BLOCK_OPEN);
+ write(ASEmitterTokens.BLOCK_CLOSE);
+ }
+
endMapping(node);
getWalker().walk(node); // TerminalNode
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/goog/TestGoogStatements.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/goog/TestGoogStatements.java
index 9df4078..b768192 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/goog/TestGoogStatements.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/goog/TestGoogStatements.java
@@ -312,14 +312,31 @@
}
@Test
- public void testVisitIf_NoClauses()
+ public void testVisitIf_NoBody()
{
IIfNode node = (IIfNode) getNode(
"if (a) ;", IIfNode.class);
asBlockWalker.visitIf(node);
- assertOut("if (a)\n{}\n");
+ assertOut("if (a) {}\n");
}
+ @Test
+ public void testVisitElseIf_NoBody()
+ {
+ IIfNode node = (IIfNode) getNode(
+ "if (a) b; else if (a);", IIfNode.class);
+ asBlockWalker.visitIf(node);
+ assertOut("if (a)\n\tb;\nelse if (a) {}\n");
+ }
+
+ @Test
+ public void testVisitElse_NoBody()
+ {
+ IIfNode node = (IIfNode) getNode(
+ "if (a) b; else;", IIfNode.class);
+ asBlockWalker.visitIf(node);
+ assertOut("if (a)\n\tb;\nelse {}\n");
+ }
@Override
protected IBackend createBackend()
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java
index 00648d5..8684665 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java
@@ -698,6 +698,25 @@
@Override
@Test
+ public void testVisitElseIf_NoBody()
+ {
+ IIfNode node = (IIfNode) getNode(
+ "if (a) b; else if (a);", IIfNode.class);
+ asBlockWalker.visitIf(node);
+ assertOut("if (a)\n b;\nelse if (a) {}\n");
+ }
+
+ @Test
+ public void testVisitElse_NoBody()
+ {
+ IIfNode node = (IIfNode) getNode(
+ "if (a) b; else;", IIfNode.class);
+ asBlockWalker.visitIf(node);
+ assertOut("if (a)\n b;\nelse {}\n");
+ }
+
+ @Override
+ @Test
public void testVisit()
{
IFileNode node = (IFileNode) getNode(