Skip to content

Commit f8c3176

Browse files
committed
Do not convert text blocks when text contains carriage return
1 parent 89f6b7e commit f8c3176

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
106106
}
107107

108108
String content = contentSb.toString();
109+
if (content.contains("\r")) {
110+
// Carriage returns aren't yet carried over into text blocks, which always end with a newline \n
111+
return super.visitBinary(binary, ctx);
112+
}
109113

110114
if (!convertStringsWithoutNewlines && !containsNewLineInContent(content)) {
111115
return super.visitBinary(binary, ctx);

src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ class Test {
256256
);
257257
}
258258

259+
@Test
260+
void preferNoChangeIfCarriageReturnInContent() {
261+
rewriteRun(
262+
//language=java
263+
java(
264+
"""
265+
class Test {
266+
String text = "Deliberate\\r\\n" +
267+
"carriage return";
268+
}
269+
"""
270+
)
271+
);
272+
}
273+
259274
@Test
260275
void preferChangeIfNoNewLineInContent() {
261276
rewriteRun(
@@ -537,7 +552,7 @@ public static void main(String[] args) {
537552
@Test
538553
void textBlockDemo() {
539554
String s1 = """
540-
555+
541556
=========================================================
542557
\s
543558
Welcome to Spring Integration! \s

0 commit comments

Comments
 (0)