You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2021/09/09 16:30:34 UTC

[royale-compiler] branch develop updated: IfEmitter: improved handling of if, else if, and else bodies that are just a semicolon

This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new 3e23e66  IfEmitter: improved handling of if, else if, and else bodies that are just a semicolon
3e23e66 is described below

commit 3e23e66ec05c549baed81fc7ecc3e266225faed1
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Sep 9 09:30:19 2021 -0700

    IfEmitter: improved handling of if, else if, and else bodies that are just a semicolon
---
 .../compiler/internal/codegen/js/jx/IfEmitter.java | 29 ++++++++++++++++------
 .../codegen/js/goog/TestGoogStatements.java        | 21 ++++++++++++++--
 .../codegen/js/royale/TestRoyaleStatements.java    | 19 ++++++++++++++
 3 files changed, 59 insertions(+), 10 deletions(-)

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 @@ public class IfEmitter extends JSSubEmitter implements
         {
             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 @@ public class IfEmitter extends JSSubEmitter implements
         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 @@ public class IfEmitter extends JSSubEmitter implements
         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 @@ public class TestGoogStatements extends TestStatements
     }
 
     @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 @@ public class TestRoyaleStatements extends TestGoogStatements
 
     @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(