Skip to content

Commit 7d2e5ad

Browse files
committed
eslint: adding maven javascript tests
1 parent dd5ae78 commit 7d2e5ad

File tree

6 files changed

+216
-68
lines changed

6 files changed

+216
-68
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private FileLocator getFileLocator() {
335335
}
336336

337337
private List<FormatterFactory> getFormatterFactories() {
338-
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, pom, sql, python, markdown))
338+
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, javascript, antlr4, pom, sql, python, markdown))
339339
.filter(Objects::nonNull)
340340
.collect(toList());
341341
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 abstract class AbstractEslint extends AbstractNpmFormatterStepFactory {
32+
33+
@Parameter
34+
protected String configFile;
35+
36+
@Parameter
37+
protected String configJs;
38+
39+
@Parameter
40+
protected String styleGuide;
41+
42+
@Parameter
43+
protected String eslintVersion;
44+
45+
@Parameter
46+
protected 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 abstract EslintConfig eslintConfig(FormatterStepConfig stepConfig);
67+
68+
private void addStyleGuideDevDependencies(Map<String, String> devDependencies) {
69+
if (this.styleGuide != null) {
70+
EslintFormatterStep.PopularStyleGuide styleGuide = EslintFormatterStep.PopularStyleGuide.fromNameOrNull(this.styleGuide);
71+
validateStyleGuide(styleGuide);
72+
devDependencies.putAll(styleGuide.devDependencies());
73+
}
74+
}
75+
76+
private void validateStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) {
77+
if (styleGuide == null) {
78+
throw new IllegalArgumentException("StyleGuide '" + this.styleGuide + "' is not supported. Supported style guides: " + supportedStyleGuides());
79+
}
80+
if (!isValidStyleGuide(styleGuide)) {
81+
throw new IllegalArgumentException("StyleGuide must be of correct type but is: " + styleGuide.getPopularStyleGuideName() + ". Use one of the following: " + supportedStyleGuides());
82+
}
83+
}
84+
85+
private String supportedStyleGuides() {
86+
return EslintFormatterStep.PopularStyleGuide.getPopularStyleGuideNames(this::isValidStyleGuide);
87+
}
88+
89+
protected abstract boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide);
90+
91+
protected abstract Map<String, String> createDefaultDependencies();
92+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/EslintJs.java

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,84 +15,23 @@
1515
*/
1616
package com.diffplug.spotless.maven.javascript;
1717

18-
import java.io.File;
1918
import java.util.Map;
20-
import java.util.TreeMap;
2119

22-
import org.apache.maven.plugins.annotations.Parameter;
23-
24-
import com.diffplug.spotless.FormatterStep;
2520
import com.diffplug.spotless.maven.FormatterStepConfig;
26-
import com.diffplug.spotless.maven.npm.AbstractNpmFormatterStepFactory;
2721
import com.diffplug.spotless.npm.EslintConfig;
2822
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-
}
5723

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-
}
24+
public class EslintJs extends AbstractEslint {
6525

6626
protected EslintConfig eslintConfig(FormatterStepConfig stepConfig) {
6727
return new EslintConfig(this.configFile != null ? stepConfig.getFileLocator().locateFile(this.configFile) : null, this.configJs);
6828
}
6929

70-
private void addStyleGuideDevDependencies(Map<String, String> devDependencies) {
71-
if (this.styleGuide != null) {
72-
EslintFormatterStep.PopularStyleGuide styleGuide = EslintFormatterStep.PopularStyleGuide.fromNameOrNull(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.getPopularStyleGuideName() + ". Use one of the following: " + supportedStyleGuides());
84-
}
85-
}
86-
87-
private String supportedStyleGuides() {
88-
return EslintFormatterStep.PopularStyleGuide.getPopularStyleGuideNames(this::isValidStyleGuide);
89-
}
90-
9130
protected boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) {
9231
return styleGuide.name().startsWith("JS_");
9332
}
9433

9534
protected Map<String, String> createDefaultDependencies() {
96-
return eslintVersion == null ? EslintFormatterStep.defaultDevDependencies() : EslintFormatterStep.defaultDevDependenciesWithEslint(eslintVersion);
35+
return this.eslintVersion == null ? EslintFormatterStep.defaultDevDependencies() : EslintFormatterStep.defaultDevDependenciesWithEslint(this.eslintVersion);
9736
}
9837
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
import org.apache.maven.plugins.annotations.Parameter;
2121

2222
import com.diffplug.spotless.maven.FormatterStepConfig;
23-
import com.diffplug.spotless.maven.javascript.EslintJs;
23+
import com.diffplug.spotless.maven.javascript.AbstractEslint;
2424
import com.diffplug.spotless.npm.EslintConfig;
2525
import com.diffplug.spotless.npm.EslintFormatterStep;
2626
import com.diffplug.spotless.npm.EslintTypescriptConfig;
2727

28-
public class EslintTs extends EslintJs {
28+
public class EslintTs extends AbstractEslint {
2929

3030
@Parameter
3131
private String tsconfigFile;
3232

3333
@Override
3434
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);
35+
return new EslintTypescriptConfig(
36+
configFile != null ? stepConfig.getFileLocator().locateFile(configFile) : null,
37+
configJs,
38+
tsconfigFile != null ? stepConfig.getFileLocator().locateFile(tsconfigFile) : null);
3739
}
3840

3941
@Override

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ protected void writePomWithCppSteps(String... steps) throws IOException {
136136
writePom(groupWithSteps("cpp", steps));
137137
}
138138

139+
protected void writePomWithJavascriptSteps(String includes, String... steps) throws IOException {
140+
writePom(groupWithSteps("javascript", including(includes), steps));
141+
}
142+
139143
protected void writePomWithTypescriptSteps(String includes, String... steps) throws IOException {
140144
writePom(groupWithSteps("typescript", including(includes), steps));
141145
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 org.junit.jupiter.api.Nested;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.ValueSource;
22+
23+
import com.diffplug.spotless.ResourceHarness;
24+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
25+
import com.diffplug.spotless.maven.MavenRunner.Result;
26+
import com.diffplug.spotless.tag.NpmTest;
27+
28+
@NpmTest
29+
class JavascriptFormatStepTest extends MavenIntegrationHarness {
30+
31+
private static final String TEST_FILE_PATH = "src/main/javascript/test.js";
32+
33+
@NpmTest
34+
@Nested
35+
class EslintCustomRulesTest extends MavenIntegrationHarness {
36+
37+
@Test
38+
void eslintConfigFile() throws Exception {
39+
writePomWithJavascriptSteps(
40+
TEST_FILE_PATH,
41+
"<eslint>",
42+
" <configFile>.eslintrc.js</configFile>",
43+
"</eslint>");
44+
setFile(".eslintrc.js").toResource("npm/eslint/javascript/custom_rules/.eslintrc.js");
45+
setFile(TEST_FILE_PATH).toResource("npm/eslint/javascript/custom_rules/javascript-es6.dirty");
46+
47+
Result result = mavenRunner().withArguments("spotless:apply").runNoError();
48+
System.out.println(result.output());
49+
assertFile(TEST_FILE_PATH).sameAsResource("npm/eslint/javascript/custom_rules/javascript-es6.clean");
50+
}
51+
52+
@Test
53+
void eslintConfigJs() throws Exception {
54+
final String configJs = ResourceHarness.getTestResource("npm/eslint/javascript/custom_rules/.eslintrc.js")
55+
.replace("module.exports = ", "");
56+
writePomWithJavascriptSteps(
57+
TEST_FILE_PATH,
58+
"<eslint>",
59+
" <configJs>" + configJs + "</configJs>",
60+
"</eslint>");
61+
setFile(TEST_FILE_PATH).toResource("npm/eslint/javascript/custom_rules/javascript-es6.dirty");
62+
63+
mavenRunner().withArguments("spotless:apply").runNoError();
64+
assertFile(TEST_FILE_PATH).sameAsResource("npm/eslint/javascript/custom_rules/javascript-es6.clean");
65+
}
66+
67+
}
68+
69+
@NpmTest
70+
@Nested
71+
class EslintStyleguidesTest extends MavenIntegrationHarness {
72+
73+
@ParameterizedTest(name = "{index}: eslint js formatting with configFile using styleguide {0}")
74+
@ValueSource(strings = {"airbnb", "google", "standard", "xo"})
75+
void eslintJsStyleguideUsingConfigFile(String styleGuide) throws Exception {
76+
final String styleGuidePath = "npm/eslint/javascript/styleguide/" + styleGuide;
77+
78+
writePomWithJavascriptSteps(
79+
TEST_FILE_PATH,
80+
"<eslint>",
81+
" <configFile>.eslintrc.js</configFile>",
82+
" <styleGuide>" + styleGuide + "</styleGuide>",
83+
"</eslint>");
84+
setFile(".eslintrc.js").toResource(styleGuidePath + "/.eslintrc.js");
85+
setFile(TEST_FILE_PATH).toResource(styleGuidePath + "/javascript-es6.dirty");
86+
87+
mavenRunner().withArguments("spotless:apply").runNoError();
88+
assertFile(TEST_FILE_PATH).sameAsResource(styleGuidePath + "/javascript-es6.clean");
89+
}
90+
91+
@ParameterizedTest(name = "{index}: eslint js formatting with inline config using styleguide {0}")
92+
@ValueSource(strings = {"airbnb", "google", "standard", "xo"})
93+
void eslintJsStyleguideUsingInlineConfig(String styleGuide) throws Exception {
94+
final String styleGuidePath = "npm/eslint/javascript/styleguide/" + styleGuide;
95+
96+
final String escapedInlineConfig = ResourceHarness.getTestResource(styleGuidePath + "/.eslintrc.js")
97+
.replace("<", "&lt;")
98+
.replace(">", "&gt;");
99+
writePomWithJavascriptSteps(
100+
TEST_FILE_PATH,
101+
"<eslint>",
102+
" <configJs>" + escapedInlineConfig + "</configJs>",
103+
" <styleGuide>" + styleGuide + "</styleGuide>",
104+
"</eslint>");
105+
setFile(TEST_FILE_PATH).toResource(styleGuidePath + "/javascript-es6.dirty");
106+
107+
mavenRunner().withArguments("spotless:apply").runNoError();
108+
assertFile(TEST_FILE_PATH).sameAsResource(styleGuidePath + "/javascript-es6.clean");
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)