Skip to content

Commit 43593eb

Browse files
committed
Fix popState to honor expression start char
Fixes #647
1 parent c82e379 commit 43593eb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

smithy-utils/src/main/java/software/amazon/smithy/utils/CodeWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,11 @@ public CodeWriter popState() {
650650
// back into a parent state.
651651
popped.builder.setLength(0);
652652
popped.builder.append(result);
653-
} else {
653+
} else if (!result.isEmpty()) {
654654
// Sections can be added that are just placeholders. In those cases,
655655
// do not write anything unless the section emitted a non-empty string.
656-
// Note that this has already been formatted, so escape "$".
657-
writeOptional(result.replace("$", "$$"));
656+
// This has already been formatted, so writeWithNoFormatting is used.
657+
writeWithNoFormatting(result);
658658
}
659659
}
660660

smithy-utils/src/test/java/software/amazon/smithy/utils/CodeWriterTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,21 @@ public void poppedSectionsEscapeDollars() {
538538
assertThat(result, equalTo("$Hello\n"));
539539
}
540540

541+
@Test
542+
public void poppedSectionsEscapeCustomExpressionStarts() {
543+
CodeWriter writer = CodeWriter.createDefault();
544+
String result = writer
545+
.setExpressionStart('#')
546+
.pushState("foo")
547+
.write("##Hello")
548+
.write("$Hello")
549+
.write("$$Hello")
550+
.popState()
551+
.toString();
552+
553+
assertThat(result, equalTo("#Hello\n$Hello\n$$Hello\n"));
554+
}
555+
541556
@Test
542557
public void canWriteInline() {
543558
String result = CodeWriter.createDefault()

0 commit comments

Comments
 (0)