Skip to content

Commit ef81093

Browse files
cushonError Prone Team
authored andcommitted
Also reorder imports in MisformattedTestData
PiperOrigin-RevId: 888754531
1 parent aa994bd commit ef81093

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

core/src/main/java/com/google/errorprone/bugpatterns/MisformattedTestData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.google.errorprone.matchers.Matcher;
3737
import com.google.googlejavaformat.java.Formatter;
3838
import com.google.googlejavaformat.java.FormatterException;
39+
import com.google.googlejavaformat.java.ImportOrderer;
40+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
3941
import com.sun.source.tree.ExpressionTree;
4042
import com.sun.source.tree.LiteralTree;
4143
import com.sun.source.tree.MethodInvocationTree;
@@ -71,7 +73,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
7173
Formatter formatter = new Formatter();
7274
String formattedSource;
7375
try {
74-
formattedSource = formatter.formatSource(string);
76+
formattedSource = ImportOrderer.reorderImports(formatter.formatSource(string), Style.GOOGLE);
7577
} catch (FormatterException exception) {
7678
return NO_MATCH;
7779
}

core/src/test/java/com/google/errorprone/bugpatterns/MisformattedTestDataTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,59 @@ void method() {
266266
""")
267267
.doTest(TEXT_MATCH);
268268
}
269+
270+
@Test
271+
public void reorderImports() {
272+
refactoringHelper
273+
.addInputLines(
274+
"Test.java",
275+
"""
276+
import com.google.errorprone.BugCheckerRefactoringTestHelper;
277+
278+
class Test {
279+
void method(BugCheckerRefactoringTestHelper h) {
280+
h.addInputLines(
281+
"Test.java",
282+
\"""
283+
package foo;
284+
285+
import java.util.List;
286+
import static java.lang.Math.min;
287+
288+
class Test {
289+
int method(List<Integer> xs) {
290+
return min(xs.get(0), xs.get(1));
291+
}
292+
}
293+
\""");
294+
}
295+
}
296+
""")
297+
.addOutputLines(
298+
"Test.java",
299+
"""
300+
import com.google.errorprone.BugCheckerRefactoringTestHelper;
301+
302+
class Test {
303+
void method(BugCheckerRefactoringTestHelper h) {
304+
h.addInputLines(
305+
"Test.java",
306+
\"""
307+
package foo;
308+
309+
import static java.lang.Math.min;
310+
311+
import java.util.List;
312+
313+
class Test {
314+
int method(List<Integer> xs) {
315+
return min(xs.get(0), xs.get(1));
316+
}
317+
}
318+
\""");
319+
}
320+
}
321+
""")
322+
.doTest(TEXT_MATCH);
323+
}
269324
}

0 commit comments

Comments
 (0)