Skip to content

Commit

Permalink
Do not escape newline in FixStringFormatExpressions (#261)
Browse files Browse the repository at this point in the history
* Do not escape newline in FixStringFormatExpressions

* Show progression of number of slashes
  • Loading branch information
timtebeek committed Feb 20, 2024
1 parent a049e1c commit b43b53d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ private static Expression replaceNewLineChars(Expression arg0) {
if (arg0 instanceof J.Literal) {
J.Literal fmt = (J.Literal) arg0;
if (fmt.getValue() != null) {
fmt = fmt.withValue(fmt.getValue().toString().replace("\n", "%n"));
fmt = fmt.withValue(fmt.getValue().toString().replaceAll("(?<!\\\\)\n", "%n"));
}
if (fmt.getValueSource() != null) {
fmt = fmt.withValueSource(fmt.getValueSource().replace("\\n", "%n"));
fmt = fmt.withValueSource(fmt.getValueSource().replaceAll("(?<!\\\\)\\\\n", "%n"));
}
arg0 = fmt;
return fmt;
}
return arg0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand All @@ -35,7 +36,6 @@ void newLineFormat() {
rewriteRun(
//language=java
java(
//language=java
"""
class T {
static {
Expand All @@ -58,7 +58,6 @@ class T {
@Test
void trimUnusedArguments() {
rewriteRun(
//language=java
//language=java
java(
"""
Expand All @@ -84,7 +83,6 @@ class T {
@Test
void allArgsAreUsed() {
rewriteRun(
//language=java
//language=java
java(
"""
Expand All @@ -98,4 +96,38 @@ class T {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/260")
void escapedNewline() {
rewriteRun(
//language=java
java(
"""
class A {
String foo(String bar) {
return ""\"
\\n
\\\\n
\\\\\\n
\\\\\\\\n
""\".formatted(bar);
}
}
""",
"""
class A {
String foo(String bar) {
return ""\"
%n
\\\\n
\\\\\\n
\\\\\\\\n
""\".formatted(bar);
}
}
"""
)
);
}
}

0 comments on commit b43b53d

Please sign in to comment.