Skip to content

Commit fe636e8

Browse files
committed
using map as config api for npm formatter steps
as discussed in PR diffplug#283
1 parent b34204b commit fe636e8

File tree

11 files changed

+128
-606
lines changed

11 files changed

+128
-606
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/npm/NodeJSWrapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.diffplug.spotless.extra.npm;
1717

1818
import java.io.File;
19+
import java.util.Map;
1920
import java.util.Objects;
2021

2122
public class NodeJSWrapper extends ReflectiveObjectWrapper {
@@ -42,6 +43,13 @@ public V8ObjectWrapper createNewObject() {
4243
return objectWrapper;
4344
}
4445

46+
public V8ObjectWrapper createNewObject(Map<String, Object> values) {
47+
Objects.requireNonNull(values);
48+
V8ObjectWrapper obj = createNewObject();
49+
values.forEach(obj::add);
50+
return obj;
51+
}
52+
4553
public V8ArrayWrapper createNewArray(Object... elements) {
4654
final V8ArrayWrapper v8ArrayWrapper = this.createNewArray();
4755
for (Object element : elements) {

lib-extra/src/main/java/com/diffplug/spotless/extra/npm/NpmFormatterStepStateBase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@
2828

2929
import com.diffplug.spotless.*;
3030

31+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
32+
3133
public abstract class NpmFormatterStepStateBase implements Serializable {
3234

3335
private static final long serialVersionUID = -5849375492831208496L;
3436

35-
public final JarState jarState;
37+
private final JarState jarState;
3638

37-
public final FileSignature nodeModulesSignature;
39+
@SuppressWarnings("unused")
40+
private final FileSignature nodeModulesSignature;
3841

42+
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
3943
public final transient File nodeModulesDir;
4044

41-
public final NpmConfig npmConfig;
45+
private final NpmConfig npmConfig;
4246

43-
public final String stepName;
47+
private final String stepName;
4448

4549
protected NpmFormatterStepStateBase(String stepName, Provisioner provisioner, NpmConfig npmConfig, File buildDir, File npm) throws IOException {
4650
this.stepName = stepName;

lib-extra/src/main/java/com/diffplug/spotless/extra/npm/PrettierConfig.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,43 @@
1616
package com.diffplug.spotless.extra.npm;
1717

1818
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.Serializable;
21+
import java.util.Map;
22+
import java.util.TreeMap;
1923

20-
public class PrettierConfig {
24+
import com.diffplug.spotless.FileSignature;
25+
import com.diffplug.spotless.ThrowingEx;
2126

22-
private final File prettierConfigPath;
27+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2328

24-
private final PrettierOptions options;
29+
public class PrettierConfig implements Serializable {
2530

26-
public PrettierConfig(File prettierConfigPath, PrettierOptions options) {
27-
this.prettierConfigPath = prettierConfigPath;
28-
this.options = options == null ? PrettierOptions.allDefaults() : options;
31+
private static final long serialVersionUID = -8709340269833126583L;
32+
33+
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
34+
private final transient File prettierConfigPath;
35+
36+
@SuppressWarnings("unused")
37+
private final FileSignature prettierConfigPathSignature;
38+
39+
private final TreeMap<String, Object> options;
40+
41+
public PrettierConfig(File prettierConfigPath, Map<String, Object> options) {
42+
try {
43+
this.prettierConfigPath = prettierConfigPath;
44+
this.prettierConfigPathSignature = FileSignature.signAsList(this.prettierConfigPath);
45+
this.options = options == null ? new TreeMap<>() : new TreeMap<>(options);
46+
} catch (IOException e) {
47+
throw ThrowingEx.asRuntime(e);
48+
}
2949
}
3050

3151
public File getPrettierConfigPath() {
3252
return prettierConfigPath;
3353
}
3454

35-
public PrettierOptions getOptions() {
36-
return options;
55+
public Map<String, Object> getOptions() {
56+
return new TreeMap<>(this.options);
3757
}
3858
}

lib-extra/src/main/java/com/diffplug/spotless/extra/npm/PrettierFormatterStep.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.io.Serializable;
24+
import java.util.Map;
25+
import java.util.TreeMap;
2426

2527
import javax.annotation.Nonnull;
2628

@@ -66,7 +68,8 @@ public FormatterFunc createFormatterFunc() {
6668
final NodeJSWrapper nodeJSWrapper = nodeJSWrapper();
6769
final V8ObjectWrapper prettier = nodeJSWrapper.require(nodeModulePath());
6870

69-
final PrettierOptions[] resolvedPrettierOptions = new PrettierOptions[1];
71+
@SuppressWarnings("unchecked")
72+
final Map<String, Object>[] resolvedPrettierOptions = (Map<String, Object>[]) new Map[1];
7073
if (this.prettierConfig.getPrettierConfigPath() != null) {
7174
final Exception[] toThrow = new Exception[1];
7275
try (
@@ -76,9 +79,12 @@ public FormatterFunc createFormatterFunc() {
7679
if (configOptions == null) {
7780
toThrow[0] = new IllegalArgumentException("Cannot find or read config file " + this.prettierConfig.getPrettierConfigPath());
7881
} else {
79-
resolvedPrettierOptions[0] = PrettierOptions
80-
.fromV8Object(configOptions)
81-
.overrideWith(this.prettierConfig.getOptions());
82+
Map<String, Object> resolvedOptions = new TreeMap<>(V8ObjectUtilsWrapper.toMap(configOptions));
83+
resolvedOptions.putAll(this.prettierConfig.getOptions());
84+
toThrow[0] = validateOptions(resolvedOptions);
85+
if (toThrow[0] == null) {
86+
resolvedPrettierOptions[0] = resolvedOptions;
87+
}
8288
}
8389
} catch (Exception e) {
8490
toThrow[0] = e;
@@ -107,10 +113,7 @@ public FormatterFunc createFormatterFunc() {
107113
resolvedPrettierOptions[0] = this.prettierConfig.getOptions();
108114
}
109115

110-
// final V8ObjectWrapper prettierConfig = nodeJSWrapper.createNewObject()
111-
// .add("parser", "typescript");
112-
113-
final V8ObjectWrapper prettierConfig = resolvedPrettierOptions[0].toV8Object(nodeJSWrapper);
116+
final V8ObjectWrapper prettierConfig = nodeJSWrapper.createNewObject(resolvedPrettierOptions[0]);
114117

115118
return FormatterFunc.Closeable.of(() -> {
116119
System.out.println("RELEASING PRETTIER FORMATTER FUNCTION");
@@ -126,5 +129,12 @@ public FormatterFunc createFormatterFunc() {
126129
}
127130

128131
}
132+
133+
private Exception validateOptions(Map<String, Object> resolvedOptions) {
134+
if (resolvedOptions.containsKey("filePath")) {
135+
return new RuntimeException("option 'filePath' is not supported.)");
136+
}
137+
return null;
138+
}
129139
}
130140
}

0 commit comments

Comments
 (0)