Skip to content

Commit

Permalink
eslint: remove direct api support for styleguides
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Jan 10, 2023
1 parent 9a0932e commit 0cacf46
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, String> devDependencies() {
Map<String, String> 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<String, String> devDependencies() {
Map<String, String> 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<String, String> devDependencies() {
Map<String, String> 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<String, String> devDependencies() {
Map<String, String> dependencies = new LinkedHashMap<>();
dependencies.put("eslint-config-google", "^0.14.0");
return dependencies;
}
},
JS_STANDARD("standard") {
@Override
public @Nonnull Map<String, String> devDependencies() {
Map<String, String> 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<String, String> devDependencies() {
Map<String, String> 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<String, String> 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<PopularStyleGuide> 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<String, String> defaultDevDependenciesForTypescript() {
return defaultDevDependenciesTypescriptWithEslint(DEFAULT_ESLINT_VERSION);
Expand Down
4 changes: 0 additions & 4 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('''
Expand Down Expand Up @@ -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('''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -73,7 +72,7 @@ public EslintBaseConfig(Project project, Consumer<FormatterStep> replaceStep, Ma
}

@SuppressWarnings("unchecked")
public T devDependencies(Map<String, String> devDependencies) {
protected T devDependencies(Map<String, String> devDependencies) {
this.devDependencies.putAll(devDependencies);
replaceStep();
return (T) this;
Expand All @@ -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<JavascriptEslintConfig> {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -189,7 +189,7 @@ public TypescriptEslintConfig eslint(Map<String, String> devDependencies) {
return eslint;
}

public class TypescriptEslintConfig extends JavascriptExtension.EslintBaseConfig<TypescriptEslintConfig> {
public class TypescriptEslintConfig extends EslintBaseConfig<TypescriptEslintConfig> {

@Nullable
Object typescriptConfigFilePath = null;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -43,36 +49,17 @@ 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");
gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
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'",
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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");
Expand All @@ -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");
}
}
Loading

0 comments on commit 0cacf46

Please sign in to comment.