-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ESLint as formatter step (#1453)
- Loading branch information
Showing
87 changed files
with
2,900 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
lib/src/main/java/com/diffplug/spotless/npm/BaseNpmRestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2016-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; | ||
|
||
abstract class BaseNpmRestService { | ||
|
||
protected final SimpleRestClient restClient; | ||
|
||
BaseNpmRestService(String baseUrl) { | ||
this.restClient = SimpleRestClient.forBaseUrl(baseUrl); | ||
} | ||
|
||
public String shutdown() { | ||
return restClient.post("/shutdown"); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
lib/src/main/java/com/diffplug/spotless/npm/EslintConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2016-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.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.diffplug.spotless.FileSignature; | ||
import com.diffplug.spotless.ThrowingEx; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
public class EslintConfig implements Serializable { | ||
|
||
private static final long serialVersionUID = -6196834313082791248L; | ||
|
||
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") | ||
@Nullable | ||
private final transient File eslintConfigPath; | ||
|
||
@SuppressWarnings("unused") | ||
private final FileSignature eslintConfigPathSignature; | ||
|
||
private final String eslintConfigJs; | ||
|
||
public EslintConfig(@Nullable File eslintConfigPath, @Nullable String eslintConfigJs) { | ||
try { | ||
this.eslintConfigPath = eslintConfigPath; | ||
this.eslintConfigPathSignature = eslintConfigPath != null ? FileSignature.signAsList(this.eslintConfigPath) : FileSignature.signAsList(); | ||
this.eslintConfigJs = eslintConfigJs; | ||
} catch (IOException e) { | ||
throw ThrowingEx.asRuntime(e); | ||
} | ||
} | ||
|
||
public EslintConfig withEslintConfigPath(@Nullable File eslintConfigPath) { | ||
return new EslintConfig(eslintConfigPath, this.eslintConfigJs); | ||
} | ||
|
||
@Nullable | ||
public File getEslintConfigPath() { | ||
return eslintConfigPath; | ||
} | ||
|
||
@Nullable | ||
public String getEslintConfigJs() { | ||
return eslintConfigJs; | ||
} | ||
|
||
public EslintConfig verify() { | ||
if (eslintConfigPath == null && eslintConfigJs == null) { | ||
throw new IllegalArgumentException("ESLint must be configured using either a configFile or a configJs - but both are null."); | ||
} | ||
return this; | ||
} | ||
} |
182 changes: 182 additions & 0 deletions
182
lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* Copyright 2016-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 static java.util.Objects.requireNonNull; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
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 javax.annotation.Nonnull; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterFunc.Closeable; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.Provisioner; | ||
import com.diffplug.spotless.ThrowingEx; | ||
import com.diffplug.spotless.npm.EslintRestService.FormatOption; | ||
|
||
public class EslintFormatterStep { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EslintFormatterStep.class); | ||
|
||
public static final String NAME = "eslint-format"; | ||
|
||
public static final String DEFAULT_ESLINT_VERSION = "^8.31.0"; | ||
|
||
public static Map<String, String> defaultDevDependenciesForTypescript() { | ||
return defaultDevDependenciesTypescriptWithEslint(DEFAULT_ESLINT_VERSION); | ||
} | ||
|
||
public static Map<String, String> defaultDevDependenciesTypescriptWithEslint(String eslintVersion) { | ||
Map<String, String> dependencies = new LinkedHashMap<>(); | ||
dependencies.put("@typescript-eslint/eslint-plugin", "^5.47.0"); | ||
dependencies.put("@typescript-eslint/parser", "^5.47.0"); | ||
dependencies.put("typescript", "^4.9.4"); | ||
dependencies.put("eslint", Objects.requireNonNull(eslintVersion)); | ||
return dependencies; | ||
} | ||
|
||
public static Map<String, String> defaultDevDependencies() { | ||
return defaultDevDependenciesWithEslint(DEFAULT_ESLINT_VERSION); | ||
} | ||
|
||
public static Map<String, String> defaultDevDependenciesWithEslint(String version) { | ||
return Collections.singletonMap("eslint", version); | ||
} | ||
|
||
public static FormatterStep create(Map<String, String> devDependencies, Provisioner provisioner, File projectDir, File buildDir, NpmPathResolver npmPathResolver, EslintConfig eslintConfig) { | ||
requireNonNull(devDependencies); | ||
requireNonNull(provisioner); | ||
requireNonNull(projectDir); | ||
requireNonNull(buildDir); | ||
return FormatterStep.createLazy(NAME, | ||
() -> new State(NAME, devDependencies, projectDir, buildDir, npmPathResolver, eslintConfig), | ||
State::createFormatterFunc); | ||
} | ||
|
||
private static class State extends NpmFormatterStepStateBase implements Serializable { | ||
|
||
private static final long serialVersionUID = -539537027004745812L; | ||
private final EslintConfig eslintConfig; | ||
|
||
State(String stepName, Map<String, String> devDependencies, File projectDir, File buildDir, NpmPathResolver npmPathResolver, EslintConfig eslintConfig) throws IOException { | ||
super(stepName, | ||
new NpmConfig( | ||
replaceDevDependencies( | ||
NpmResourceHelper.readUtf8StringFromClasspath(EslintFormatterStep.class, "/com/diffplug/spotless/npm/eslint-package.json"), | ||
new TreeMap<>(devDependencies)), | ||
"eslint", | ||
NpmResourceHelper.readUtf8StringFromClasspath(EslintFormatterStep.class, | ||
"/com/diffplug/spotless/npm/common-serve.js", | ||
"/com/diffplug/spotless/npm/eslint-serve.js"), | ||
npmPathResolver.resolveNpmrcContent()), | ||
projectDir, | ||
buildDir, | ||
npmPathResolver.resolveNpmExecutable()); | ||
this.eslintConfig = localCopyFiles(requireNonNull(eslintConfig)); | ||
} | ||
|
||
private EslintConfig localCopyFiles(EslintConfig orig) { | ||
if (orig.getEslintConfigPath() == null) { | ||
return orig.verify(); | ||
} | ||
// If any config files are provided, we need to make sure they are at the same location as the node modules | ||
// as eslint will try to resolve plugin/config names relatively to the config file location and some | ||
// eslint configs contain relative paths to additional config files (such as tsconfig.json e.g.) | ||
FormattedPrinter.SYSOUT.print("Copying config file <%s> to <%s> and using the copy", orig.getEslintConfigPath(), nodeModulesDir); | ||
File configFileCopy = NpmResourceHelper.copyFileToDir(orig.getEslintConfigPath(), nodeModulesDir); | ||
return orig.withEslintConfigPath(configFileCopy).verify(); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public FormatterFunc createFormatterFunc() { | ||
try { | ||
FormattedPrinter.SYSOUT.print("creating formatter function (starting server)"); | ||
ServerProcessInfo eslintRestServer = npmRunServer(); | ||
EslintRestService restService = new EslintRestService(eslintRestServer.getBaseUrl()); | ||
return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(projectDir, nodeModulesDir, eslintConfig, restService)); | ||
} catch (IOException e) { | ||
throw ThrowingEx.asRuntime(e); | ||
} | ||
} | ||
|
||
private void endServer(BaseNpmRestService restService, ServerProcessInfo restServer) throws Exception { | ||
FormattedPrinter.SYSOUT.print("Closing formatting function (ending server)."); | ||
try { | ||
restService.shutdown(); | ||
} catch (Throwable t) { | ||
logger.info("Failed to request shutdown of rest service via api. Trying via process.", t); | ||
} | ||
restServer.close(); | ||
} | ||
|
||
} | ||
|
||
private static class EslintFilePathPassingFormatterFunc implements FormatterFunc.NeedsFile { | ||
private final File projectDir; | ||
private final File nodeModulesDir; | ||
private final EslintConfig eslintConfig; | ||
private final EslintRestService restService; | ||
|
||
public EslintFilePathPassingFormatterFunc(File projectDir, File nodeModulesDir, EslintConfig eslintConfig, EslintRestService restService) { | ||
this.projectDir = requireNonNull(projectDir); | ||
this.nodeModulesDir = requireNonNull(nodeModulesDir); | ||
this.eslintConfig = requireNonNull(eslintConfig); | ||
this.restService = requireNonNull(restService); | ||
} | ||
|
||
@Override | ||
public String applyWithFile(String unix, File file) throws Exception { | ||
FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, Math.min(50, unix.length())) + "[...]' in file '" + file + "'"); | ||
|
||
Map<FormatOption, Object> eslintCallOptions = new HashMap<>(); | ||
setConfigToCallOptions(eslintCallOptions); | ||
setFilePathToCallOptions(eslintCallOptions, file); | ||
return restService.format(unix, eslintCallOptions); | ||
} | ||
|
||
private void setFilePathToCallOptions(Map<FormatOption, Object> eslintCallOptions, File fileToBeFormatted) { | ||
eslintCallOptions.put(FormatOption.FILE_PATH, fileToBeFormatted.getAbsolutePath()); | ||
} | ||
|
||
private void setConfigToCallOptions(Map<FormatOption, Object> eslintCallOptions) { | ||
if (eslintConfig.getEslintConfigPath() != null) { | ||
eslintCallOptions.put(FormatOption.ESLINT_OVERRIDE_CONFIG_FILE, eslintConfig.getEslintConfigPath().getAbsolutePath()); | ||
} | ||
if (eslintConfig.getEslintConfigJs() != null) { | ||
eslintCallOptions.put(FormatOption.ESLINT_OVERRIDE_CONFIG, eslintConfig.getEslintConfigJs()); | ||
} | ||
if (eslintConfig instanceof EslintTypescriptConfig) { | ||
// if we are a ts config, see if we need to use specific paths or use default projectDir | ||
File tsConfigFilePath = ((EslintTypescriptConfig) eslintConfig).getTypescriptConfigPath(); | ||
File tsConfigRootDir = tsConfigFilePath != null ? tsConfigFilePath.getParentFile() : projectDir; | ||
eslintCallOptions.put(FormatOption.TS_CONFIG_ROOT_DIR, nodeModulesDir.getAbsoluteFile().toPath().relativize(tsConfigRootDir.getAbsoluteFile().toPath()).toString()); | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/src/main/java/com/diffplug/spotless/npm/EslintRestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2016-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.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
public class EslintRestService extends BaseNpmRestService { | ||
|
||
EslintRestService(String baseUrl) { | ||
super(baseUrl); | ||
} | ||
|
||
public String format(String fileContent, Map<FormatOption, Object> formatOptions) { | ||
Map<String, Object> jsonProperties = new LinkedHashMap<>(); | ||
jsonProperties.put("file_content", fileContent); | ||
for (Entry<FormatOption, Object> option : formatOptions.entrySet()) { | ||
jsonProperties.put(option.getKey().backendName, option.getValue()); | ||
} | ||
return restClient.postJson("/eslint/format", jsonProperties); | ||
} | ||
|
||
enum FormatOption { | ||
ESLINT_OVERRIDE_CONFIG("eslint_override_config"), ESLINT_OVERRIDE_CONFIG_FILE("eslint_override_config_file"), FILE_PATH("file_path"), TS_CONFIG_ROOT_DIR("ts_config_root_dir"); | ||
|
||
private final String backendName; | ||
|
||
FormatOption(String backendName) { | ||
this.backendName = backendName; | ||
} | ||
} | ||
} |
Oops, something went wrong.