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 c2bee35552de82f6d3cafebe6b316c48c9abfb04 Author: Josh Tynjala <[email protected]> AuthorDate: Wed Oct 6 14:58:51 2021 -0700 formatter: fix unnecessary escape of quote if wrapping quote is different --- .../org/apache/royale/formatter/FORMATTER.java | 12 +++++-- .../apache/royale/formatter/TestStringLiteral.java | 42 ++++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java index 02dec76..7608a45 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java +++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java @@ -1241,14 +1241,20 @@ class FORMATTER { } private String formatLiteralString(String string) { - String charsToEscape = "\b\t\n\f\r\"\'\\"; - String escapeChars = "btnfr\"\'\\"; + String charsToEscape = "\b\t\n\f\r\\"; + String escapeChars = "btnfr\\"; int escapeIndex = -1; char currChar; StringBuilder builder = new StringBuilder(); for (int i = 0; i < string.length(); ++i) { currChar = string.charAt(i); - if (i == 0 || i == string.length() - 1) { + if (i == 0) { + charsToEscape += currChar; + escapeChars += currChar; + builder.append(currChar); + continue; + } + if (i == string.length() - 1) { builder.append(currChar); continue; } diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestStringLiteral.java b/formatter/src/test/java/org/apache/royale/formatter/TestStringLiteral.java index f32e5b9..437338f 100644 --- a/formatter/src/test/java/org/apache/royale/formatter/TestStringLiteral.java +++ b/formatter/src/test/java/org/apache/royale/formatter/TestStringLiteral.java @@ -63,7 +63,7 @@ public class TestStringLiteral extends BaseFormatterTests { } @Test - public void testWithEscapedDoubleQuote() { + public void testDoubleQuoteWithEscapedDoubleQuote() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -82,7 +82,26 @@ public class TestStringLiteral extends BaseFormatterTests { } @Test - public void testWithEscapedSingleQuote() { + public void testDoubleQuoteWithUnescapedSingleQuote() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = true; + formatter.insertSpaces = false; + String result = formatter.formatText( + // @formatter:off + "\"'\";", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "\"'\";", + // @formatter:on + result); + } + + @Test + public void testSingleQuoteWithEscapedSingleQuote() { FORMATTER formatter = new FORMATTER(); formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; formatter.placeOpenBraceOnNewLine = true; @@ -99,4 +118,23 @@ public class TestStringLiteral extends BaseFormatterTests { // @formatter:on result); } + + @Test + public void testSingleQuoteWithUnescapedDoubleQuote() { + FORMATTER formatter = new FORMATTER(); + formatter.insertSpaceAfterKeywordsInControlFlowStatements = true; + formatter.placeOpenBraceOnNewLine = true; + formatter.insertSpaces = false; + String result = formatter.formatText( + // @formatter:off + "'\"';", + // @formatter:on + problems + ); + assertEquals( + // @formatter:off + "'\"';", + // @formatter:on + result); + } }
