From 0cacf46ecd50767321b4bc979b412c167d9c68d1 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Tue, 10 Jan 2023 08:35:14 +0100 Subject: [PATCH] eslint: remove direct api support for styleguides --- .../spotless/npm/EslintFormatterStep.java | 93 +----------- plugin-gradle/README.md | 4 - .../gradle/spotless/JavascriptExtension.java | 25 +--- .../gradle/spotless/TypescriptExtension.java | 15 +- .../spotless/JavascriptExtensionTest.java | 41 ++---- .../spotless/TypescriptExtensionTest.java | 32 ++--- plugin-maven/README.md | 4 - .../maven/javascript/AbstractEslint.java | 28 ---- .../spotless/maven/javascript/EslintJs.java | 4 - .../spotless/maven/typescript/EslintTs.java | 5 - .../javascript/JavascriptFormatStepTest.java | 10 +- .../typescript/TypescriptFormatStepTest.java | 10 +- .../spotless/npm/EslintStyleGuide.java | 135 ++++++++++++++++++ 13 files changed, 178 insertions(+), 228 deletions(-) create mode 100644 testlib/src/main/java/com/diffplug/spotless/npm/EslintStyleGuide.java diff --git a/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java index ac3ee05345..81c5b6ce78 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java @@ -20,14 +20,12 @@ import java.io.File; import java.io.IOException; import java.io.Serializable; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; import java.util.TreeMap; -import java.util.function.Predicate; import javax.annotation.Nonnull; @@ -47,96 +45,7 @@ public class EslintFormatterStep { public static final String NAME = "eslint-format"; - public static final String DEFAULT_ESLINT_VERSION = "^8.30.0"; - - public enum PopularStyleGuide { - TS_STANDARD_WITH_TYPESCRIPT("standard-with-typescript") { - @Override - public @Nonnull Map devDependencies() { - Map dependencies = new LinkedHashMap<>(); - dependencies.put("eslint-config-standard-with-typescript", "^24.0.0"); - dependencies.put("eslint-plugin-import", "^2.26.0"); - dependencies.put("eslint-plugin-n", "^15.6.0"); - dependencies.put("eslint-plugin-promise", "^6.1.1"); - return dependencies; - } - }, - TS_XO_TYPESCRIPT("xo-typescript") { - @Override - public @Nonnull Map devDependencies() { - Map dependencies = new LinkedHashMap<>(); - dependencies.put("eslint-config-xo", "^0.43.1"); - dependencies.put("eslint-config-xo-typescript", "^0.55.1"); - return dependencies; - } - }, - JS_AIRBNB("airbnb") { - @Override - public @Nonnull Map devDependencies() { - Map dependencies = new LinkedHashMap<>(); - dependencies.put("eslint-config-airbnb-base", "^15.0.0"); - dependencies.put("eslint-plugin-import", "^2.26.0"); - return dependencies; - } - }, - JS_GOOGLE("google") { - @Override - public @Nonnull Map devDependencies() { - Map dependencies = new LinkedHashMap<>(); - dependencies.put("eslint-config-google", "^0.14.0"); - return dependencies; - } - }, - JS_STANDARD("standard") { - @Override - public @Nonnull Map devDependencies() { - Map dependencies = new LinkedHashMap<>(); - dependencies.put("eslint-config-standard", "^17.0.0"); - dependencies.put("eslint-plugin-import", "^2.26.0"); - dependencies.put("eslint-plugin-n", "^15.6.0"); - dependencies.put("eslint-plugin-promise", "^6.1.1"); - return dependencies; - } - }, - JS_XO("xo") { - @Override - public @Nonnull Map devDependencies() { - Map dependencies = new LinkedHashMap<>(); - dependencies.put("eslint-config-xo", "^0.43.1"); - return dependencies; - } - }; - - private final String popularStyleGuideName; - - PopularStyleGuide(String popularStyleGuideName) { - this.popularStyleGuideName = popularStyleGuideName; - } - - public String getPopularStyleGuideName() { - return popularStyleGuideName; - } - - public abstract @Nonnull Map devDependencies(); - - public static PopularStyleGuide fromNameOrNull(String popularStyleGuideName) { - for (PopularStyleGuide popularStyleGuide : PopularStyleGuide.values()) { - if (popularStyleGuide.popularStyleGuideName.equals(popularStyleGuideName)) { - return popularStyleGuide; - } - } - return null; - } - - public static String getPopularStyleGuideNames(Predicate filter) { - // collect matching style guide names using stream - return Arrays.stream(PopularStyleGuide.values()) - .filter(filter) - .map(PopularStyleGuide::getPopularStyleGuideName) - .sorted() - .collect(java.util.stream.Collectors.joining(", ")); - } - } + public static final String DEFAULT_ESLINT_VERSION = "^8.31.0"; public static Map defaultDevDependenciesForTypescript() { return defaultDevDependenciesTypescriptWithEslint(DEFAULT_ESLINT_VERSION); diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 1ed2b942b5..8a06351bed 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -634,8 +634,6 @@ spotless { eslint(['my-eslint-fork': '1.2.3', 'my-eslint-plugin': '1.2.1']) // can specify exactly which npm packages to use eslint() - // optional: use a popular eslint styleguide for typescript - .styleGuide('standard-with-typescript') // or 'xo-typescript' // configuration is mandatory. Provide inline config or a config file. // a) inline-configuration .configJs(''' @@ -703,8 +701,6 @@ spotless { eslint(['my-eslint-fork': '1.2.3', 'my-eslint-plugin': '1.2.1']) // can specify exactly which npm packages to use eslint() - // optional: use a popular eslint styleguide for javascript - .styleGuide('standard') // or 'airbnb', 'google', 'xo' // configuration is mandatory. Provide inline config or a config file. // a) inline-configuration .configJs(''' diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavascriptExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavascriptExtension.java index 862c7566ee..dd476b6a69 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavascriptExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavascriptExtension.java @@ -31,7 +31,6 @@ import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.npm.EslintConfig; import com.diffplug.spotless.npm.EslintFormatterStep; -import com.diffplug.spotless.npm.EslintFormatterStep.PopularStyleGuide; import com.diffplug.spotless.npm.NpmPathResolver; import com.diffplug.spotless.npm.PrettierFormatterStep; @@ -73,7 +72,7 @@ public EslintBaseConfig(Project project, Consumer replaceStep, Ma } @SuppressWarnings("unchecked") - public T devDependencies(Map devDependencies) { + protected T devDependencies(Map devDependencies) { this.devDependencies.putAll(devDependencies); replaceStep(); return (T) this; @@ -92,17 +91,6 @@ public T configFile(Object configFilePath) { replaceStep(); return (T) this; } - - @SuppressWarnings("unchecked") - public T styleGuide(String styleGuide) { - PopularStyleGuide popularStyleGuide = PopularStyleGuide.fromNameOrNull(styleGuide); - - verifyStyleGuideIsSupported(styleGuide, popularStyleGuide); - assert popularStyleGuide != null; - return devDependencies(popularStyleGuide.devDependencies()); - } - - protected abstract void verifyStyleGuideIsSupported(String styleGuideName, PopularStyleGuide popularStyleGuide); } public class JavascriptEslintConfig extends EslintBaseConfig { @@ -123,17 +111,6 @@ public FormatterStep createStep() { eslintConfig()); } - @Override - protected void verifyStyleGuideIsSupported(String styleGuideName, PopularStyleGuide popularStyleGuide) { - if (!isJsStyleGuide(popularStyleGuide)) { - throw new IllegalArgumentException("Unknown style guide: " + styleGuideName + ". Known javascript style guides: " + PopularStyleGuide.getPopularStyleGuideNames(this::isJsStyleGuide)); - } - } - - private boolean isJsStyleGuide(PopularStyleGuide popularStyleGuide) { - return popularStyleGuide != null && popularStyleGuide.name().startsWith("JS_"); - } - protected EslintConfig eslintConfig() { return new EslintConfig(configFilePath != null ? getProject().file(configFilePath) : null, configJs); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TypescriptExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TypescriptExtension.java index 5de1bb1ac3..0824caa769 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TypescriptExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TypescriptExtension.java @@ -27,10 +27,10 @@ import org.gradle.api.Project; +import com.diffplug.gradle.spotless.JavascriptExtension.EslintBaseConfig; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.npm.EslintConfig; import com.diffplug.spotless.npm.EslintFormatterStep; -import com.diffplug.spotless.npm.EslintFormatterStep.PopularStyleGuide; import com.diffplug.spotless.npm.EslintTypescriptConfig; import com.diffplug.spotless.npm.NpmPathResolver; import com.diffplug.spotless.npm.PrettierFormatterStep; @@ -189,7 +189,7 @@ public TypescriptEslintConfig eslint(Map devDependencies) { return eslint; } - public class TypescriptEslintConfig extends JavascriptExtension.EslintBaseConfig { + public class TypescriptEslintConfig extends EslintBaseConfig { @Nullable Object typescriptConfigFilePath = null; @@ -204,17 +204,6 @@ public TypescriptEslintConfig tsconfigFile(Object path) { return this; } - @Override - protected void verifyStyleGuideIsSupported(String styleGuideName, PopularStyleGuide popularStyleGuide) { - if (!isTsStyleGuide(popularStyleGuide)) { - throw new IllegalArgumentException("Unknown style guide: " + styleGuideName + ". Known typescript style guides: " + PopularStyleGuide.getPopularStyleGuideNames(this::isTsStyleGuide)); - } - } - - private boolean isTsStyleGuide(PopularStyleGuide popularStyleGuide) { - return popularStyleGuide != null && popularStyleGuide.name().startsWith("TS_"); - } - public FormatterStep createStep() { final Project project = getProject(); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavascriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavascriptExtensionTest.java index 331e17f28e..26354b93be 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavascriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavascriptExtensionTest.java @@ -24,11 +24,17 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import com.diffplug.spotless.npm.EslintFormatterStep; +import com.diffplug.spotless.npm.EslintStyleGuide; import com.diffplug.spotless.tag.NpmTest; @NpmTest class JavascriptExtensionTest extends GradleIntegrationHarness { + private static String styleGuideMapString(String styleGuideName) { + return EslintStyleGuide.fromNameOrNull(styleGuideName).asGradleMapStringMergedWith(EslintFormatterStep.defaultDevDependencies()); + } + @NpmTest @Nested class EslintGeneralJavascriptTests extends GradleIntegrationHarness { @@ -43,7 +49,7 @@ void supportsEslintFormattingForJavascript() throws IOException { "spotless {", " javascript {", " target 'test.js'", - " eslint().configFile('.eslintrc.js').styleGuide('standard')", + " eslint(" + styleGuideMapString("standard") + ").configFile('.eslintrc.js')", " }", "}"); setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); @@ -51,28 +57,9 @@ void supportsEslintFormattingForJavascript() throws IOException { assertFile("test.js").sameAsResource("npm/eslint/javascript/styleguide/standard/javascript-es6.clean"); } - @Test - void eslintDoesNotAllowToUseTsStyleGuideForJavascript() throws IOException { - setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); - setFile("build.gradle").toLines( - "plugins {", - " id 'com.diffplug.spotless'", - "}", - "repositories { mavenCentral() }", - "spotless {", - " javascript {", - " target 'test.js'", - " eslint().configFile('.eslintrc.js').styleGuide('xo-typescript')", - " }", - "}"); - setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); - final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail(); - Assertions.assertThat(spotlessApply.getOutput()).contains("Unknown style guide: xo-typescript"); - } - @Test void eslintAllowsToSpecifyEslintVersionForJavascript() throws IOException { - setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); + setFile(".eslintrc.js").toResource("npm/eslint/javascript/custom_rules/.eslintrc.js"); setFile("build.gradle").toLines( "plugins {", " id 'com.diffplug.spotless'", @@ -81,12 +68,12 @@ void eslintAllowsToSpecifyEslintVersionForJavascript() throws IOException { "spotless {", " javascript {", " target 'test.js'", - " eslint('8.28.0').configFile('.eslintrc.js').styleGuide('standard')", + " eslint('8.28.0').configFile('.eslintrc.js')", " }", "}"); - setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); + setFile("test.js").toResource("npm/eslint/javascript/custom_rules/javascript-es6.dirty"); gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); - assertFile("test.js").sameAsResource("npm/eslint/javascript/styleguide/standard/javascript-es6.clean"); + assertFile("test.js").sameAsResource("npm/eslint/javascript/custom_rules/javascript-es6.clean"); } @Test @@ -115,7 +102,7 @@ void esllintAllowsToSpecifyInlineConfig() throws IOException { "spotless {", " javascript {", " target 'test.js'", - " eslint().configJs('''" + eslintConfigJs + "''').styleGuide('standard')", + " eslint(" + styleGuideMapString("standard") + ").configJs('''" + eslintConfigJs + "''')", " }", "}"); setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); @@ -134,7 +121,7 @@ void eslintRequiresAnExplicitEslintConfig() throws IOException { "spotless {", " javascript {", " target 'test.js'", - " eslint().styleGuide('standard')", + " eslint(" + styleGuideMapString("standard") + ")", " }", "}"); setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); @@ -187,7 +174,7 @@ void formattingUsingStyleguide(String styleguide) throws Exception { "spotless {", " javascript {", " target 'test.js'", - " eslint().configFile('.eslintrc.js').styleGuide('" + styleguide + "')", + " eslint(" + styleGuideMapString(styleguide) + ").configFile('.eslintrc.js')", " }", "}"); setFile("test.js").toResource(styleguidePath + "javascript-es6.dirty"); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java index 598ff7dc14..9b4ec8372e 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/TypescriptExtensionTest.java @@ -17,14 +17,19 @@ import java.io.IOException; -import org.assertj.core.api.Assertions; -import org.gradle.testkit.runner.BuildResult; import org.junit.jupiter.api.Test; +import com.diffplug.spotless.npm.EslintFormatterStep; +import com.diffplug.spotless.npm.EslintStyleGuide; import com.diffplug.spotless.tag.NpmTest; @NpmTest class TypescriptExtensionTest extends GradleIntegrationHarness { + + private static String styleGuideMapString(String styleGuideName) { + return EslintStyleGuide.fromNameOrNull(styleGuideName).asGradleMapStringMergedWith(EslintFormatterStep.defaultDevDependencies()); + } + @Test void allowToSpecifyFormatterVersion() throws IOException { setFile("build.gradle").toLines( @@ -175,7 +180,7 @@ void useEslintXoStandardRules() throws IOException { "spotless {", " typescript {", " target 'test.ts'", - " eslint().styleGuide('xo-typescript').configFile('.eslintrc.js')", + " eslint(" + styleGuideMapString("xo-typescript") + ").configFile('.eslintrc.js')", " }", "}"); setFile("test.ts").toResource("npm/eslint/typescript/styleguide/xo/typescript.dirty"); @@ -195,30 +200,11 @@ void useEslintStandardWithTypescriptRules() throws IOException { "spotless {", " typescript {", " target 'test.ts'", - " eslint().styleGuide('standard-with-typescript').configFile('.eslintrc.js')", + " eslint(" + styleGuideMapString("standard-with-typescript") + ").configFile('.eslintrc.js')", " }", "}"); setFile("test.ts").toResource("npm/eslint/typescript/styleguide/standard_with_typescript/typescript.dirty"); gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); assertFile("test.ts").sameAsResource("npm/eslint/typescript/styleguide/standard_with_typescript/typescript.clean"); } - - @Test - void useEslintForTypescriptDoesNotAllowUsingJsStyleguide() throws IOException { - setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/airbnb/.eslintrc.js"); - setFile("build.gradle").toLines( - "plugins {", - " id 'com.diffplug.spotless'", - "}", - "repositories { mavenCentral() }", - "spotless {", - " typescript {", - " target 'test.ts'", - " eslint().styleGuide('airbnb').configFile('.eslintrc.js')", - " }", - "}"); - setFile("test.js").toResource("npm/eslint/javascript/styleguide/airbnb/javascript-es6.dirty"); - BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail(); - Assertions.assertThat(spotlessApply.getOutput()).contains("Unknown style guide: airbnb"); - } } diff --git a/plugin-maven/README.md b/plugin-maven/README.md index dcf9df45d9..2a2aa31010 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -745,8 +745,6 @@ styleguides and the requirement for a tsconfigFile. 0.14.2 - - standard-with-typescript ${project.basedir}/.eslintrc.js @@ -826,8 +824,6 @@ styleguides and no requirement for a tsconfig (of course). 0.14.2 - - standard ${project.basedir}/.eslintrc.js diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/AbstractEslint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/AbstractEslint.java index 1829f91b4e..b06d3079e7 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/AbstractEslint.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/AbstractEslint.java @@ -39,9 +39,6 @@ public abstract class AbstractEslint extends AbstractNpmFormatterStepFactory { @Parameter protected String configJs; - @Parameter - protected String styleGuide; - @Parameter protected String eslintVersion; @@ -68,8 +65,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { devDependencies.putAll(defaultDependencies); } - addStyleGuideDevDependencies(devDependencies); - File buildDir = buildDir(stepConfig); File baseDir = baseDir(stepConfig); NpmPathResolver npmPathResolver = npmPathResolver(stepConfig); @@ -82,28 +77,5 @@ private static IllegalArgumentException onlyOneConfig() { protected abstract EslintConfig eslintConfig(FormatterStepConfig stepConfig); - private void addStyleGuideDevDependencies(Map devDependencies) { - if (this.styleGuide != null) { - EslintFormatterStep.PopularStyleGuide styleGuide = EslintFormatterStep.PopularStyleGuide.fromNameOrNull(this.styleGuide); - validateStyleGuide(styleGuide); - devDependencies.putAll(styleGuide.devDependencies()); - } - } - - private void validateStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) { - if (styleGuide == null) { - throw new IllegalArgumentException("StyleGuide '" + this.styleGuide + "' is not supported. Supported style guides: " + supportedStyleGuides()); - } - if (!isValidStyleGuide(styleGuide)) { - throw new IllegalArgumentException("StyleGuide must be of correct type but is: " + styleGuide.getPopularStyleGuideName() + ". Use one of the following: " + supportedStyleGuides()); - } - } - - private String supportedStyleGuides() { - return EslintFormatterStep.PopularStyleGuide.getPopularStyleGuideNames(this::isValidStyleGuide); - } - - protected abstract boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide); - protected abstract Map createDefaultDependencies(); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/EslintJs.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/EslintJs.java index f9954ab0c8..483d38ae1e 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/EslintJs.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/EslintJs.java @@ -27,10 +27,6 @@ protected EslintConfig eslintConfig(FormatterStepConfig stepConfig) { return new EslintConfig(this.configFile != null ? stepConfig.getFileLocator().locateFile(this.configFile) : null, this.configJs); } - protected boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) { - return styleGuide.name().startsWith("JS_"); - } - protected Map createDefaultDependencies() { return this.eslintVersion == null ? EslintFormatterStep.defaultDevDependencies() : EslintFormatterStep.defaultDevDependenciesWithEslint(this.eslintVersion); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/EslintTs.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/EslintTs.java index 72f3735d97..dc43185dcd 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/EslintTs.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/EslintTs.java @@ -38,11 +38,6 @@ protected EslintConfig eslintConfig(FormatterStepConfig stepConfig) { tsconfigFile != null ? stepConfig.getFileLocator().locateFile(tsconfigFile) : null); } - @Override - protected boolean isValidStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide) { - return styleGuide.name().startsWith("TS_"); - } - @Override protected Map createDefaultDependencies() { return this.eslintVersion == null ? EslintFormatterStep.defaultDevDependenciesForTypescript() : EslintFormatterStep.defaultDevDependenciesTypescriptWithEslint(this.eslintVersion); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/javascript/JavascriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/javascript/JavascriptFormatStepTest.java index 7ebc592add..5c8a5f04b3 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/javascript/JavascriptFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/javascript/JavascriptFormatStepTest.java @@ -23,6 +23,8 @@ import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.maven.MavenIntegrationHarness; import com.diffplug.spotless.maven.MavenRunner.Result; +import com.diffplug.spotless.npm.EslintFormatterStep; +import com.diffplug.spotless.npm.EslintStyleGuide; import com.diffplug.spotless.tag.NpmTest; @NpmTest @@ -30,6 +32,10 @@ class JavascriptFormatStepTest extends MavenIntegrationHarness { private static final String TEST_FILE_PATH = "src/main/javascript/test.js"; + private static String styleGuideDevDependenciesString(String styleGuideName) { + return EslintStyleGuide.fromNameOrNull(styleGuideName).asMavenXmlStringMergedWith(EslintFormatterStep.defaultDevDependencies()); + } + @NpmTest @Nested class EslintCustomRulesTest extends MavenIntegrationHarness { @@ -79,7 +85,7 @@ void eslintJsStyleguideUsingConfigFile(String styleGuide) throws Exception { TEST_FILE_PATH, "", " .eslintrc.js", - " " + styleGuide + "", + " " + styleGuideDevDependenciesString(styleGuide), ""); setFile(".eslintrc.js").toResource(styleGuidePath + "/.eslintrc.js"); setFile(TEST_FILE_PATH).toResource(styleGuidePath + "/javascript-es6.dirty"); @@ -100,7 +106,7 @@ void eslintJsStyleguideUsingInlineConfig(String styleGuide) throws Exception { TEST_FILE_PATH, "", " " + escapedInlineConfig + "", - " " + styleGuide + "", + " " + styleGuideDevDependenciesString(styleGuide), ""); setFile(TEST_FILE_PATH).toResource(styleGuidePath + "/javascript-es6.dirty"); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java index ac925cc9b6..95097fe467 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -24,6 +24,8 @@ import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.maven.MavenIntegrationHarness; import com.diffplug.spotless.maven.MavenRunner.Result; +import com.diffplug.spotless.npm.EslintFormatterStep; +import com.diffplug.spotless.npm.EslintStyleGuide; import com.diffplug.spotless.tag.NpmTest; @NpmTest @@ -31,6 +33,10 @@ class TypescriptFormatStepTest extends MavenIntegrationHarness { private static final String TEST_FILE_PATH = "src/main/typescript/test.ts"; + private static String styleGuideDevDependenciesString(String styleGuideName) { + return EslintStyleGuide.fromNameOrNull(styleGuideName).asMavenXmlStringMergedWith(EslintFormatterStep.defaultDevDependencies()); + } + private void runTsfmt(String kind) throws IOException, InterruptedException { String path = prepareRunTsfmt(kind); mavenRunner().withArguments("spotless:apply").runNoError(); @@ -192,7 +198,7 @@ void eslintStyleguideStandardWithTypescript() throws Exception { TEST_FILE_PATH, "", " .eslintrc.js", - " standard-with-typescript", + " " + styleGuideDevDependenciesString("standard-with-typescript"), " ${basedir}/tsconfig.json", ""); setFile(".eslintrc.js").toResource("npm/eslint/typescript/styleguide/standard_with_typescript/.eslintrc.js"); @@ -209,7 +215,7 @@ void eslintStyleguideXo() throws Exception { TEST_FILE_PATH, "", " .eslintrc.js", - " xo-typescript", + " " + styleGuideDevDependenciesString("xo-typescript"), " ${basedir}/tsconfig.json", ""); setFile(".eslintrc.js").toResource("npm/eslint/typescript/styleguide/xo/.eslintrc.js"); diff --git a/testlib/src/main/java/com/diffplug/spotless/npm/EslintStyleGuide.java b/testlib/src/main/java/com/diffplug/spotless/npm/EslintStyleGuide.java new file mode 100644 index 0000000000..dc997eb140 --- /dev/null +++ b/testlib/src/main/java/com/diffplug/spotless/npm/EslintStyleGuide.java @@ -0,0 +1,135 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.npm; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import javax.annotation.Nonnull; + +/** + * A helper class to create dev dependencies for eslint when using one of the popular styleguides in testing. + */ +public enum EslintStyleGuide { + TS_STANDARD_WITH_TYPESCRIPT("standard-with-typescript") { + @Override + public @Nonnull Map devDependencies() { + Map dependencies = new LinkedHashMap<>(); + dependencies.put("eslint-config-standard-with-typescript", "^24.0.0"); + dependencies.put("eslint-plugin-import", "^2.26.0"); + dependencies.put("eslint-plugin-n", "^15.6.0"); + dependencies.put("eslint-plugin-promise", "^6.1.1"); + return dependencies; + } + }, + TS_XO_TYPESCRIPT("xo-typescript") { + @Override + public @Nonnull Map devDependencies() { + Map dependencies = new LinkedHashMap<>(); + dependencies.put("eslint-config-xo", "^0.43.1"); + dependencies.put("eslint-config-xo-typescript", "^0.55.1"); + return dependencies; + } + }, + JS_AIRBNB("airbnb") { + @Override + public @Nonnull Map devDependencies() { + Map dependencies = new LinkedHashMap<>(); + dependencies.put("eslint-config-airbnb-base", "^15.0.0"); + dependencies.put("eslint-plugin-import", "^2.26.0"); + return dependencies; + } + }, + JS_GOOGLE("google") { + @Override + public @Nonnull Map devDependencies() { + Map dependencies = new LinkedHashMap<>(); + dependencies.put("eslint-config-google", "^0.14.0"); + return dependencies; + } + }, + JS_STANDARD("standard") { + @Override + public @Nonnull Map devDependencies() { + Map dependencies = new LinkedHashMap<>(); + dependencies.put("eslint-config-standard", "^17.0.0"); + dependencies.put("eslint-plugin-import", "^2.26.0"); + dependencies.put("eslint-plugin-n", "^15.6.0"); + dependencies.put("eslint-plugin-promise", "^6.1.1"); + return dependencies; + } + }, + JS_XO("xo") { + @Override + public @Nonnull Map devDependencies() { + Map dependencies = new LinkedHashMap<>(); + dependencies.put("eslint-config-xo", "^0.43.1"); + return dependencies; + } + }; + + private final String popularStyleGuideName; + + EslintStyleGuide(String popularStyleGuideName) { + this.popularStyleGuideName = popularStyleGuideName; + } + + public String getPopularStyleGuideName() { + return popularStyleGuideName; + } + + public abstract @Nonnull Map devDependencies(); + + public static EslintStyleGuide fromNameOrNull(String popularStyleGuideName) { + for (EslintStyleGuide popularStyleGuide : EslintStyleGuide.values()) { + if (popularStyleGuide.popularStyleGuideName.equals(popularStyleGuideName)) { + return popularStyleGuide; + } + } + return null; + } + + public static String getPopularStyleGuideNames(Predicate filter) { + // collect matching style guide names using stream + return Arrays.stream(EslintStyleGuide.values()) + .filter(filter) + .map(EslintStyleGuide::getPopularStyleGuideName) + .sorted() + .collect(Collectors.joining(", ")); + } + + public Map mergedWith(Map devDependencies) { + Map merged = new LinkedHashMap<>(devDependencies); + merged.putAll(devDependencies()); + return merged; + } + + public String asGradleMapStringMergedWith(Map devDependencies) { + return mergedWith(devDependencies).entrySet().stream() + .map(entry -> "'" + entry.getKey() + "': '" + entry.getValue() + "'") + .collect(Collectors.joining(", ", "[", "]")); + } + + public String asMavenXmlStringMergedWith(Map devDependencies) { + return mergedWith(devDependencies).entrySet().stream() + .map(entry -> "<" + entry.getKey() + ">" + entry.getValue() + "") + .collect(Collectors.joining("", "", "")); + } + +}