Skip to content

Commit dc969c1

Browse files
authored
Merge branch 'main' into update-for-biome-2.x
2 parents 8fc0604 + 1ed7249 commit dc969c1

File tree

9 files changed

+108
-49
lines changed

9 files changed

+108
-49
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Added
1414
* Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020), [#2535](https://github.com/diffplug/spotless/pull/2535))
1515
* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
16+
* Use `palantir-java-format` as default formatter in `RemoveUnusedImportsStep`. ([#2541](https://github.com/diffplug/spotless/pull/2541))
17+
* scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460))
1618
### Fixed
1719
* Fix biome formatter for new major release 2.x of biome ([#2537](https://github.com/diffplug/spotless/pull/2537))
1820
* Make sure npm-based formatters use the correct `node_modules` directory when running in parallel. ([#2542](https://github.com/diffplug/spotless/pull/2542))

lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,12 +22,12 @@
2222
import com.diffplug.spotless.FormatterStep;
2323
import com.diffplug.spotless.Provisioner;
2424

25-
/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
25+
/** Uses palantir-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
2626
public class RemoveUnusedImportsStep implements Serializable {
2727
private static final long serialVersionUID = 1L;
2828
static final String NAME = "removeUnusedImports";
2929

30-
static final String GJF = "google-java-format";
30+
static final String DEFAULT_FORMATTER = "palantir-java-format";
3131
static final String CLEANTHAT = "cleanthat-javaparser-unnecessaryimport";
3232

3333
// https://github.com/solven-eu/cleanthat/blob/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryImport.java
@@ -37,18 +37,17 @@ public class RemoveUnusedImportsStep implements Serializable {
3737
private RemoveUnusedImportsStep() {}
3838

3939
public static String defaultFormatter() {
40-
return GJF;
40+
return DEFAULT_FORMATTER;
4141
}
4242

4343
public static FormatterStep create(Provisioner provisioner) {
44-
// The default importRemover is GJF
45-
return create(GJF, provisioner);
44+
return create(DEFAULT_FORMATTER, provisioner);
4645
}
4746

4847
public static FormatterStep create(String unusedImportRemover, Provisioner provisioner) {
4948
Objects.requireNonNull(provisioner, "provisioner");
5049
switch (unusedImportRemover) {
51-
case GJF:
50+
case DEFAULT_FORMATTER:
5251
return GoogleJavaFormatStep.createRemoveUnusedImportsOnly(provisioner);
5352
case CLEANTHAT:
5453
return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner);

lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 DiffPlug
2+
* Copyright 2022-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.nio.file.Files;
2222

2323
import org.scalafmt.Scalafmt;
24+
import org.scalafmt.Versions;
2425
import org.scalafmt.config.ScalafmtConfig;
2526
import org.scalafmt.config.ScalafmtConfig$;
2627

@@ -45,6 +46,16 @@ public ScalafmtFormatterFunc(FileSignature configSignature) throws Exception {
4546
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
4647
config = Scalafmt.parseHoconConfig(configStr).get();
4748
}
49+
50+
// This check is to raise awareness to the user that the version of the config file is currently not used.
51+
// Context: https://github.com/diffplug/spotless/issues/2460
52+
// This check should be removed when Spotless dynamically loads the proper version of the Scalafmt library.
53+
String scalafmtLibraryVersion = Versions.version();
54+
if (!config.version().equals(scalafmtLibraryVersion)) {
55+
throw new IllegalArgumentException(
56+
"Spotless is using " + scalafmtLibraryVersion + " but the config file declares " + config.version() +
57+
". Both must match. Update the version declared in the plugin's settings and/or the config file.");
58+
}
4859
}
4960

5061
@Override

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
### Changed
1313
* Bump internal dependencies for npm-based formatters ([#2542](https://github.com/diffplug/spotless/pull/2542))
1414

15+
### Changed
16+
* scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460))
17+
1518
## [7.0.4] - 2025-05-27
1619
### Fixed
1720
* Fix `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487))

plugin-maven/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
### Changed
1313
* Bump internal dependencies for npm-based formatters ([#2542](https://github.com/diffplug/spotless/pull/2542))
1414

15+
### Changed
16+
* scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460))
17+
1518
## [2.44.5] - 2025-05-27
1619
### Changed
1720
* Bump default `eclipse` version to latest `4.34` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458))
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2016-2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.java;
17+
18+
import static com.diffplug.spotless.java.RemoveUnusedImportsStep.DEFAULT_FORMATTER;
19+
import static com.diffplug.spotless.java.RemoveUnusedImportsStep.NAME;
20+
import static com.diffplug.spotless.java.RemoveUnusedImportsStep.defaultFormatter;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.util.Collections;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
29+
30+
class RemoveUnusedImportsStepTest extends MavenIntegrationHarness {
31+
32+
@Test
33+
void testRemoveUnusedImports() throws Exception {
34+
writePomWithJavaSteps("<removeUnusedImports/>");
35+
36+
String path = "src/main/java/test.java";
37+
setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test");
38+
mavenRunner().withArguments("spotless:apply").runNoError();
39+
assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test");
40+
}
41+
42+
@Test
43+
void testDefaults() {
44+
assertEquals("palantir-java-format", defaultFormatter());
45+
assertEquals("palantir-java-format", DEFAULT_FORMATTER);
46+
assertEquals("removeUnusedImports", NAME);
47+
}
48+
49+
@Test
50+
void testCreateWithDefaultFormatter() {
51+
assertEquals(NAME, RemoveUnusedImportsStep.create((groupArtifact, version) -> Collections.emptySet()).getName());
52+
}
53+
54+
@Test
55+
void testCreateWithPalantirFormatter() {
56+
assertEquals(NAME, RemoveUnusedImportsStep.create("palantir-java-format", (groupArtifact, version) -> Collections.emptySet()).getName());
57+
}
58+
59+
@Test
60+
void testCreateWithCleanthatFormatter() {
61+
assertEquals(NAME, RemoveUnusedImportsStep.create("cleanthat-javaparser-unnecessaryimport", (groupArtifact, version) -> Collections.emptySet()).getName());
62+
}
63+
64+
@Test
65+
void testCreateWithInvalidFormatter() {
66+
assertThrows(IllegalArgumentException.class, () -> RemoveUnusedImportsStep.create("invalid-formatter", (groupArtifact, version) -> Collections.emptySet()));
67+
}
68+
69+
@Test
70+
void testCreateWithNullProvisioner() {
71+
assertThrows(NullPointerException.class, () -> RemoveUnusedImportsStep.create("palantir-java-format", null));
72+
}
73+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
class RemoveUnusedImportsStep_withGoogleJavaFormatTest {
2626
@Test
2727
void behavior() throws Exception {
28-
FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral());
28+
FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral());
2929
StepHarness.forStep(step)
3030
.testResource("java/removeunusedimports/JavaCodeUnformatted.test", "java/removeunusedimports/JavaCodeFormatted.test")
3131
.testResource("java/removeunusedimports/JavaCodeWithLicenseUnformatted.test", "java/removeunusedimports/JavaCodeWithLicenseFormatted.test")
@@ -48,7 +48,7 @@ protected void setupTest(API api) {
4848

4949
@Override
5050
protected FormatterStep create() {
51-
return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral());
51+
return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral());
5252
}
5353
}.testEquals();
5454
}

testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,17 +48,18 @@ void behaviorFileOverride() {
4848
}
4949

5050
@Test
51-
void behaviorDefaultConfigVersion_3_0_0() {
51+
void behaviorNoConfigFile() {
5252
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null);
5353
StepHarness.forStep(step)
5454
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.clean");
5555
}
5656

5757
@Test
58-
void behaviorCustomConfigVersion_3_0_0() {
58+
void behaviorConfigFileVersionDoesnotMatchLibrary() {
5959
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf"));
60-
StepHarness.forStep(step)
61-
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.cleanWithCustomConf");
60+
Assertions.assertThatThrownBy(() -> {
61+
step.format("", new File(""));
62+
}).cause().message().contains("Spotless is using 3.0.0 but the config file declares 3.8.1. Both must match. Update the version declared in the plugin's settings and/or the config file.");
6263
}
6364

6465
@Test

0 commit comments

Comments
 (0)