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 edc2b2dda352f8c0c96a2259145030b70bf047c9 Author: Josh Tynjala <joshtynj...@apache.org> AuthorDate: Tue Dec 6 09:39:09 2022 -0800 formatter: fix ternary inside if condition --- .../apache/royale/formatter/ASTokenFormatter.java | 8 ++++---- .../royale/formatter/TestTernaryStatement.java | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) 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 3513d2d38..5b2eee400 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java +++ b/formatter/src/main/java/org/apache/royale/formatter/ASTokenFormatter.java @@ -770,7 +770,10 @@ public class ASTokenFormatter extends BaseTokenFormatter { break; } case ASTokenTypes.TOKEN_COLON: { - if (!inControlFlowStatement && !inVarOrConstDeclaration && !inFunctionDeclaration) { + if (ternaryStack > 0) { + ternaryStack--; + requiredSpace = true; + } else if (!inControlFlowStatement && !inVarOrConstDeclaration && !inFunctionDeclaration) { if (inCaseOrDefaultClause) { inCaseOrDefaultClause = false; caseOrDefaultBlockOpenPending = true; @@ -798,9 +801,6 @@ public class ASTokenFormatter extends BaseTokenFormatter { numRequiredNewLines = Math.max(numRequiredNewLines, 1); } } - } else if (ternaryStack > 0) { - ternaryStack--; - requiredSpace = true; } else { requiredSpace = true; } diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java b/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java index b324d9baa..9c15b1f13 100644 --- a/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java +++ b/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java @@ -82,4 +82,25 @@ public class TestTernaryStatement extends BaseFormatterTests { // @formatter:on result); } + + @Test + public void testInsideConditional() { + FormatterSettings settings = new FormatterSettings(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = true; + settings.placeOpenBraceOnNewLine = true; + settings.insertSpaces = false; + settings.collapseEmptyBlocks = true; + ASTokenFormatter formatter = new ASTokenFormatter(settings); + String result = formatter.format("file.as", + // @formatter:off + "if (condition ? statement : statement) {}", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "if (condition ? statement : statement) {}", + // @formatter:on + result); + } }