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

commit 42f23ee49c48eb3ca70171a10511ceef9a9001ab
Author: Josh Tynjala <[email protected]>
AuthorDate: Thu Oct 31 10:34:37 2024 -0700

    formatter: fix exception when control flow statements are nested with mixed 
braces
---
 .../apache/royale/formatter/ASTokenFormatter.java  | 13 ++++++
 .../apache/royale/formatter/TestIfStatement.java   | 54 ++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git 
a/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java 
b/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java
index 817ab41d6..976f7a085 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java
@@ -332,6 +332,19 @@ public class ASTokenFormatter extends BaseTokenFormatter {
                                        }
                                        case ASTokenTypes.TOKEN_BLOCK_CLOSE: {
                                                boolean skipSwitchDecrease = 
false;
+                                               boolean checkNext = true;
+                                               while (!blockStack.isEmpty() && 
checkNext) {
+                                                       BlockStackItem 
stackItem = blockStack.get(blockStack.size() - 1);
+                                                       if 
(stackItem.controlFlow && !stackItem.braces) {
+                                                               // this block 
close token applies to a block
+                                                               // that 
contains nested control flow statements
+                                                               // without 
braces, so remove all of them
+                                                               
blockStack.remove(blockStack.size() - 1);
+                                                               indent = 
decreaseIndent(indent);
+                                                               continue;
+                                                       }
+                                                       checkNext = false;
+                                               }
                                                if (!blockStack.isEmpty()) {
                                                        BlockStackItem 
stackItem = blockStack.get(blockStack.size() - 1);
                                                        if 
(stackItem.blockDepth <= 1) {
diff --git 
a/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java 
b/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java
index 470bae98c..e3fd79f49 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestIfStatement.java
@@ -703,6 +703,60 @@ public class TestIfStatement extends BaseFormatterTests {
                                result);
        }
 
+       @Test
+       public void testNestedBodiesWithMixedParentheses1() {
+               FormatterSettings settings = new FormatterSettings();
+               settings.insertSpaceAfterKeywordsInControlFlowStatements = true;
+               settings.placeOpenBraceOnNewLine = true;
+               settings.insertSpaces = false;
+               ASTokenFormatter formatter = new ASTokenFormatter(settings);
+               System.err.println("**** BEFORE");
+               String result = formatter.format("file.as",
+               // @formatter:off
+                       "{if (condition1) if (condition2) {statement;}}",
+                       // @formatter:on
+                       problems
+               );
+               System.err.println("**** AFTER");
+               assertEquals(
+               // @formatter:off
+                               "{\n" +
+                               "\tif (condition1)\n" +
+                               "\t\tif (condition2)\n" +
+                               "\t\t{\n" +
+                               "\t\t\tstatement;\n" +
+                               "\t\t}\n" +
+                               "}",
+                               // @formatter:on
+                               result);
+       }
+
+       @Test
+       public void testNestedBodiesWithMixedParentheses2() {
+               FormatterSettings settings = new FormatterSettings();
+               settings.insertSpaceAfterKeywordsInControlFlowStatements = true;
+               settings.placeOpenBraceOnNewLine = true;
+               settings.insertSpaces = false;
+               ASTokenFormatter formatter = new ASTokenFormatter(settings);
+               String result = formatter.format("file.as",
+               // @formatter:off
+                       "{if (condition1) { if (condition2) statement; }}",
+                       // @formatter:on
+                       problems
+               );
+               assertEquals(
+               // @formatter:off
+                               "{\n" +
+                               "\tif (condition1)\n" +
+                               "\t{\n" +
+                               "\t\tif (condition2)\n" +
+                               "\t\t\tstatement;\n" +
+                               "\t}\n" +
+                               "}",
+                               // @formatter:on
+                               result);
+       }
+
        @Test
        public void testCollapseEmptyBlock1() {
                FormatterSettings settings = new FormatterSettings();

Reply via email to