Skip to content

Commit 91cd139

Browse files
authored
Merge branch 'diffplug:main' into feat/skip-lines-before-license-header
2 parents 3a3583b + 1b2c449 commit 91cd139

File tree

28 files changed

+622
-370
lines changed

28 files changed

+622
-370
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Changes
1414
* Bump the dev version of Gradle from `7.5.1` to `7.6` ([#1409](https://github.com/diffplug/spotless/pull/1409))
1515
* We also removed the no-longer-required dependency `org.codehaus.groovy:groovy-xml`
16+
* Breaking changes to Spotless' internal testing infrastructure `testlib` ([#1443](https://github.com/diffplug/spotless/pull/1443))
17+
* `ResourceHarness` no longer has any duplicated functionality which was also present in `StepHarness`
18+
* `StepHarness` now operates on `Formatter` rather than a `FormatterStep`
19+
* `StepHarnessWithFile` now takes a `ResourceHarness` in its constructor to handle the file manipulation parts
20+
* Standardized that we test exception *messages*, not types, which will ease the transition to linting later on
1621

1722
## [2.31.1] - 2023-01-02
1823
### Fixed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ VER_SLF4J=[1.6,2.0[
2727
VER_DURIAN=1.2.0
2828
VER_JGIT=5.13.1.202206130422-r
2929
VER_JUNIT=5.9.1
30-
VER_ASSERTJ=3.23.1
30+
VER_ASSERTJ=3.24.1
3131
VER_MOCKITO=4.11.0
3232

3333
# Used for Maven Plugin

lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -26,24 +26,19 @@
2626
import org.eclipse.jgit.api.errors.GitAPIException;
2727
import org.junit.jupiter.api.Test;
2828

29-
import com.diffplug.common.base.Errors;
3029
import com.diffplug.common.base.StringPrinter;
3130
import com.diffplug.spotless.LineEnding;
3231
import com.diffplug.spotless.ResourceHarness;
3332

3433
class GitAttributesTest extends ResourceHarness {
3534
private List<File> testFiles(String prefix) {
36-
try {
37-
List<File> result = new ArrayList<>();
38-
for (String path : TEST_PATHS) {
39-
String prefixedPath = prefix + path;
40-
setFile(prefixedPath).toContent("");
41-
result.add(newFile(prefixedPath));
42-
}
43-
return result;
44-
} catch (IOException e) {
45-
throw Errors.asRuntime(e);
35+
List<File> result = new ArrayList<>();
36+
for (String path : TEST_PATHS) {
37+
String prefixedPath = prefix + path;
38+
setFile(prefixedPath).toContent("");
39+
result.add(newFile(prefixedPath));
4640
}
41+
return result;
4742
}
4843

4944
private List<File> testFiles() {
@@ -53,7 +48,7 @@ private List<File> testFiles() {
5348
private static final List<String> TEST_PATHS = Arrays.asList("someFile", "subfolder/someFile", "MANIFEST.MF", "subfolder/MANIFEST.MF");
5449

5550
@Test
56-
void cacheTest() throws IOException {
51+
void cacheTest() {
5752
setFile(".gitattributes").toContent(StringPrinter.buildStringFromLines(
5853
"* eol=lf",
5954
"*.MF eol=crlf"));
@@ -84,7 +79,7 @@ void cacheTest() throws IOException {
8479
}
8580

8681
@Test
87-
void policyTest() throws IOException {
82+
void policyTest() {
8883
setFile(".gitattributes").toContent(StringPrinter.buildStringFromLines(
8984
"* eol=lf",
9085
"*.MF eol=crlf"));
@@ -96,7 +91,7 @@ void policyTest() throws IOException {
9691
}
9792

9893
@Test
99-
void policyDefaultLineEndingTest() throws GitAPIException, IOException {
94+
void policyDefaultLineEndingTest() throws GitAPIException {
10095
Git git = Git.init().setDirectory(rootFolder()).call();
10196
git.close();
10297
setFile(".git/config").toContent(StringPrinter.buildStringFromLines(

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Prevent tool configurations from being resolved outside project ([#1447](https://github.com/diffplug/spotless/pull/1447) fixes [#1215](https://github.com/diffplug/spotless/issues/1215))
68

79
## [6.12.1] - 2023-01-02
810
### Fixed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -118,6 +118,8 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
118118
.forEach(config.getDependencies()::add);
119119
config.setDescription(mavenCoords.toString());
120120
config.setTransitive(withTransitives);
121+
config.setCanBeConsumed(false);
122+
config.setVisible(false);
121123
config.attributes(attr -> {
122124
attr.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.EXTERNAL));
123125
});

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
## [2.29.0] - 2023-01-02
88
### Added
99
* Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413))
10+
* Add JSON support ([#1446](https://github.com/diffplug/spotless/pull/1446))
1011
### Fixed
1112
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
1213
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))

plugin-maven/README.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ user@machine repo % mvn spotless:check
5858
- [Maven Pom](#maven-pom) ([sortPom](#sortpom))
5959
- [Markdown](#markdown) ([flexmark](#flexmark))
6060
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
61+
- [JSON](#json)
6162
- Multiple languages
6263
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
6364
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -128,7 +129,16 @@ To use it in your pom, just [add the Spotless dependency](https://search.maven.o
128129
Spotless consists of a list of formats (in the example above, `misc` and `java`), and each format has:
129130
- a `target` (the files to format), which you set with [`includes` and `excludes`](https://github.com/diffplug/spotless/blob/989abbecff4d8373c6111c1a98f359eadc532429/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java#L51-L55)
130131
- a list of `FormatterStep`, which are just `String -> String` functions, such as [`replace`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java), [`replaceRegex`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java), [`trimTrailingWhitespace`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java), [`indent`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java), [`prettier`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java), [`eclipseWtp`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EclipseWtp.java), and [`licenseHeader`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java).
131-
132+
- **order matters**, and this is good! (More info [here](https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md) and [here](https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-spotless-works))
133+
- For example, `googleJavaFormat` always indents with spaces, but some wish it had a tab mode
134+
- ```xml
135+
<googleJavaFormat/> // this works
136+
<indent><tabs>true</tabs><spacesPerTab>2</spacesPerTab></indent>
137+
```
138+
- ```xml
139+
<indent><tabs>true</tabs><spacesPerTab>2</spacesPerTab></indent>
140+
<googleJavaFormat/> // the tab indentation gets overwritten
141+
```
132142

133143
### Requirements
134144

@@ -178,6 +188,10 @@ any other maven phase (i.e. compile) then it can be configured as below;
178188
<include>src/test/java/**/*.java</include>
179189
</includes>
180190

191+
<googleJavaFormat /> <!-- has its own section below -->
192+
<eclipse /> <!-- has its own section below -->
193+
<prettier /> <!-- has its own section below -->
194+
181195
<importOrder /> <!-- standard import order -->
182196
<importOrder> <!-- or a custom ordering -->
183197
<wildcardsLast>false</wildcardsLast> <!-- Optional, default false. Sort wildcard import after specific imports -->
@@ -187,10 +201,6 @@ any other maven phase (i.e. compile) then it can be configured as below;
187201

188202
<removeUnusedImports /> <!-- self-explanatory -->
189203

190-
<googleJavaFormat /> <!-- has its own section below -->
191-
<eclipse /> <!-- has its own section below -->
192-
<prettier /> <!-- has its own section below -->
193-
194204
<formatAnnotations /> <!-- fixes formatting of type annotations, see below -->
195205

196206
<licenseHeader>
@@ -431,7 +441,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
431441
<configuration>
432442
<cpp>
433443
<includes> <!-- You have to set the target manually -->
434-
<include>src/native/**</inclue>
444+
<include>src/native/**</include>
435445
</includes>
436446

437447
<eclipseCdt /> <!-- has its own section below -->
@@ -700,6 +710,53 @@ The auto-discovery of config files (up the file tree) will not work when using t
700710

701711
For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.
702712

713+
## JSON
714+
715+
- `com.diffplug.spotless.maven.json.Json` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/json.java)
716+
717+
```xml
718+
<configuration>
719+
<json>
720+
<includes> <!-- You have to set the target manually -->
721+
<include>src/**/*.json</include>
722+
</includes>
723+
724+
<simple /> <!-- has its own section below -->
725+
<gson /> <!-- has its own section below -->
726+
</json>
727+
</configuration>
728+
```
729+
730+
### simple
731+
732+
Uses a JSON pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:
733+
734+
```xml
735+
<simple>
736+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
737+
</simple>
738+
```
739+
740+
### Gson
741+
742+
Uses Google Gson to also allow sorting by keys besides custom indentation - useful for i18n files.
743+
744+
```xml
745+
<gson>
746+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
747+
<sortByKeys>false</sortByKeys> <!-- optional: sort JSON by its keys -->
748+
<escapeHtml>false</indentSpaces> <!-- optional: escape HTML in values -->
749+
<version>2.8.1</version> <!-- optional: specify version -->
750+
</gson>
751+
```
752+
753+
Notes:
754+
* There's no option in Gson to leave HTML as-is (i.e. escaped HTML would remain escaped, raw would remain raw). Either
755+
all HTML characters are written escaped or none. Set `escapeHtml` if you prefer the former.
756+
* `sortByKeys` will apply lexicographic order on the keys of the input JSON. See the
757+
[javadoc of String](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String))
758+
for details.
759+
703760
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
704761

705762
## Prettier
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 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.maven.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.gson.GsonStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Gson implements FormatterStepFactory {
26+
private static final String DEFAULT_GSON_VERSION = "2.8.9";
27+
28+
@Parameter
29+
int indentSpaces = Json.DEFAULT_INDENTATION;
30+
31+
@Parameter
32+
boolean sortByKeys = false;
33+
34+
@Parameter
35+
boolean escapeHtml = false;
36+
37+
@Parameter
38+
String version = DEFAULT_GSON_VERSION;
39+
40+
@Override
41+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
42+
int indentSpaces = this.indentSpaces;
43+
return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner());
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 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.maven.json;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <json>...</json>} configuration element.
25+
*/
26+
public class Json extends FormatterFactory {
27+
public static final int DEFAULT_INDENTATION = 4;
28+
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return Collections.emptySet();
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return null;
37+
}
38+
39+
public void addSimple(Simple simple) {
40+
addStepFactory(simple);
41+
}
42+
43+
public void addGson(Gson gson) {
44+
addStepFactory(gson);
45+
}
46+
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023 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.maven.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.JsonSimpleStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Simple implements FormatterStepFactory {
26+
27+
@Parameter
28+
int indentSpaces = Json.DEFAULT_INDENTATION;
29+
30+
@Override
31+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
32+
int indentSpaces = this.indentSpaces;
33+
return JsonSimpleStep.create(indentSpaces, stepConfig.getProvisioner());
34+
}
35+
}

0 commit comments

Comments
 (0)