Skip to content

Commit 7631fb0

Browse files
authored
[prettier] Add support for prettier v3 plugin changes (#1802 fixes #1798)
2 parents 614e75b + 3754eb1 commit 7631fb0

File tree

17 files changed

+324
-59
lines changed

17 files changed

+324
-59
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111

1212
## [Unreleased]
1313

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+
1417
## [2.41.0] - 2023-08-29
1518
### Added
1619
* 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))

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

+17-1
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 lib/src/main/java/com/diffplug/spotless/npm/JsonWriter.java

+23-10
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

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

+1-2
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

+2-2
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

+1-1
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

+1-1
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

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66

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+
710
## [6.21.0] - 2023-08-29
811
### Added
912
* 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))

plugin-gradle/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,12 @@ Since spotless uses the actual npm prettier package behind the scenes, it is pos
10241024
spotless {
10251025
java {
10261026
prettier(['prettier': '2.8.8', 'prettier-plugin-java': '2.2.0']).config(['parser': 'java', 'tabWidth': 4])
1027+
// prettier(['prettier': '3.0.3', 'prettier-plugin-java': '2.3.0']).config(['parser': 'java', 'tabWidth': 4, 'plugins': ['prettier-plugin-java']]) // Prettier v3 requires additional 'plugins' config
10271028
}
10281029
format 'php', {
10291030
target 'src/**/*.php'
10301031
prettier(['prettier': '2.8.8', '@prettier/plugin-php': '0.19.6']).config(['parser': 'php', 'tabWidth': 3])
1032+
// prettier(['prettier': '3.0.3', '@prettier/plugin-php': '0.20.1']).config(['parser': 'php', 'tabWidth': 3, 'plugins': ['@prettier/plugin-php']]) // Prettier v3 requires additional 'plugins' config
10311033
}
10321034
}
10331035
```

0 commit comments

Comments
 (0)