From ed9612b73c16c8f18e17d777c22514532b741a95 Mon Sep 17 00:00:00 2001 From: naciron Date: Thu, 26 Mar 2020 13:49:00 +0000 Subject: [PATCH 01/10] Add Typescript Tsfmt to Maven Plugin --- .../spotless/maven/AbstractSpotlessMojo.java | 6 +- .../spotless/maven/typescript/Tsfmt.java | 98 ++++++++++++++ .../spotless/maven/typescript/Typescript.java | 49 +++++++ .../spotless/maven/MavenIntegrationTest.java | 4 + .../typescript/TypescriptFormatStepTest.java | 125 ++++++++++++++++++ .../tsfmt/TypescriptCodeFormatted.test | 4 + .../tsfmt/TypescriptCodeUnformatted.test | 4 + .../resources/typescript/tsfmt/tsconfig.json | 3 + .../resources/typescript/tsfmt/tsfmt.json | 24 ++++ .../resources/typescript/tsfmt/tslint.json | 11 ++ .../resources/typescript/tsfmt/vscode.json | 15 +++ 11 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java create mode 100644 testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test create mode 100644 testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test create mode 100644 testlib/src/main/resources/typescript/tsfmt/tsconfig.json create mode 100644 testlib/src/main/resources/typescript/tsfmt/tsfmt.json create mode 100644 testlib/src/main/resources/typescript/tsfmt/tslint.json create mode 100644 testlib/src/main/resources/typescript/tsfmt/vscode.json diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index f045348117..e43d1c7850 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -45,6 +45,7 @@ import com.diffplug.spotless.maven.java.Java; import com.diffplug.spotless.maven.kotlin.Kotlin; import com.diffplug.spotless.maven.scala.Scala; +import com.diffplug.spotless.maven.typescript.Typescript; public abstract class AbstractSpotlessMojo extends AbstractMojo { @@ -97,6 +98,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private Cpp cpp; + + @Parameter + private Typescript typescript; /** The CSS extension is discontinued. */ @Parameter @@ -179,7 +183,7 @@ private FileLocator getFileLocator() { } private List getFormatterFactories() { - return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, css, xml)) + return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, typescript, css, xml)) .filter(Objects::nonNull) .collect(toList()); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java new file mode 100644 index 0000000000..c5613eb275 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -0,0 +1,98 @@ +/* + * Copyright 2016 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.maven.typescript; + +import java.io.File; +import java.util.Map; + +import org.apache.maven.plugins.annotations.Parameter; +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; +import com.diffplug.spotless.npm.TsConfigFileType; +import com.diffplug.spotless.npm.TsFmtFormatterStep; +import com.diffplug.spotless.npm.TypedTsFmtConfigFile; + +public class Tsfmt implements FormatterStepFactory { + + @Parameter + private String tslintFile; + + @Parameter + private String tsconfigFile; + + @Parameter + private String vscodeFile; + + @Parameter + private String tsfmtFile; + + @Parameter + private String typescriptFormatterVersion; + + @Parameter + private String typescriptVersion; + + @Parameter + private String tslintVersion; + + @Parameter + private String npmExecutable; + + @Parameter + private Map config; + + @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) + private File buildDir; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { + + Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); + if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); } + if (typescriptVersion != null) { devDependencies.put("typescript", typescriptVersion); } + if (tslintVersion != null) { devDependencies.put("tslint", tslintVersion); } + + File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null; + + TypedTsFmtConfigFile configFile = null; + + // check that there is only 1 config file or inline config + if (this.tsconfigFile != null + ^ this.tsfmtFile != null + ^ this.tslintFile != null + ^ this.vscodeFile != null) { + if (this.tsconfigFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); + } else if (this.tsfmtFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); + } else if (this.tslintFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); + } else if (this.vscodeFile != null) { + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); + } + } else { + if (config == null) { + throw new IllegalArgumentException("must specify exactly one configFile or config"); + } + } + + if (buildDir == null) { + buildDir = new File("."); + } + return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, configFile, config); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java new file mode 100644 index 0000000000..9fe596e4ab --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016 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.maven.typescript; + +import java.util.Set; + +import com.diffplug.common.collect.ImmutableSet; +import com.diffplug.spotless.maven.FormatterFactory; +import com.diffplug.spotless.maven.generic.LicenseHeader; + +/** + * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element. + *

+ * It defines a formatter for typescript source files. + */ +public class Typescript extends FormatterFactory { + + private static final Set DEFAULT_INCLUDES = ImmutableSet.of("**/*.ts"); + + private static final String LICENSE_HEADER_DELIMITER = null; + + @Override + public Set defaultIncludes() { + return DEFAULT_INCLUDES; + } + + @Override + public String licenseHeaderDelimiter() { + return LICENSE_HEADER_DELIMITER; + } + + public void addTsfmt(Tsfmt tsfmt) { + addStepFactory(tsfmt); + } + +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java index 8036b1899e..e27f965a6a 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java @@ -118,6 +118,10 @@ protected void writePomWithCssSteps(String... steps) throws IOException { writePom(groupWithSteps("css", steps)); } + protected void writePomWithTypescriptSteps(String... steps) throws IOException { + writePom(groupWithSteps("typescript", steps)); + } + protected void writePom(String... configuration) throws IOException { writePom(null, configuration); } 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 new file mode 100644 index 0000000000..c56ceeb5d6 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java @@ -0,0 +1,125 @@ +/* + * Copyright 2016 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.maven.typescript; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Ignore; +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; +import com.diffplug.spotless.maven.MavenRunner; + +public class TypescriptFormatStepTest extends MavenIntegrationTest { + + @Test + public void testTypescriptTsfmtTslintFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tslint.json", + " 7.2.2", + ""); + setFile("tslint.json").toResource("typescript/tsfmt/tslint.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + @Ignore + public void testTypescriptTsfmtTsConfigFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/", + " ${basedir}/tsconfig.json", + " ${basedir}/tsfmt.json", + " 7.2.2", + ""); + setFile("tsconfig.json").toResource("typescript/tsfmt/tsconfig.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescriptTsfmtTsFmtFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tsfmt.json", + " 7.2.2", + ""); + setFile("tsfmt.json").toResource("typescript/tsfmt/tsfmt.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescriptTsfmtVsCodeFile() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/vscode.json", + " 7.2.2", + ""); + setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescriptTsfmtInlineConfig() throws Exception { + writePomWithTypescriptSteps( + "", + " ", + " 1", + " true", + " ", + " 7.2.2", + ""); + setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); + } + + @Test + public void testTypescript_2_Configs() throws Exception { + writePomWithTypescriptSteps( + "", + " ${basedir}/tslint.json", + " ${basedir}/tslint.json", + " 7.2.2", + ""); + setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); + setFile("tsfmt.json").toResource("typescript/tsfmt/tsfmt.json"); + + String path = "src/main/typescript/test.ts"; + setFile(path).toResource("typescript/tsfmt/TypescriptCodeUnformatted.test"); + MavenRunner.Result result = mavenRunner().withArguments("spotless:apply").runHasError(); + assertThat(result.output()).contains("must specify exactly one configFile or config"); + } +} diff --git a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test new file mode 100644 index 0000000000..63d84d87ca --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeFormatted.test @@ -0,0 +1,4 @@ +import '@stencil/router'; + +class Sample { hello(word = "world") { return "Hello, " + word; } } +new Sample().hello("TypeScript"); \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test new file mode 100644 index 0000000000..e366b51e0d --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/TypescriptCodeUnformatted.test @@ -0,0 +1,4 @@ +import '@stencil/router'; + +class Sample{hello(word="world"){return "Hello, "+word;}} +new Sample().hello("TypeScript"); \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tsconfig.json b/testlib/src/main/resources/typescript/tsfmt/tsconfig.json new file mode 100644 index 0000000000..4b87261319 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/tsconfig.json @@ -0,0 +1,3 @@ +{ + "include": ["src/main/typescript/*.ts"] +} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tsfmt.json b/testlib/src/main/resources/typescript/tsfmt/tsfmt.json new file mode 100644 index 0000000000..86f08eced6 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/tsfmt.json @@ -0,0 +1,24 @@ +{ + "baseIndentSize": 0, + "indentSize": 4, + "tabSize": 4, + "indentStyle": 2, + "newLineCharacter": "\r\n", + "convertTabsToSpaces": true, + "insertSpaceAfterCommaDelimiter": true, + "insertSpaceAfterSemicolonInForStatements": true, + "insertSpaceBeforeAndAfterBinaryOperators": true, + "insertSpaceAfterConstructor": false, + "insertSpaceAfterKeywordsInControlFlowStatements": true, + "insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, + "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true, + "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, + "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false, + "insertSpaceAfterTypeAssertion": false, + "insertSpaceBeforeFunctionParenthesis": false, + "insertSpaceBeforeTypeAnnotation": true, + "placeOpenBraceOnNewLineForFunctions": false, + "placeOpenBraceOnNewLineForControlBlocks": false +} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/tslint.json b/testlib/src/main/resources/typescript/tsfmt/tslint.json new file mode 100644 index 0000000000..3c77774f17 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/tslint.json @@ -0,0 +1,11 @@ +{ + "rules": { + "indent": [true, 4], + "whitespace": [true, + "check-branch", + "check-operator", + "check-separator", + "check-typecast" + ] + } +} \ No newline at end of file diff --git a/testlib/src/main/resources/typescript/tsfmt/vscode.json b/testlib/src/main/resources/typescript/tsfmt/vscode.json new file mode 100644 index 0000000000..9f3ee12bf0 --- /dev/null +++ b/testlib/src/main/resources/typescript/tsfmt/vscode.json @@ -0,0 +1,15 @@ +{ + // Place your settings in this file to overwrite default and user settings. + "typescript.format.enable": true, + "typescript.format.insertSpaceAfterCommaDelimiter": true, + "typescript.format.insertSpaceAfterSemicolonInForStatements": true, + "typescript.format.insertSpaceBeforeAndAfterBinaryOperators": true, + "typescript.format.insertSpaceAfterKeywordsInControlFlowStatements": true, + "typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, + "typescript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false, + "typescript.format.placeOpenBraceOnNewLineForFunctions": false, + "typescript.format.placeOpenBraceOnNewLineForControlBlocks": false +} \ No newline at end of file From 0e429a5304cd60d727f2fab228c2ccb35060657d Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 14:51:58 +0100 Subject: [PATCH 02/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c311133da..8fe571ec2a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ extra('java.EclipseFormatterStep') +'{{yes}} | {{yes}} lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{no}} |', lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} |', lib('npm.PrettierFormatterStep') +'{{yes}} | {{no}} | {{no}} |', -lib('npm.TsFmtFormatterStep') +'{{yes}} | {{no}} | {{no}} |', +lib('npm.TsFmtFormatterStep') +'{{yes}} | {{yes}} | {{no}} |', lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |', lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |', extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | {{no}} |', From bb72ebdc4da6106970af8950c574f3a71c1b7964 Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 15:02:26 +0100 Subject: [PATCH 03/10] Update README to include tsfmt maven plugin --- plugin-maven/README.md | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 7e03e07eee..6b46764f09 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -193,6 +193,80 @@ By default, all files matching `src/main/cpp/**/*.` and `src/test/cpp/**/*. ``` Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). Within the preferences *Edit...* dialog, you can export your configuration as XML file, which can be used as a configuration ``. If no `` is provided, the CDT default configuration is used. + + +## Applying to Typescript source + +To use tsfmt, you first have to specify the files that you want it to apply to. +Then you specify `tsfmt`, and optionally how you want to apply it. + +By default, all typescript source sets will be formatted. To change this, +set the `target` parameter as described in the [Custom rules](#custom) section. + +```xml + + + + + ${basedir}/path/to/repo/tslint.json + ${basedir}/path/to/repo/tsfmt.json + ${basedir}/path/to/repo/tsconfig.json + ${basedir}/path/to/repo/vscode.json + + 7.2.2 + 3.3.3 + 5.12.1 + + + +``` +Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective +[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34). + +*Please note:* +The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless, + hence you are required to provide resolvable file paths for config files. + +... or alternatively provide the configuration inline ... + +```xml + + + + + + 1 + true + + + 7.2.2 + 3.3.3 + 5.12.1 + + + +``` + +See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available. + +### Prerequisite: tsfmt requires a working NodeJS version + +tsfmt is based on NodeJS, so to use it, a working NodeJS installation (especially npm) is required on the host running spotless. +Spotless will try to auto-discover an npm installation. If that is not working for you, it is possible to directly configure the npm binary to use. + +```xml + + + + ${basedir}/path/to/repo/tslint.json + /usr/bin/npm + + + +``` + +Spotless uses npm to install necessary packages locally. It runs tsfmt using [J2V8](https://github.com/eclipsesource/J2V8) internally after that. + ## Applying to custom sources From ae0501087c5a34ce0088ab28d67493c2062320cb Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 15:10:01 +0100 Subject: [PATCH 04/10] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 6f119b2b42..bbe8edd319 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Tsfmt Maven Plugin ([#548](https://github.com/diffplug/spotless/pull/548)) ## [1.28.0] - 2020-03-20 ### Added From 588990aa94268758d8f9cbe5e738034a6aa691ef Mon Sep 17 00:00:00 2001 From: Naciron Date: Thu, 26 Mar 2020 15:10:25 +0100 Subject: [PATCH 05/10] Update CHANGES.md --- plugin-maven/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 0bc301fa73..1027efba3e 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Tsfmt Maven Plugin ([#548](https://github.com/diffplug/spotless/pull/548)) ## [1.28.0] - 2020-03-20 ### Added From e973a2eb4901667c3ab25ad6bc7ff858001eac4b Mon Sep 17 00:00:00 2001 From: naciron Date: Thu, 26 Mar 2020 14:13:07 +0000 Subject: [PATCH 06/10] Fix unit test, correct xml end tag --- .../spotless/maven/typescript/TypescriptFormatStepTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c56ceeb5d6..2d89c2f2ed 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 @@ -94,7 +94,7 @@ public void testTypescriptTsfmtInlineConfig() throws Exception { "", " ", " 1", - " true", + " true", " ", " 7.2.2", ""); From 2ebe1630860da3bf78e6d9111c734814f0ebd974 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Mar 2020 14:24:41 +0000 Subject: [PATCH 07/10] Spotless apply --- .../spotless/maven/AbstractSpotlessMojo.java | 2 +- .../spotless/maven/typescript/Tsfmt.java | 39 +++++++++++-------- .../spotless/maven/typescript/Typescript.java | 5 +-- .../spotless/maven/MavenIntegrationTest.java | 2 +- .../typescript/TypescriptFormatStepTest.java | 14 +++---- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index e43d1c7850..195614a768 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -98,7 +98,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private Cpp cpp; - + @Parameter private Typescript typescript; diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java index c5613eb275..1eb2fbd079 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java @@ -19,6 +19,7 @@ import java.util.Map; import org.apache.maven.plugins.annotations.Parameter; + import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; @@ -39,7 +40,7 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private String tsfmtFile; - + @Parameter private String typescriptFormatterVersion; @@ -51,7 +52,7 @@ public class Tsfmt implements FormatterStepFactory { @Parameter private String npmExecutable; - + @Parameter private Map config; @@ -60,36 +61,42 @@ public class Tsfmt implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { - + Map devDependencies = TsFmtFormatterStep.defaultDevDependencies(); - if (typescriptFormatterVersion != null) { devDependencies.put("typescript-formatter", typescriptFormatterVersion); } - if (typescriptVersion != null) { devDependencies.put("typescript", typescriptVersion); } - if (tslintVersion != null) { devDependencies.put("tslint", tslintVersion); } - + if (typescriptFormatterVersion != null) { + devDependencies.put("typescript-formatter", typescriptFormatterVersion); + } + if (typescriptVersion != null) { + devDependencies.put("typescript", typescriptVersion); + } + if (tslintVersion != null) { + devDependencies.put("tslint", tslintVersion); + } + File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null; - + TypedTsFmtConfigFile configFile = null; - + // check that there is only 1 config file or inline config - if (this.tsconfigFile != null + if (this.tsconfigFile != null ^ this.tsfmtFile != null ^ this.tslintFile != null ^ this.vscodeFile != null) { if (this.tsconfigFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSCONFIG, stepConfig.getFileLocator().locateFile(tsconfigFile)); } else if (this.tsfmtFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSFMT, stepConfig.getFileLocator().locateFile(tsfmtFile)); } else if (this.tslintFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); + configFile = new TypedTsFmtConfigFile(TsConfigFileType.TSLINT, stepConfig.getFileLocator().locateFile(tslintFile)); } else if (this.vscodeFile != null) { - configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); - } + configFile = new TypedTsFmtConfigFile(TsConfigFileType.VSCODE, stepConfig.getFileLocator().locateFile(vscodeFile)); + } } else { if (config == null) { throw new IllegalArgumentException("must specify exactly one configFile or config"); } } - + if (buildDir == null) { buildDir = new File("."); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java index 9fe596e4ab..5e4a162cbd 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java @@ -19,7 +19,6 @@ import com.diffplug.common.collect.ImmutableSet; import com.diffplug.spotless.maven.FormatterFactory; -import com.diffplug.spotless.maven.generic.LicenseHeader; /** * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element. @@ -31,7 +30,7 @@ public class Typescript extends FormatterFactory { private static final Set DEFAULT_INCLUDES = ImmutableSet.of("**/*.ts"); private static final String LICENSE_HEADER_DELIMITER = null; - + @Override public Set defaultIncludes() { return DEFAULT_INCLUDES; @@ -41,7 +40,7 @@ public Set defaultIncludes() { public String licenseHeaderDelimiter() { return LICENSE_HEADER_DELIMITER; } - + public void addTsfmt(Tsfmt tsfmt) { addStepFactory(tsfmt); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java index e27f965a6a..09da08e2e5 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java @@ -121,7 +121,7 @@ protected void writePomWithCssSteps(String... steps) throws IOException { protected void writePomWithTypescriptSteps(String... steps) throws IOException { writePom(groupWithSteps("typescript", steps)); } - + protected void writePom(String... configuration) throws IOException { writePom(null, configuration); } 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 2d89c2f2ed..3ef3d734da 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 @@ -39,7 +39,7 @@ public void testTypescriptTsfmtTslintFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test @Ignore public void testTypescriptTsfmtTsConfigFile() throws Exception { @@ -57,7 +57,7 @@ public void testTypescriptTsfmtTsConfigFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescriptTsfmtTsFmtFile() throws Exception { writePomWithTypescriptSteps( @@ -72,12 +72,12 @@ public void testTypescriptTsfmtTsFmtFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescriptTsfmtVsCodeFile() throws Exception { writePomWithTypescriptSteps( "", - " ${basedir}/vscode.json", + " ${basedir}/vscode.json", " 7.2.2", ""); setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); @@ -87,7 +87,7 @@ public void testTypescriptTsfmtVsCodeFile() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescriptTsfmtInlineConfig() throws Exception { writePomWithTypescriptSteps( @@ -95,7 +95,7 @@ public void testTypescriptTsfmtInlineConfig() throws Exception { " ", " 1", " true", - " ", + " ", " 7.2.2", ""); setFile("vscode.json").toResource("typescript/tsfmt/vscode.json"); @@ -105,7 +105,7 @@ public void testTypescriptTsfmtInlineConfig() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile(path).sameAsResource("typescript/tsfmt/TypescriptCodeFormatted.test"); } - + @Test public void testTypescript_2_Configs() throws Exception { writePomWithTypescriptSteps( From 588aa43bd906c496ebdbc8af339507a42973d13e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Mar 2020 14:41:27 +0000 Subject: [PATCH 08/10] Spotless apply --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8fe571ec2a..c5a1de180d 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :white_large_square: | | [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: | | [`npm.PrettierFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | -| [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | +| [`npm.TsFmtFormatterStep`](lib/src/main/java/com/diffplug/spotless/npm/TsFmtFormatterStep.java) | :+1: | :+1: | :white_large_square: | | [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: | | [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | | [`wtp.EclipseWtpFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java) | :+1: | :+1: | :white_large_square: | From 811c66134b2a31354d112f753419c04a2d69b4c7 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 28 Mar 2020 22:09:40 -0700 Subject: [PATCH 09/10] Make the Typescript defaults a little less expensive to compute, and closer to the other defaults. --- .../java/com/diffplug/spotless/maven/typescript/Typescript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java index 5e4a162cbd..e2cd953268 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Typescript.java @@ -27,7 +27,7 @@ */ public class Typescript extends FormatterFactory { - private static final Set DEFAULT_INCLUDES = ImmutableSet.of("**/*.ts"); + private static final Set DEFAULT_INCLUDES = ImmutableSet.of("src/**/*.ts"); private static final String LICENSE_HEADER_DELIMITER = null; From 90a187039eefb16eb21ea41ff0d40e8363577b5c Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 28 Mar 2020 22:10:15 -0700 Subject: [PATCH 10/10] Condense the typescript docs for plugin-maven. --- plugin-maven/README.md | 52 ++++++++++++------------------------------ 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 6b46764f09..1e1948dea9 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -197,43 +197,19 @@ Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentati ## Applying to Typescript source -To use tsfmt, you first have to specify the files that you want it to apply to. -Then you specify `tsfmt`, and optionally how you want to apply it. - -By default, all typescript source sets will be formatted. To change this, -set the `target` parameter as described in the [Custom rules](#custom) section. - ```xml - + + + src/**/*.ts + + ${basedir}/path/to/repo/tslint.json ${basedir}/path/to/repo/tsfmt.json ${basedir}/path/to/repo/tsconfig.json ${basedir}/path/to/repo/vscode.json - - 7.2.2 - 3.3.3 - 5.12.1 - - - -``` -Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective -[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34). - -*Please note:* -The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless, - hence you are required to provide resolvable file paths for config files. - -... or alternatively provide the configuration inline ... - -```xml - - - - 1 true @@ -247,7 +223,12 @@ The auto-discovery of config files (up the file tree) will not work when using t ``` -See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available. +Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective +[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34). See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available. + +*Please note:* +The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless, + hence you are required to provide resolvable file paths for config files. ### Prerequisite: tsfmt requires a working NodeJS version @@ -255,14 +236,9 @@ tsfmt is based on NodeJS, so to use it, a working NodeJS installation (especiall Spotless will try to auto-discover an npm installation. If that is not working for you, it is possible to directly configure the npm binary to use. ```xml - - - - ${basedir}/path/to/repo/tslint.json - /usr/bin/npm - - - + + ... + /usr/bin/npm ``` Spotless uses npm to install necessary packages locally. It runs tsfmt using [J2V8](https://github.com/eclipsesource/J2V8) internally after that.