Skip to content

Commit

Permalink
smaller api for tsfmt config files
Browse files Browse the repository at this point in the history
and represent config file content in TsFmtFormatterStep's state
  • Loading branch information
simschla committed Sep 11, 2018
1 parent bf16cab commit 3d3f304
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.extra.npm;

import java.util.Arrays;

public enum TsConfigFileType {
TSCONFIG, TSLINT, VSCODE, TSFMT;

public static TsConfigFileType forNameIgnoreCase(String name) {
return Arrays.stream(values())
.filter(type -> type.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Config file type " + name + " is not supported. Supported values (case is ignored): " + Arrays.toString(values())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,40 @@ public class TsFmtFormatterStep {

public static final String NAME = "tsfmt-format";

public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, Map<String, Object> tsFmtCliOptions, Map<String, Object> inlineTsFmtSettings) {
public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, File baseDir, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) {
requireNonNull(provisioner);
requireNonNull(buildDir);
validateOptions(requireNonNull(tsFmtCliOptions));
requireNonNull(inlineTsFmtSettings);
requireNonNull(baseDir);
return FormatterStep.createLazy(NAME,
() -> new State(NAME, provisioner, buildDir, npm, tsFmtCliOptions, inlineTsFmtSettings),
() -> new State(NAME, provisioner, buildDir, npm, baseDir, configFile, inlineTsFmtSettings),
State::createFormatterFunc);
}

private static void validateOptions(Map<String, Object> options) {
final Set<String> optionNames = new TreeSet<>(options.keySet());
optionNames.retainAll(asList("dryRun", "replace", "verify"));

if (!optionNames.isEmpty()) {
throw new BlacklistedOptionException("The following config options are specified but not supported by spotless: " + optionNames);
}
}

public static class State extends NpmFormatterStepStateBase implements Serializable {

private static final long serialVersionUID = -3811104513825329168L;

private final TreeMap<String, Object> tsFmtCliOptions;

private final TreeMap<String, Object> inlineTsFmtSettings;

private final File buildDir;

public State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, Map<String, Object> tsFmtCliOptions, Map<String, Object> inlineTsFmtSettings) throws IOException {
@Nullable
private final TypedTsFmtConfigFile configFile;

private final File baseDir;

public State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, File baseDir, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) throws IOException {
super(stepName,
provisioner,
new NpmConfig(
readFileFromClasspath(TsFmtFormatterStep.class, "/com/diffplug/spotless/extra/npm/tsfmt-package.json"),
"typescript-formatter"),
buildDir,
npm);
this.buildDir = buildDir;
this.tsFmtCliOptions = new TreeMap<>(tsFmtCliOptions);
this.inlineTsFmtSettings = new TreeMap<>(inlineTsFmtSettings);
this.buildDir = requireNonNull(buildDir);
this.baseDir = requireNonNull(baseDir);
this.configFile = configFile;
this.inlineTsFmtSettings = inlineTsFmtSettings == null ? new TreeMap<>() : new TreeMap<>(inlineTsFmtSettings);
}

@Override
Expand Down Expand Up @@ -146,25 +140,17 @@ private V8FunctionWrapper createFormatResultCallback(NodeJSWrapper nodeJSWrapper
}

private Map<String, Object> unifyOptions() {
Map<String, Object> unified = new HashMap<>(this.tsFmtCliOptions);

Map<String, Object> unified = new HashMap<>();
if (!this.inlineTsFmtSettings.isEmpty()) {
removeAllConfigFileSettings(unified);
File targetFile = new File(this.buildDir, "inline-tsfmt.json");
SimpleJsonWriter.of(this.inlineTsFmtSettings).toJsonFile(targetFile);
unified.put("tsfmt", true);
unified.put("tsfmtFile", targetFile.getAbsolutePath());
} else if (this.configFile != null) {
unified.put(this.configFile.configFileEnabledOptionName(), Boolean.TRUE);
unified.put(this.configFile.configFileOptionName(), this.configFile.absolutePath());
}
return unified;
}

private void removeAllConfigFileSettings(Map<String, Object> settings) {
Arrays.asList("tsconfig", "tslint", "editorconfig", "vscode", "tsfmt").forEach(
format -> {
settings.remove(format);
settings.remove(format + "File");
});
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.extra.npm;

import static java.util.Objects.requireNonNull;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;

import com.diffplug.common.base.Errors;
import com.diffplug.spotless.FileSignature;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class TypedTsFmtConfigFile implements Serializable {

private static final long serialVersionUID = -4442310349275775501L;

private final TsConfigFileType configFileType;

private final File configFile;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
@SuppressWarnings("unused")
private final FileSignature configFileSignature;

public TypedTsFmtConfigFile(TsConfigFileType configFileType, File configFile) {
this.configFileType = requireNonNull(configFileType);
this.configFile = requireNonNull(configFile);
try {
this.configFileSignature = FileSignature.signAsList(configFile);
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}

TsConfigFileType configFileType() {
return configFileType;
}

File configFile() {
return configFile;
}

String configFileEnabledOptionName() {
return this.configFileType.name().toLowerCase(Locale.ENGLISH);
}

String configFileOptionName() {
return this.configFileEnabledOptionName() + "File";
}

String absolutePath() {
return this.configFile.getAbsolutePath();
}

static TypedTsFmtConfigFile named(String name, File file) {
return new TypedTsFmtConfigFile(TsConfigFileType.forNameIgnoreCase(name), file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.diffplug.spotless.extra.npm;

import static org.assertj.core.api.Assertions.fail;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -65,11 +63,8 @@ public void formattingUsingConfigFile() throws Exception {
TestProvisioner.mavenCentral(),
buildDir(),
npmExecutable(),
ImmutableMap.<String, Object> builder()
.put("basedir", configFile.getParent())
.put(configFileNameWithoutExtension, Boolean.TRUE)
.put(configFileNameWithoutExtension + "File", configFile.getPath())
.build(),
configFile.getParentFile(),
TypedTsFmtConfigFile.named(configFileNameWithoutExtension, configFile),
Collections.emptyMap());

try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
Expand All @@ -89,43 +84,13 @@ public void formattingUsingInlineConfigWorks() throws Exception {
TestProvisioner.mavenCentral(),
buildDir(),
npmExecutable(),
ImmutableMap.<String, Object> builder()
.put("basedir", buildDir().getAbsolutePath())
.build(),
buildDir().getAbsoluteFile(),
null,
inlineConfig);

try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
stepHarness.testResource("npm/tsfmt/tsfmt/tsfmt.dirty", "npm/tsfmt/tsfmt/tsfmt.clean");
}
}
}

@Category(NpmTest.class)
@RunWith(Parameterized.class)
public static class TsFmtBlacklistedOptionsTest extends NpmFormatterStepCommonTests {
@Parameterized.Parameter
public String blackListedOption;

@Parameterized.Parameters(name = "{index}: config option '{0}' is blacklisted")
public static Iterable<String> blacklistedOption() {
return Arrays.asList("dryRun", "replace", "verify");
}

@Test(expected = BlacklistedOptionException.class)
public void blacklistedOptionIsThrown() throws Exception {
TsFmtFormatterStep.create(
TestProvisioner.mavenCentral(),
buildDir(),
npmExecutable(),
ImmutableMap.<String, Object> builder()
.put(blackListedOption, Boolean.TRUE)
.build(),
Collections.emptyMap());

fail("should never be reached!");

}

}

}
19 changes: 8 additions & 11 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,16 @@ set the `target` parameter as described in the [Custom rules](#custom) section.
spotless {
typescript {
// using existing config files
tsfmt().configFile('tslint', '/path/to/repo/tslint.json')
tsfmt().tslintFile('/path/to/repo/tslint.json')
}
}
```
Supported config file types are the ones supported by [tsfmt config options](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L26).
Use the option names of type boolean in the tsfmt config object: `tsconfig`, `tslint`, `editorconfig`, `vscode` or `tsfmt`. For the option `editorconfig`, no path is supported.
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 prettier within spotless,
hence you are required to provide absolute file paths for config files.
- The config file type `editorconfig` is only read from its default location (the user'home).
Any value you pass for the path will be ignored.
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 ...

Expand All @@ -333,11 +331,9 @@ spotless {
}
```

See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L8) for what is available.

*Please note:* If both `configFile` and `config` is provided, only `config` is used.

See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available.

... and it is also possible to apply `prettier()` instead of `tsfmt()` as formatter. For details see the section about [prettier](#typescript-prettier).

### Prerequisite: tsfmt requires a working NodeJS version

Expand Down Expand Up @@ -408,6 +404,7 @@ spotless {
}
```

<a name="typescript-prettier"></a>
Prettier can also be applied from within the [typescript config block](#typescript-formatter):

```gradle
Expand Down
Loading

0 comments on commit 3d3f304

Please sign in to comment.