Skip to content

Commit 31462a2

Browse files
committed
eslint: initial maven support
1 parent 218db79 commit 31462a2

File tree

9 files changed

+221
-29
lines changed

9 files changed

+221
-29
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.Serializable;
23+
import java.util.Arrays;
2324
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.LinkedHashMap;
2627
import java.util.Map;
2728
import java.util.Objects;
2829
import java.util.TreeMap;
30+
import java.util.function.Predicate;
2931

3032
import javax.annotation.Nonnull;
3133

@@ -50,7 +52,7 @@ public class EslintFormatterStep {
5052
public enum PopularStyleGuide {
5153
TS_STANDARD_WITH_TYPESCRIPT("standard-with-typescript") {
5254
@Override
53-
public Map<String, String> devDependencies() {
55+
public @Nonnull Map<String, String> devDependencies() {
5456
Map<String, String> dependencies = new LinkedHashMap<>();
5557
dependencies.put("eslint-config-standard-with-typescript", "^24.0.0");
5658
dependencies.put("eslint-plugin-import", "^2.26.0");
@@ -61,7 +63,7 @@ public Map<String, String> devDependencies() {
6163
},
6264
TS_XO_TYPESCRIPT("xo-typescript") {
6365
@Override
64-
public Map<String, String> devDependencies() {
66+
public @Nonnull Map<String, String> devDependencies() {
6567
Map<String, String> dependencies = new LinkedHashMap<>();
6668
dependencies.put("eslint-config-xo", "^0.43.1");
6769
dependencies.put("eslint-config-xo-typescript", "^0.55.1");
@@ -70,7 +72,7 @@ public Map<String, String> devDependencies() {
7072
},
7173
JS_AIRBNB("airbnb") {
7274
@Override
73-
public Map<String, String> devDependencies() {
75+
public @Nonnull Map<String, String> devDependencies() {
7476
Map<String, String> dependencies = new LinkedHashMap<>();
7577
dependencies.put("eslint-config-airbnb-base", "^15.0.0");
7678
dependencies.put("eslint-plugin-import", "^2.26.0");
@@ -79,15 +81,15 @@ public Map<String, String> devDependencies() {
7981
},
8082
JS_GOOGLE("google") {
8183
@Override
82-
public Map<String, String> devDependencies() {
84+
public @Nonnull Map<String, String> devDependencies() {
8385
Map<String, String> dependencies = new LinkedHashMap<>();
8486
dependencies.put("eslint-config-google", "^0.14.0");
8587
return dependencies;
8688
}
8789
},
8890
JS_STANDARD("standard") {
8991
@Override
90-
public Map<String, String> devDependencies() {
92+
public @Nonnull Map<String, String> devDependencies() {
9193
Map<String, String> dependencies = new LinkedHashMap<>();
9294
dependencies.put("eslint-config-standard", "^17.0.0");
9395
dependencies.put("eslint-plugin-import", "^2.26.0");
@@ -98,7 +100,7 @@ public Map<String, String> devDependencies() {
98100
},
99101
JS_XO("xo") {
100102
@Override
101-
public Map<String, String> devDependencies() {
103+
public @Nonnull Map<String, String> devDependencies() {
102104
Map<String, String> dependencies = new LinkedHashMap<>();
103105
dependencies.put("eslint-config-xo", "^0.43.1");
104106
return dependencies;
@@ -115,7 +117,7 @@ public String getPopularStyleGuideName() {
115117
return popularStyleGuideName;
116118
}
117119

118-
public abstract Map<String, String> devDependencies();
120+
public abstract @Nonnull Map<String, String> devDependencies();
119121

120122
public static PopularStyleGuide fromNameOrNull(String popularStyleGuideName) {
121123
for (PopularStyleGuide popularStyleGuide : PopularStyleGuide.values()) {
@@ -125,6 +127,15 @@ public static PopularStyleGuide fromNameOrNull(String popularStyleGuideName) {
125127
}
126128
return null;
127129
}
130+
131+
public static String getPopularStyleGuideNames(Predicate<PopularStyleGuide> filter) {
132+
// collect matching style guide names using stream
133+
return Arrays.stream(PopularStyleGuide.values())
134+
.filter(filter)
135+
.map(PopularStyleGuide::getPopularStyleGuideName)
136+
.sorted()
137+
.collect(java.util.stream.Collectors.joining(", "));
138+
}
128139
}
129140

130141
public static Map<String, String> defaultDevDependenciesForTypescript() {

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
import static java.util.Objects.requireNonNull;
1919

20-
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.LinkedHashMap;
2322
import java.util.Map;
2423
import java.util.function.Consumer;
25-
import java.util.stream.Collectors;
2624

2725
import javax.annotation.Nullable;
2826
import javax.inject.Inject;
@@ -100,9 +98,8 @@ public T styleGuide(String styleGuide) {
10098
PopularStyleGuide popularStyleGuide = PopularStyleGuide.fromNameOrNull(styleGuide);
10199

102100
verifyStyleGuideIsSupported(styleGuide, popularStyleGuide);
103-
devDependencies(popularStyleGuide.devDependencies());
104-
replaceStep();
105-
return (T) this;
101+
assert popularStyleGuide != null;
102+
return devDependencies(popularStyleGuide.devDependencies());
106103
}
107104

108105
protected abstract void verifyStyleGuideIsSupported(String styleGuideName, PopularStyleGuide popularStyleGuide);
@@ -129,12 +126,7 @@ public FormatterStep createStep() {
129126
@Override
130127
protected void verifyStyleGuideIsSupported(String styleGuideName, PopularStyleGuide popularStyleGuide) {
131128
if (!isJsStyleGuide(popularStyleGuide)) {
132-
throw new IllegalArgumentException("Unknown style guide: " + styleGuideName + ". Known javascript style guides: "
133-
+ Arrays.stream(PopularStyleGuide.values())
134-
.filter(this::isJsStyleGuide)
135-
.map(PopularStyleGuide::getPopularStyleGuideName)
136-
.sorted()
137-
.collect(Collectors.joining(", ")));
129+
throw new IllegalArgumentException("Unknown style guide: " + styleGuideName + ". Known javascript style guides: " + PopularStyleGuide.getPopularStyleGuideNames(this::isJsStyleGuide));
138130
}
139131
}
140132

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
import static java.util.Objects.requireNonNull;
1919

20-
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.Map;
2322
import java.util.Objects;
2423
import java.util.TreeMap;
25-
import java.util.stream.Collectors;
2624

2725
import javax.annotation.Nullable;
2826
import javax.inject.Inject;
@@ -209,12 +207,7 @@ public TypescriptEslintConfig tsconfigFile(Object path) {
209207
@Override
210208
protected void verifyStyleGuideIsSupported(String styleGuideName, PopularStyleGuide popularStyleGuide) {
211209
if (!isTsStyleGuide(popularStyleGuide)) {
212-
throw new IllegalArgumentException("Unknown style guide: " + styleGuideName + ". Known typescript style guides: "
213-
+ Arrays.stream(EslintFormatterStep.PopularStyleGuide.values())
214-
.filter(this::isTsStyleGuide)
215-
.map(PopularStyleGuide::getPopularStyleGuideName)
216-
.sorted()
217-
.collect(Collectors.joining(", ")));
210+
throw new IllegalArgumentException("Unknown style guide: " + styleGuideName + ". Known typescript style guides: " + PopularStyleGuide.getPopularStyleGuideNames(this::isTsStyleGuide));
218211
}
219212
}
220213

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.diffplug.spotless.maven.incremental.UpToDateChecker;
6565
import com.diffplug.spotless.maven.incremental.UpToDateChecking;
6666
import com.diffplug.spotless.maven.java.Java;
67+
import com.diffplug.spotless.maven.javascript.Javascript;
6768
import com.diffplug.spotless.maven.kotlin.Kotlin;
6869
import com.diffplug.spotless.maven.markdown.Markdown;
6970
import com.diffplug.spotless.maven.pom.Pom;
@@ -152,6 +153,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
152153
@Parameter
153154
private Typescript typescript;
154155

156+
@Parameter
157+
private Javascript javascript;
158+
155159
@Parameter
156160
private Antlr4 antlr4;
157161

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2016-2022 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.maven.javascript;
17+
18+
import java.io.File;
19+
import java.util.Map;
20+
import java.util.TreeMap;
21+
22+
import org.apache.maven.plugins.annotations.Parameter;
23+
24+
import com.diffplug.spotless.FormatterStep;
25+
import com.diffplug.spotless.maven.FormatterStepConfig;
26+
import com.diffplug.spotless.maven.npm.AbstractNpmFormatterStepFactory;
27+
import com.diffplug.spotless.npm.EslintConfig;
28+
import com.diffplug.spotless.npm.EslintFormatterStep;
29+
import com.diffplug.spotless.npm.NpmPathResolver;
30+
31+
public class EslintJs extends AbstractNpmFormatterStepFactory {
32+
33+
@Parameter
34+
private String configFile;
35+
36+
@Parameter
37+
private String configJs;
38+
39+
@Parameter
40+
private String styleGuide;
41+
42+
@Parameter
43+
protected String eslintVersion;
44+
45+
@Parameter
46+
private Map<String, String> devDependencies;
47+
48+
@Override
49+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
50+
Map<String, String> devDependencies = new TreeMap<>();
51+
if (this.devDependencies != null) {
52+
devDependencies.putAll(this.devDependencies);
53+
} else {
54+
Map<String, String> defaultDependencies = createDefaultDependencies();
55+
devDependencies.putAll(defaultDependencies);
56+
}
57+
58+
addStyleGuideDevDependencies(devDependencies);
59+
60+
File buildDir = buildDir(stepConfig);
61+
File baseDir = baseDir(stepConfig);
62+
NpmPathResolver npmPathResolver = npmPathResolver(stepConfig);
63+
return EslintFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, npmPathResolver, eslintConfig(stepConfig));
64+
}
65+
66+
protected EslintConfig eslintConfig(FormatterStepConfig stepConfig) {
67+
return new EslintConfig(this.configFile != null ? stepConfig.getFileLocator().locateFile(this.configFile) : null, this.configJs);
68+
}
69+
70+
private void addStyleGuideDevDependencies(Map<String, String> devDependencies) {
71+
if (this.styleGuide != null) {
72+
EslintFormatterStep.PopularStyleGuide styleGuide = EslintFormatterStep.PopularStyleGuide.valueOf(this.styleGuide);
73+
validateStyleGuide(styleGuide);
74+
devDependencies.putAll(styleGuide.devDependencies());
75+
}
76+
}
77+
78+
private void validateStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) {
79+
if (styleGuide == null) {
80+
throw new IllegalArgumentException("StyleGuide '" + this.styleGuide + "' is not supported. Supported style guides: " + supportedStyleGuides());
81+
}
82+
if (!isValidStyleGuide(styleGuide)) {
83+
throw new IllegalArgumentException("StyleGuide must be of correct type but is: " + styleGuide + ". Use one of the following: " + supportedStyleGuides());
84+
}
85+
}
86+
87+
private String supportedStyleGuides() {
88+
return EslintFormatterStep.PopularStyleGuide.getPopularStyleGuideNames(this::isValidStyleGuide);
89+
}
90+
91+
protected boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) {
92+
return styleGuide.name().startsWith("JS_");
93+
}
94+
95+
protected Map<String, String> createDefaultDependencies() {
96+
return eslintVersion == null ? EslintFormatterStep.defaultDevDependencies() : EslintFormatterStep.defaultDevDependenciesWithEslint(eslintVersion);
97+
}
98+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016-2022 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.maven.javascript;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <javascript>...</javascript>} configuration element.
25+
* <p>
26+
* It defines a formatter for typescript source files.
27+
*/
28+
public class Javascript extends FormatterFactory {
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return Collections.emptySet();
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return null;
37+
}
38+
39+
public void addEslint(EslintJs eslint) {
40+
addStepFactory(eslint);
41+
}
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2022 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.maven.typescript;
17+
18+
import java.util.Map;
19+
20+
import org.apache.maven.plugins.annotations.Parameter;
21+
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.javascript.EslintJs;
24+
import com.diffplug.spotless.npm.EslintConfig;
25+
import com.diffplug.spotless.npm.EslintFormatterStep;
26+
import com.diffplug.spotless.npm.EslintTypescriptConfig;
27+
28+
public class EslintTs extends EslintJs {
29+
30+
@Parameter
31+
private String tsconfigFile;
32+
33+
@Override
34+
protected EslintConfig eslintConfig(FormatterStepConfig stepConfig) {
35+
EslintConfig jsConfig = super.eslintConfig(stepConfig);
36+
return new EslintTypescriptConfig(jsConfig.getEslintConfigPath(), jsConfig.getEslintConfigJs(), tsconfigFile != null ? stepConfig.getFileLocator().locateFile(tsconfigFile) : null);
37+
}
38+
39+
@Override
40+
protected boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) {
41+
return styleGuide.name().startsWith("TS_");
42+
}
43+
44+
@Override
45+
protected Map<String, String> createDefaultDependencies() {
46+
return this.eslintVersion == null ? EslintFormatterStep.defaultDevDependenciesForTypescript() : EslintFormatterStep.defaultDevDependenciesTypescriptWithEslint(this.eslintVersion);
47+
}
48+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2022 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.
@@ -23,7 +23,7 @@
2323
/**
2424
* A {@link FormatterFactory} implementation that corresponds to {@code <typescript>...</typescript>} configuration element.
2525
* <p>
26-
* It defines a formatter for typescript source files.
26+
* It defines formatters for typescript source files.
2727
*/
2828
public class Typescript extends FormatterFactory {
2929
@Override
@@ -39,4 +39,8 @@ public String licenseHeaderDelimiter() {
3939
public void addTsfmt(Tsfmt tsfmt) {
4040
addStepFactory(tsfmt);
4141
}
42+
43+
public void addEslint(EslintTs eslint) {
44+
addStepFactory(eslint);
45+
}
4246
}

0 commit comments

Comments
 (0)