Skip to content

Commit 783ee73

Browse files
committed
Port Eslint to be compatible with serialization roundtrip.
1 parent 78b5108 commit 783ee73

File tree

4 files changed

+28
-54
lines changed

4 files changed

+28
-54
lines changed

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

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 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.
@@ -16,37 +16,20 @@
1616
package com.diffplug.spotless.npm;
1717

1818
import java.io.File;
19-
import java.io.IOException;
2019
import java.io.Serializable;
2120

2221
import javax.annotation.Nullable;
2322

2423
import com.diffplug.spotless.FileSignature;
25-
import com.diffplug.spotless.ThrowingEx;
26-
27-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2824

2925
public class EslintConfig implements Serializable {
30-
31-
private static final long serialVersionUID = -6196834313082791248L;
32-
33-
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
34-
@Nullable
35-
private final transient File eslintConfigPath;
36-
3726
@SuppressWarnings("unused")
38-
private final FileSignature eslintConfigPathSignature;
39-
27+
private final FileSignature.Promised eslintConfigPathSignature;
4028
private final String eslintConfigJs;
4129

4230
public EslintConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs) {
43-
try {
44-
this.eslintConfigPath = eslintConfigPath;
45-
this.eslintConfigPathSignature = eslintConfigPath != null ? FileSignature.signAsList(this.eslintConfigPath) : FileSignature.signAsList();
46-
this.eslintConfigJs = eslintConfigJs;
47-
} catch (IOException e) {
48-
throw ThrowingEx.asRuntime(e);
49-
}
31+
this.eslintConfigPathSignature = eslintConfigPath == null ? null : FileSignature.promise(eslintConfigPath);
32+
this.eslintConfigJs = eslintConfigJs;
5033
}
5134

5235
public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
@@ -55,7 +38,7 @@ public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
5538

5639
@Nullable
5740
public File getEslintConfigPath() {
58-
return eslintConfigPath;
41+
return eslintConfigPathSignature == null ? null : eslintConfigPathSignature.get().getOnlyFile();
5942
}
6043

6144
@Nullable
@@ -64,7 +47,7 @@ public String getEslintConfigJs() {
6447
}
6548

6649
public EslintConfig verify() {
67-
if (eslintConfigPath == null && eslintConfigJs == null) {
50+
if (eslintConfigPathSignature == null && eslintConfigJs == null) {
6851
throw new IllegalArgumentException("ESLint must be configured using either a configFile or a configJs - but both are null.");
6952
}
7053
return this;

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import com.diffplug.spotless.ThrowingEx;
4040
import com.diffplug.spotless.npm.EslintRestService.FormatOption;
4141

42-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
43-
4442
public class EslintFormatterStep {
4543

4644
private static final Logger logger = LoggerFactory.getLogger(EslintFormatterStep.class);
@@ -82,11 +80,8 @@ public static FormatterStep create(Map<String, String> devDependencies, Provisio
8280

8381
private static class State extends NpmFormatterStepStateBase implements Serializable {
8482

85-
private static final long serialVersionUID = -539537027004745812L;
8683
private final EslintConfig origEslintConfig;
87-
88-
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
89-
private transient EslintConfig eslintConfigInUse;
84+
private EslintConfig eslintConfigInUse;
9085

9186
State(String stepName, Map<String, String> devDependencies, File projectDir, File buildDir, File cacheDir, NpmPathResolver npmPathResolver, EslintConfig eslintConfig) throws IOException {
9287
super(stepName,
@@ -108,8 +103,8 @@ private static class State extends NpmFormatterStepStateBase implements Serializ
108103
}
109104

110105
@Override
111-
protected void prepareNodeServerLayout() throws IOException {
112-
super.prepareNodeServerLayout();
106+
protected void prepareNodeServerLayout(NodeServerLayout nodeServerLayout) throws IOException {
107+
super.prepareNodeServerLayout(nodeServerLayout);
113108
if (origEslintConfig.getEslintConfigPath() != null) {
114109
// If any config files are provided, we need to make sure they are at the same location as the node modules
115110
// as eslint will try to resolve plugin/config names relatively to the config file location and some
@@ -125,9 +120,10 @@ protected void prepareNodeServerLayout() throws IOException {
125120
public FormatterFunc createFormatterFunc() {
126121
try {
127122
logger.info("Creating formatter function (starting server)");
128-
ServerProcessInfo eslintRestServer = npmRunServer();
123+
Runtime runtime = toRuntime();
124+
ServerProcessInfo eslintRestServer = runtime.npmRunServer();
129125
EslintRestService restService = new EslintRestService(eslintRestServer.getBaseUrl());
130-
return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(locations.projectDir(), nodeServerLayout.nodeModulesDir(), eslintConfigInUse, restService));
126+
return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(locations.projectDir(), runtime.nodeServerLayout().nodeModulesDir(), eslintConfigInUse, restService));
131127
} catch (IOException e) {
132128
throw ThrowingEx.asRuntime(e);
133129
}
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 DiffPlug
2+
* Copyright 2022-2024 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.
@@ -16,43 +16,27 @@
1616
package com.diffplug.spotless.npm;
1717

1818
import java.io.File;
19-
import java.io.IOException;
2019

2120
import javax.annotation.Nullable;
2221

2322
import com.diffplug.spotless.FileSignature;
24-
import com.diffplug.spotless.ThrowingEx;
25-
26-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2723

2824
public class EslintTypescriptConfig extends EslintConfig {
29-
30-
private static final long serialVersionUID = -126864670181617006L;
31-
32-
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
33-
@Nullable
34-
private final transient File typescriptConfigPath;
35-
3625
@SuppressWarnings("unused")
37-
private final FileSignature typescriptConfigPathSignature;
26+
private final FileSignature.Promised typescriptConfigPathSignature;
3827

3928
public EslintTypescriptConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs, @Nullable File typescriptConfigPath) {
4029
super(eslintConfigPath, eslintConfigJs);
41-
try {
42-
this.typescriptConfigPath = typescriptConfigPath;
43-
this.typescriptConfigPathSignature = typescriptConfigPath != null ? FileSignature.signAsList(this.typescriptConfigPath) : FileSignature.signAsList();
44-
} catch (IOException e) {
45-
throw ThrowingEx.asRuntime(e);
46-
}
30+
this.typescriptConfigPathSignature = typescriptConfigPath != null ? FileSignature.promise(typescriptConfigPath) : null;
4731
}
4832

4933
@Override
5034
public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
51-
return new EslintTypescriptConfig(eslintConfigPath, this.getEslintConfigJs(), this.typescriptConfigPath);
35+
return new EslintTypescriptConfig(eslintConfigPath, this.getEslintConfigJs(), getTypescriptConfigPath());
5236
}
5337

5438
@Nullable
5539
public File getTypescriptConfigPath() {
56-
return typescriptConfigPath;
40+
return typescriptConfigPathSignature == null ? null : this.typescriptConfigPathSignature.get().getOnlyFile();
5741
}
5842
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,28 @@ public Runtime toRuntime() {
5858
return new Runtime(this);
5959
}
6060

61+
protected void prepareNodeServerLayout(NodeServerLayout layout) throws IOException {
62+
63+
}
64+
6165
public static class Runtime {
66+
private final NpmFormatterStepStateBase parent;
6267
private final NodeServerLayout nodeServerLayout;
6368
private final NodeServeApp nodeServeApp;
6469

6570
Runtime(NpmFormatterStepStateBase parent) {
71+
this.parent = parent;
6672
this.nodeServerLayout = new NodeServerLayout(parent.locations.buildDir(), parent.npmConfig.getPackageJsonContent());
6773
this.nodeServeApp = new NodeServeApp(nodeServerLayout, parent.npmConfig, parent.locations);
6874
}
6975

76+
public NodeServerLayout nodeServerLayout() {
77+
return nodeServerLayout;
78+
}
79+
7080
protected void prepareNodeServerLayout() throws IOException {
7181
nodeServeApp.prepareNodeAppLayout();
82+
parent.prepareNodeServerLayout(nodeServerLayout);
7283
}
7384

7485
protected void prepareNodeServer() throws IOException {

0 commit comments

Comments
 (0)