Skip to content

Commit 75a1dbb

Browse files
committed
Write concatenated multi-line strings as text blocks
error-prone's StringConcatToTextBlock warned at 16 concatenations of string literals: the inputs and expected outputs in ModuleImportTest, and the template DebugRenderer writes to output.js. They are text blocks now, from error-prone's own fix formatted with formatDiff. The strings are the same: both classes hold the same string constants before and after, ./gradlew test passes, and so does :open-java-format:test on JDK 25, which runs the ModuleImportTest cases that JDK 21 skips.
1 parent 331d683 commit 75a1dbb

2 files changed

Lines changed: 81 additions & 22 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/DebugRenderer.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,15 @@ static void render(
6161
String formatterDecisionsJson) {
6262

6363
String javascript = String.format(
64-
"window.palantirJavaFormat = {\njavaInput: %s,\nops: %s,\ndoc: %s,\njavaOutput: %s,\n"
65-
+ "formatterDecisions: %s\n};\n",
64+
"""
65+
window.palantirJavaFormat = {
66+
javaInput: %s,
67+
ops: %s,
68+
doc: %s,
69+
javaOutput: %s,
70+
formatterDecisions: %s
71+
};
72+
""",
6673
jsonEscapedString(javaInput.getText()),
6774
opsJson(opsOutput),
6875
new JsonDocVisitor(finalState).visit(doc),

‎open-java-format/src/test/java/com/palantir/javaformat/java/ModuleImportTest.java‎

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,31 @@ private static void assertFormats(String input, String expected) throws Formatte
4949

5050
@Test
5151
public void formatsAndFixesImports() throws FormatterException {
52-
String input = "import module java.base;\n" + "class Example {}\n";
53-
String expected = "import module java.base;\n" + "\n" + "class Example {}\n";
52+
String input = """
53+
import module java.base;
54+
class Example {}
55+
""";
56+
String expected = """
57+
import module java.base;
58+
59+
class Example {}
60+
""";
5461
assertFormats(input, expected);
5562
}
5663

5764
@Test
5865
public void fixesImportsOnlyFromTheCommandLine() throws Exception {
5966
// The flag combination from the #1506 report, which failed with `Expected ; after import`.
60-
String input = "import module java.base;\n" + "class Example {}\n";
67+
String input = """
68+
import module java.base;
69+
class Example {}
70+
""";
6171
// Reordering puts a blank line after the import block; nothing else changes.
62-
String expected = "import module java.base;\n" + "\n" + "class Example {}\n";
72+
String expected = """
73+
import module java.base;
74+
75+
class Example {}
76+
""";
6377
StringWriter out = new StringWriter();
6478
StringWriter err = new StringWriter();
6579
Main main = new Main(
@@ -74,50 +88,88 @@ public void fixesImportsOnlyFromTheCommandLine() throws Exception {
7488

7589
@Test
7690
public void keepsACommentBetweenModuleAndTheModuleName() throws FormatterException {
77-
String input = "import module /* comment */ java.base;\n" + "class Example {}\n";
78-
String expected = "import module /* comment */ java.base;\n" + "\n" + "class Example {}\n";
91+
String input = """
92+
import module /* comment */ java.base;
93+
class Example {}
94+
""";
95+
String expected = """
96+
import module /* comment */ java.base;
97+
98+
class Example {}
99+
""";
79100
assertFormats(input, expected);
80101
}
81102

82103
@Test
83104
public void keepsACommentBetweenThePartsOfTheModuleName() throws FormatterException {
84-
String input = "import module java./* comment */base;\n" + "class Example {}\n";
105+
String input = """
106+
import module java./* comment */base;
107+
class Example {}
108+
""";
85109
// Reordering normalizes the whitespace around the comment and leaves it between the parts.
86-
String reordered = "import module java./* comment */ base;\n" + "\n" + "class Example {}\n";
110+
String reordered = """
111+
import module java./* comment */ base;
112+
113+
class Example {}
114+
""";
87115
assertThat(Formatter.create().fixImports(input)).isEqualTo(reordered);
88116
assertThat(Formatter.create().fixImports(reordered)).isEqualTo(reordered);
89117

90118
// Formatting then breaks the line after the dot, which is where the formatter puts a comment
91119
// in a qualified name; that output is stable too.
92-
String formatted = "import module java.\n" + "/* comment */ base;\n" + "\n" + "class Example {}\n";
120+
String formatted = """
121+
import module java.
122+
/* comment */ base;
123+
124+
class Example {}
125+
""";
93126
assertThat(Formatter.create().formatSourceAndFixImports(input)).isEqualTo(formatted);
94127
assertThat(Formatter.create().formatSourceAndFixImports(formatted)).isEqualTo(formatted);
95128
}
96129

97130
@Test
98131
public void normalizesWhitespaceInsideTheModuleName() throws FormatterException {
99-
String input = "import module java . base;\n" + "class Example {}\n";
100-
String expected = "import module java.base;\n" + "\n" + "class Example {}\n";
132+
String input = """
133+
import module java . base;
134+
class Example {}
135+
""";
136+
String expected = """
137+
import module java.base;
138+
139+
class Example {}
140+
""";
101141
assertFormats(input, expected);
102142
}
103143

104144
@Test
105145
public void keepsBothCopiesOfADuplicateThatCarriesAComment() throws FormatterException {
106146
// Identical declarations collapse; ones that differ are both kept, so no comment is dropped.
107-
String input = "import module /* explanation A */ java.base;\n"
108-
+ "import module /* explanation B */ java.base;\n"
109-
+ "class Example {}\n";
110-
String expected = "import module /* explanation A */ java.base;\n"
111-
+ "import module /* explanation B */ java.base;\n"
112-
+ "\n"
113-
+ "class Example {}\n";
147+
String input = """
148+
import module /* explanation A */ java.base;
149+
import module /* explanation B */ java.base;
150+
class Example {}
151+
""";
152+
String expected = """
153+
import module /* explanation A */ java.base;
154+
import module /* explanation B */ java.base;
155+
156+
class Example {}
157+
""";
114158
assertFormats(input, expected);
115159
}
116160

117161
@Test
118162
public void acceptsALineBreakAfterModule() throws FormatterException {
119-
String input = "import module\n" + " java.base;\n" + "class Example {}\n";
120-
String expected = "import module java.base;\n" + "\n" + "class Example {}\n";
163+
String input = """
164+
import module
165+
java.base;
166+
class Example {}
167+
""";
168+
String expected = """
169+
import module java.base;
170+
171+
class Example {}
172+
""";
121173
assertFormats(input, expected);
122174
}
123175
}

0 commit comments

Comments
 (0)