Skip to content

Commit 7022b7f

Browse files
committed
eslint: add unit tests, cleanup code
1 parent 9633967 commit 7022b7f

File tree

10 files changed

+309
-69
lines changed

10 files changed

+309
-69
lines changed

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020
import java.io.Serializable;
21-
import java.util.Collections;
22-
import java.util.Map;
23-
import java.util.Optional;
24-
import java.util.TreeMap;
2521

26-
import javax.annotation.Nonnull;
2722
import javax.annotation.Nullable;
2823

2924
import com.diffplug.spotless.FileSignature;
@@ -44,23 +39,20 @@ public class EslintConfig implements Serializable {
4439

4540
private final String eslintConfigJs;
4641

47-
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
48-
private final transient Map<File, Optional<String>> additionalConfigFiles; // key: source-file, value: target-remapping path relative to package.json (if needed)
49-
50-
private final FileSignature additionalConfigFilesSignature;
51-
52-
public EslintConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs, Map<File, Optional<String>> additionalConfigFiles) {
42+
public EslintConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs) {
5343
try {
5444
this.eslintConfigPath = eslintConfigPath;
5545
this.eslintConfigPathSignature = eslintConfigPath != null ? FileSignature.signAsList(this.eslintConfigPath) : FileSignature.signAsList();
5646
this.eslintConfigJs = eslintConfigJs;
57-
this.additionalConfigFiles = additionalConfigFiles != null ? new TreeMap<>(additionalConfigFiles) : Collections.emptyMap();
58-
this.additionalConfigFilesSignature = FileSignature.signAsList(this.additionalConfigFiles.keySet().toArray(new File[0]));
5947
} catch (IOException e) {
6048
throw ThrowingEx.asRuntime(e);
6149
}
6250
}
6351

52+
public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
53+
return new EslintConfig(eslintConfigPath, this.eslintConfigJs);
54+
}
55+
6456
@Nullable
6557
public File getEslintConfigPath() {
6658
return eslintConfigPath;
@@ -70,9 +62,4 @@ public File getEslintConfigPath() {
7062
public String getEslintConfigJs() {
7163
return eslintConfigJs;
7264
}
73-
74-
@Nonnull
75-
public Map<File, Optional<String>> getAdditionalConfigFiles() {
76-
return additionalConfigFiles;
77-
}
7865
}

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.LinkedHashMap;
2626
import java.util.Map;
2727
import java.util.Objects;
28-
import java.util.Optional;
2928
import java.util.TreeMap;
3029

3130
import javax.annotation.Nonnull;
@@ -152,17 +151,7 @@ private EslintConfig localCopyFiles(EslintConfig orig) {
152151
// eslint configs contain relative paths to additional config files (such as tsconfig.json e.g.)
153152
FormattedPrinter.SYSOUT.print("Copying config file <%s> to <%s> and using the copy", orig.getEslintConfigPath(), nodeModulesDir);
154153
File configFileCopy = NpmResourceHelper.copyFileToDir(orig.getEslintConfigPath(), nodeModulesDir);
155-
156-
for (Map.Entry<File, Optional<String>> additionalConfigFile : orig.getAdditionalConfigFiles().entrySet()) {
157-
FormattedPrinter.SYSOUT.print("Copying additional config file <%s> to <%s> at subpath <%s> and using the copy", additionalConfigFile.getKey(), nodeModulesDir, additionalConfigFile.getValue());
158-
159-
if (additionalConfigFile.getValue().isPresent()) {
160-
NpmResourceHelper.copyFileToDirAtSubpath(additionalConfigFile.getKey(), nodeModulesDir, additionalConfigFile.getValue().get());
161-
} else {
162-
NpmResourceHelper.copyFileToDir(additionalConfigFile.getKey(), nodeModulesDir);
163-
}
164-
}
165-
return new EslintConfig(configFileCopy, orig.getEslintConfigJs(), orig.getAdditionalConfigFiles());
154+
return orig.withEslintConfigPath(configFileCopy);
166155
}
167156

168157
@Override
@@ -226,8 +215,12 @@ private void setConfigToCallOptions(Map<FormatOption, Object> eslintCallOptions)
226215
}
227216
eslintCallOptions.put(FormatOption.NODE_MODULES_DIR, nodeModulesDir.getAbsolutePath());
228217

229-
// TODO (simschla, 09.12.22): maybe only add this if there is a typescript config active? (TBD: how to detect)
230-
eslintCallOptions.put(FormatOption.TS_CONFIG_ROOT_DIR, nodeModulesDir.toPath().relativize(projectDir.toPath()).toString());
218+
if (eslintConfig instanceof EslintTypescriptConfig) {
219+
// if we are a ts config, see if we need to use specific paths or use default projectDir
220+
File tsConfigFilePath = ((EslintTypescriptConfig) eslintConfig).getTypescriptConfigPath();
221+
File tsConfigRootDir = tsConfigFilePath != null ? tsConfigFilePath.getParentFile() : projectDir;
222+
eslintCallOptions.put(FormatOption.TS_CONFIG_ROOT_DIR, nodeModulesDir.getAbsoluteFile().toPath().relativize(tsConfigRootDir.getAbsoluteFile().toPath()).toString());
223+
}
231224
}
232225
}
233226
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.diffplug.spotless.npm;
2+
3+
import com.diffplug.spotless.FileSignature;
4+
import com.diffplug.spotless.ThrowingEx;
5+
6+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
7+
8+
import javax.annotation.Nullable;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
13+
public class EslintTypescriptConfig extends EslintConfig {
14+
15+
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
16+
@Nullable
17+
private final transient File typescriptConfigPath;
18+
19+
@SuppressWarnings("unused")
20+
private final FileSignature typescriptConfigPathSignature;
21+
22+
public EslintTypescriptConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs, @Nullable File typescriptConfigPath) {
23+
super(eslintConfigPath, eslintConfigJs);
24+
try {
25+
this.typescriptConfigPath = typescriptConfigPath;
26+
this.typescriptConfigPathSignature = typescriptConfigPath != null ? FileSignature.signAsList(this.typescriptConfigPath) : FileSignature.signAsList();
27+
} catch (IOException e) {
28+
throw ThrowingEx.asRuntime(e);
29+
}
30+
}
31+
32+
@Override
33+
public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) {
34+
return new EslintTypescriptConfig(eslintConfigPath, this.getEslintConfigJs(), this.typescriptConfigPath);
35+
}
36+
37+
@Nullable
38+
public File getTypescriptConfigPath() {
39+
return typescriptConfigPath;
40+
}
41+
}

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,6 @@ public class EslintFormatExtension extends NpmStepConfig<EslintFormatExtension>
621621
@Nullable
622622
String configJs = null;
623623

624-
List<AdditionalEslintConfig> additionalConfigs = new ArrayList<>();
625-
626624
public EslintFormatExtension(Map<String, String> devDependencies) {
627625
this.devDependencies.putAll(requireNonNull(devDependencies));
628626
}
@@ -645,10 +643,6 @@ public EslintFormatExtension configFile(Object configFilePath) {
645643
return this;
646644
}
647645

648-
protected void additionalConfigFilePath(Object sourceFile, String relativeTargetPath) {
649-
this.additionalConfigs.add(new AdditionalEslintConfig(sourceFile, relativeTargetPath));
650-
}
651-
652646
public FormatterStep createStep() {
653647
final Project project = getProject();
654648

@@ -661,28 +655,8 @@ public FormatterStep createStep() {
661655
eslintConfig());
662656
}
663657

664-
private EslintConfig eslintConfig() {
665-
return new EslintConfig(configFilePath != null ? getProject().file(configFilePath) : null, configJs, additionalConfigs());
666-
}
667-
668-
private Map<File, Optional<String>> additionalConfigs() {
669-
// convert additionalConfigs to a map explicitly allowing null values
670-
671-
return additionalConfigs.stream()
672-
.collect(Collectors.toMap(
673-
config -> getProject().file(config.configFilePath),
674-
config -> Optional.ofNullable(config.relativeTargetPath)));
675-
}
676-
}
677-
678-
private static class AdditionalEslintConfig {
679-
final Object configFilePath;
680-
681-
final String relativeTargetPath;
682-
683-
AdditionalEslintConfig(Object configFilePath, String relativeTargetPath) {
684-
this.configFilePath = configFilePath;
685-
this.relativeTargetPath = relativeTargetPath;
658+
protected EslintConfig eslintConfig() {
659+
return new EslintConfig(configFilePath != null ? getProject().file(configFilePath) : null, configJs);
686660
}
687661
}
688662

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
import javax.annotation.Nullable;
2828
import javax.inject.Inject;
2929

30+
import com.diffplug.spotless.npm.EslintConfig;
31+
32+
import com.diffplug.spotless.npm.EslintTypescriptConfig;
33+
3034
import org.gradle.api.Project;
3135

3236
import com.diffplug.spotless.FormatterStep;
@@ -193,6 +197,9 @@ public TypescriptEslintFormatExtension eslint(Map<String, String> devDependencie
193197

194198
public class TypescriptEslintFormatExtension extends EslintFormatExtension {
195199

200+
@Nullable
201+
Object typescriptConfigFilePath = null;
202+
196203
public TypescriptEslintFormatExtension(Map<String, String> devDependencies) {
197204
super(devDependencies);
198205
}
@@ -224,14 +231,16 @@ public TypescriptEslintFormatExtension styleGuide(String styleGuide) {
224231
}
225232

226233
public TypescriptEslintFormatExtension tsconfigFile(Object path) {
227-
return tsconfigFile(path, null);
228-
}
229-
230-
public TypescriptEslintFormatExtension tsconfigFile(Object path, String remapping) {
231-
additionalConfigFilePath(path, remapping);
234+
this.typescriptConfigFilePath = requireNonNull(path);
232235
replaceStep(createStep());
233236
return this;
234237
}
238+
239+
@Override
240+
protected EslintConfig eslintConfig() {
241+
EslintConfig config = super.eslintConfig();
242+
return new EslintTypescriptConfig(config.getEslintConfigPath(), config.getEslintConfigJs(), typescriptConfigFilePath != null ? getProject().file(typescriptConfigFilePath) : null);
243+
}
235244
}
236245

237246
@Override

testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Arrays;
2929
import java.util.List;
30+
import java.util.Optional;
3031
import java.util.function.Consumer;
3132
import java.util.function.UnaryOperator;
3233

@@ -86,11 +87,20 @@ protected void replace(String path, String toReplace, String replaceWith) throws
8687

8788
/** Returns the contents of the given file from the src/test/resources directory. */
8889
protected static String getTestResource(String filename) {
89-
URL url = ResourceHarness.class.getResource("/" + filename);
90-
if (url == null) {
91-
throw new IllegalArgumentException("No such resource " + filename);
90+
Optional<URL> resourceUrl = getTestResourceUrl(filename);
91+
if (resourceUrl.isPresent()) {
92+
return ThrowingEx.get(() -> LineEnding.toUnix(Resources.toString(resourceUrl.get(), StandardCharsets.UTF_8)));
9293
}
93-
return ThrowingEx.get(() -> LineEnding.toUnix(Resources.toString(url, StandardCharsets.UTF_8)));
94+
throw new IllegalArgumentException("No such resource " + filename);
95+
}
96+
97+
protected static boolean existsTestResource(String filename) {
98+
return getTestResourceUrl(filename).isPresent();
99+
}
100+
101+
private static Optional<URL> getTestResourceUrl(String filename) {
102+
URL url = ResourceHarness.class.getResource("/" + filename);
103+
return Optional.ofNullable(url);
94104
}
95105

96106
/** Returns Files (in a temporary folder) which has the contents of the given file from the src/test/resources directory. */

0 commit comments

Comments
 (0)