Skip to content

Commit 066eeb2

Browse files
committed
Merge branch 'main' into support-google-java-format-format-javadoc-option
2 parents d74264b + eb5dc61 commit 066eeb2

File tree

24 files changed

+375
-103
lines changed

24 files changed

+375
-103
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
14+
### Fixed
15+
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
16+
17+
## [2.41.0] - 2023-08-29
1318
### Added
1419
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
1520
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))

lib/src/main/java/com/diffplug/spotless/npm/JsonEscaper.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 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.
@@ -34,6 +34,22 @@ public static String jsonEscape(Object val) {
3434
if (val instanceof String) {
3535
return jsonEscape((String) val);
3636
}
37+
if (ListableAdapter.canAdapt(val)) {
38+
// create an array
39+
StringBuilder sb = new StringBuilder();
40+
sb.append('[');
41+
boolean first = true;
42+
for (Object o : ListableAdapter.adapt(val)) {
43+
if (first) {
44+
first = false;
45+
} else {
46+
sb.append(", ");
47+
}
48+
sb.append(jsonEscape(o));
49+
}
50+
sb.append(']');
51+
return sb.toString();
52+
}
3753
return val.toString();
3854
}
3955

lib/src/main/java/com/diffplug/spotless/npm/SimpleJsonWriter.java renamed to lib/src/main/java/com/diffplug/spotless/npm/JsonWriter.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 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.
@@ -28,33 +28,46 @@
2828

2929
import com.diffplug.spotless.ThrowingEx;
3030

31-
public class SimpleJsonWriter {
31+
class JsonWriter {
3232

3333
private final LinkedHashMap<String, Object> valueMap = new LinkedHashMap<>();
3434

35-
public static SimpleJsonWriter of(Map<String, ?> values) {
36-
SimpleJsonWriter writer = new SimpleJsonWriter();
35+
public static JsonWriter of(Map<String, ?> values) {
36+
JsonWriter writer = new JsonWriter();
3737
writer.putAll(values);
3838
return writer;
3939
}
4040

41-
SimpleJsonWriter putAll(Map<String, ?> values) {
41+
JsonWriter putAll(Map<String, ?> values) {
4242
verifyValues(values);
4343
this.valueMap.putAll(values);
4444
return this;
4545
}
4646

47-
SimpleJsonWriter put(String name, Object value) {
47+
JsonWriter put(String name, Object value) {
4848
verifyValues(Collections.singletonMap(name, value));
4949
this.valueMap.put(name, value);
5050
return this;
5151
}
5252

5353
private void verifyValues(Map<String, ?> values) {
54-
if (values.values()
55-
.stream()
56-
.anyMatch(val -> !(val instanceof String || val instanceof JsonRawValue || val instanceof Number || val instanceof Boolean))) {
57-
throw new IllegalArgumentException("Only values of type 'String', 'JsonRawValue', 'Number' and 'Boolean' are supported. You provided: " + values.values());
54+
for (Object value : values.values()) {
55+
verifyValue(value);
56+
}
57+
}
58+
59+
private void verifyValue(Object val) {
60+
if (val == null) {
61+
return;
62+
}
63+
if (ListableAdapter.canAdapt(val)) {
64+
for (Object o : ListableAdapter.adapt(val)) {
65+
verifyValue(o);
66+
}
67+
return;
68+
}
69+
if (!(val instanceof String || val instanceof JsonRawValue || val instanceof Number || val instanceof Boolean)) {
70+
throw new IllegalArgumentException("Only values of type 'String', 'JsonRawValue', 'Number' and 'Boolean' are supported. You provided: " + val);
5871
}
5972
}
6073

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.npm;
17+
18+
import java.util.Arrays;
19+
import java.util.Iterator;
20+
import java.util.List;
21+
import java.util.Objects;
22+
23+
import javax.annotation.Nonnull;
24+
25+
class ListableAdapter<T> implements Iterable<T> {
26+
27+
private final List<T> delegate;
28+
29+
@SuppressWarnings("unchecked")
30+
private ListableAdapter(Object delegate) {
31+
Objects.requireNonNull(delegate);
32+
if (!canAdapt(delegate)) {
33+
throw new IllegalArgumentException("Cannot create ListableAdapter from " + delegate.getClass() + ". Use canAdapt() to check first.");
34+
}
35+
if (delegate instanceof List) {
36+
this.delegate = (List<T>) delegate;
37+
} else if (delegate.getClass().isArray()) {
38+
this.delegate = Arrays.asList((T[]) delegate);
39+
} else {
40+
throw new IllegalArgumentException("Cannot create IterableAdapter from " + delegate.getClass());
41+
}
42+
}
43+
44+
static <T> Iterable<T> adapt(Object delegate) {
45+
return new ListableAdapter<>(delegate);
46+
}
47+
48+
@Override
49+
@Nonnull
50+
public Iterator<T> iterator() {
51+
return delegate.iterator();
52+
}
53+
54+
static boolean canAdapt(Object delegate) {
55+
Objects.requireNonNull(delegate);
56+
return delegate instanceof List || delegate.getClass().isArray();
57+
}
58+
}

lib/src/main/java/com/diffplug/spotless/npm/PrettierRestService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public String resolveConfig(File prettierConfigPath, Map<String, Object> prettie
3131
jsonProperties.put("prettier_config_path", prettierConfigPath.getAbsolutePath());
3232
}
3333
if (prettierConfigOptions != null) {
34-
jsonProperties.put("prettier_config_options", SimpleJsonWriter.of(prettierConfigOptions).toJsonRawValue());
35-
34+
jsonProperties.put("prettier_config_options", JsonWriter.of(prettierConfigOptions).toJsonRawValue());
3635
}
3736
return restClient.postJson("/prettier/config-options", jsonProperties);
3837
}

lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 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.
@@ -40,7 +40,7 @@ static SimpleRestClient forBaseUrl(String baseUrl) {
4040
}
4141

4242
String postJson(String endpoint, Map<String, Object> jsonParams) throws SimpleRestException {
43-
final SimpleJsonWriter jsonWriter = SimpleJsonWriter.of(jsonParams);
43+
final JsonWriter jsonWriter = JsonWriter.of(jsonParams);
4444
final String jsonString = jsonWriter.toJsonString();
4545

4646
return postJson(endpoint, jsonString);

lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private Map<String, Object> unifyOptions() {
107107
Map<String, Object> unified = new HashMap<>();
108108
if (!this.inlineTsFmtSettings.isEmpty()) {
109109
File targetFile = new File(this.buildDir, "inline-tsfmt.json");
110-
SimpleJsonWriter.of(this.inlineTsFmtSettings).toJsonFile(targetFile);
110+
JsonWriter.of(this.inlineTsFmtSettings).toJsonFile(targetFile);
111111
unified.put("tsfmt", true);
112112
unified.put("tsfmtFile", targetFile.getAbsolutePath());
113113
} else if (this.configFile != null) {

lib/src/main/java/com/diffplug/spotless/npm/TsFmtRestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public String format(String fileContent, Map<String, Object> configOptions) {
2828
Map<String, Object> jsonProperties = new LinkedHashMap<>();
2929
jsonProperties.put("file_content", fileContent);
3030
if (configOptions != null && !configOptions.isEmpty()) {
31-
jsonProperties.put("config_options", SimpleJsonWriter.of(configOptions).toJsonRawValue());
31+
jsonProperties.put("config_options", JsonWriter.of(configOptions).toJsonRawValue());
3232
}
3333

3434
return restClient.postJson("/tsfmt/format", jsonProperties);

plugin-gradle/CHANGES.md

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

55
## [Unreleased]
6+
7+
### Fixed
8+
* Added support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
9+
10+
## [6.21.0] - 2023-08-29
611
### Added
712
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
813
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))

0 commit comments

Comments
 (0)