Skip to content

Commit 3d3f304

Browse files
committed
smaller api for tsfmt config files
and represent config file content in TsFmtFormatterStep's state
1 parent bf16cab commit 3d3f304

File tree

7 files changed

+171
-123
lines changed

7 files changed

+171
-123
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.npm;
17+
18+
import java.util.Arrays;
19+
20+
public enum TsConfigFileType {
21+
TSCONFIG, TSLINT, VSCODE, TSFMT;
22+
23+
public static TsConfigFileType forNameIgnoreCase(String name) {
24+
return Arrays.stream(values())
25+
.filter(type -> type.name().equalsIgnoreCase(name))
26+
.findFirst()
27+
.orElseThrow(() -> new IllegalArgumentException("Config file type " + name + " is not supported. Supported values (case is ignored): " + Arrays.toString(values())));
28+
}
29+
}

lib-extra/src/main/java/com/diffplug/spotless/extra/npm/TsFmtFormatterStep.java

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,40 @@ public class TsFmtFormatterStep {
3535

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

38-
public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, Map<String, Object> tsFmtCliOptions, Map<String, Object> inlineTsFmtSettings) {
38+
public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, File baseDir, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) {
3939
requireNonNull(provisioner);
4040
requireNonNull(buildDir);
41-
validateOptions(requireNonNull(tsFmtCliOptions));
42-
requireNonNull(inlineTsFmtSettings);
41+
requireNonNull(baseDir);
4342
return FormatterStep.createLazy(NAME,
44-
() -> new State(NAME, provisioner, buildDir, npm, tsFmtCliOptions, inlineTsFmtSettings),
43+
() -> new State(NAME, provisioner, buildDir, npm, baseDir, configFile, inlineTsFmtSettings),
4544
State::createFormatterFunc);
4645
}
4746

48-
private static void validateOptions(Map<String, Object> options) {
49-
final Set<String> optionNames = new TreeSet<>(options.keySet());
50-
optionNames.retainAll(asList("dryRun", "replace", "verify"));
51-
52-
if (!optionNames.isEmpty()) {
53-
throw new BlacklistedOptionException("The following config options are specified but not supported by spotless: " + optionNames);
54-
}
55-
}
56-
5747
public static class State extends NpmFormatterStepStateBase implements Serializable {
5848

5949
private static final long serialVersionUID = -3811104513825329168L;
6050

61-
private final TreeMap<String, Object> tsFmtCliOptions;
62-
6351
private final TreeMap<String, Object> inlineTsFmtSettings;
6452

6553
private final File buildDir;
6654

67-
public State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, Map<String, Object> tsFmtCliOptions, Map<String, Object> inlineTsFmtSettings) throws IOException {
55+
@Nullable
56+
private final TypedTsFmtConfigFile configFile;
57+
58+
private final File baseDir;
59+
60+
public State(String stepName, Provisioner provisioner, File buildDir, @Nullable File npm, File baseDir, @Nullable TypedTsFmtConfigFile configFile, @Nullable Map<String, Object> inlineTsFmtSettings) throws IOException {
6861
super(stepName,
6962
provisioner,
7063
new NpmConfig(
7164
readFileFromClasspath(TsFmtFormatterStep.class, "/com/diffplug/spotless/extra/npm/tsfmt-package.json"),
7265
"typescript-formatter"),
7366
buildDir,
7467
npm);
75-
this.buildDir = buildDir;
76-
this.tsFmtCliOptions = new TreeMap<>(tsFmtCliOptions);
77-
this.inlineTsFmtSettings = new TreeMap<>(inlineTsFmtSettings);
68+
this.buildDir = requireNonNull(buildDir);
69+
this.baseDir = requireNonNull(baseDir);
70+
this.configFile = configFile;
71+
this.inlineTsFmtSettings = inlineTsFmtSettings == null ? new TreeMap<>() : new TreeMap<>(inlineTsFmtSettings);
7872
}
7973

8074
@Override
@@ -146,25 +140,17 @@ private V8FunctionWrapper createFormatResultCallback(NodeJSWrapper nodeJSWrapper
146140
}
147141

148142
private Map<String, Object> unifyOptions() {
149-
Map<String, Object> unified = new HashMap<>(this.tsFmtCliOptions);
150-
143+
Map<String, Object> unified = new HashMap<>();
151144
if (!this.inlineTsFmtSettings.isEmpty()) {
152-
removeAllConfigFileSettings(unified);
153145
File targetFile = new File(this.buildDir, "inline-tsfmt.json");
154146
SimpleJsonWriter.of(this.inlineTsFmtSettings).toJsonFile(targetFile);
155147
unified.put("tsfmt", true);
156148
unified.put("tsfmtFile", targetFile.getAbsolutePath());
149+
} else if (this.configFile != null) {
150+
unified.put(this.configFile.configFileEnabledOptionName(), Boolean.TRUE);
151+
unified.put(this.configFile.configFileOptionName(), this.configFile.absolutePath());
157152
}
158153
return unified;
159154
}
160-
161-
private void removeAllConfigFileSettings(Map<String, Object> settings) {
162-
Arrays.asList("tsconfig", "tslint", "editorconfig", "vscode", "tsfmt").forEach(
163-
format -> {
164-
settings.remove(format);
165-
settings.remove(format + "File");
166-
});
167-
}
168-
169155
}
170156
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.npm;
17+
18+
import static java.util.Objects.requireNonNull;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.Serializable;
23+
import java.util.Locale;
24+
25+
import com.diffplug.common.base.Errors;
26+
import com.diffplug.spotless.FileSignature;
27+
28+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29+
30+
public class TypedTsFmtConfigFile implements Serializable {
31+
32+
private static final long serialVersionUID = -4442310349275775501L;
33+
34+
private final TsConfigFileType configFileType;
35+
36+
private final File configFile;
37+
38+
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
39+
@SuppressWarnings("unused")
40+
private final FileSignature configFileSignature;
41+
42+
public TypedTsFmtConfigFile(TsConfigFileType configFileType, File configFile) {
43+
this.configFileType = requireNonNull(configFileType);
44+
this.configFile = requireNonNull(configFile);
45+
try {
46+
this.configFileSignature = FileSignature.signAsList(configFile);
47+
} catch (IOException e) {
48+
throw Errors.asRuntime(e);
49+
}
50+
}
51+
52+
TsConfigFileType configFileType() {
53+
return configFileType;
54+
}
55+
56+
File configFile() {
57+
return configFile;
58+
}
59+
60+
String configFileEnabledOptionName() {
61+
return this.configFileType.name().toLowerCase(Locale.ENGLISH);
62+
}
63+
64+
String configFileOptionName() {
65+
return this.configFileEnabledOptionName() + "File";
66+
}
67+
68+
String absolutePath() {
69+
return this.configFile.getAbsolutePath();
70+
}
71+
72+
static TypedTsFmtConfigFile named(String name, File file) {
73+
return new TypedTsFmtConfigFile(TsConfigFileType.forNameIgnoreCase(name), file);
74+
}
75+
76+
}

lib-extra/src/test/java/com/diffplug/spotless/extra/npm/TsFmtFormatterStepTest.java

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.extra.npm;
1717

18-
import static org.assertj.core.api.Assertions.fail;
19-
2018
import java.io.File;
2119
import java.nio.charset.StandardCharsets;
2220
import java.nio.file.Files;
@@ -65,11 +63,8 @@ public void formattingUsingConfigFile() throws Exception {
6563
TestProvisioner.mavenCentral(),
6664
buildDir(),
6765
npmExecutable(),
68-
ImmutableMap.<String, Object> builder()
69-
.put("basedir", configFile.getParent())
70-
.put(configFileNameWithoutExtension, Boolean.TRUE)
71-
.put(configFileNameWithoutExtension + "File", configFile.getPath())
72-
.build(),
66+
configFile.getParentFile(),
67+
TypedTsFmtConfigFile.named(configFileNameWithoutExtension, configFile),
7368
Collections.emptyMap());
7469

7570
try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
@@ -89,43 +84,13 @@ public void formattingUsingInlineConfigWorks() throws Exception {
8984
TestProvisioner.mavenCentral(),
9085
buildDir(),
9186
npmExecutable(),
92-
ImmutableMap.<String, Object> builder()
93-
.put("basedir", buildDir().getAbsolutePath())
94-
.build(),
87+
buildDir().getAbsoluteFile(),
88+
null,
9589
inlineConfig);
9690

9791
try (StepHarness stepHarness = StepHarness.forStep(formatterStep)) {
9892
stepHarness.testResource("npm/tsfmt/tsfmt/tsfmt.dirty", "npm/tsfmt/tsfmt/tsfmt.clean");
9993
}
10094
}
10195
}
102-
103-
@Category(NpmTest.class)
104-
@RunWith(Parameterized.class)
105-
public static class TsFmtBlacklistedOptionsTest extends NpmFormatterStepCommonTests {
106-
@Parameterized.Parameter
107-
public String blackListedOption;
108-
109-
@Parameterized.Parameters(name = "{index}: config option '{0}' is blacklisted")
110-
public static Iterable<String> blacklistedOption() {
111-
return Arrays.asList("dryRun", "replace", "verify");
112-
}
113-
114-
@Test(expected = BlacklistedOptionException.class)
115-
public void blacklistedOptionIsThrown() throws Exception {
116-
TsFmtFormatterStep.create(
117-
TestProvisioner.mavenCentral(),
118-
buildDir(),
119-
npmExecutable(),
120-
ImmutableMap.<String, Object> builder()
121-
.put(blackListedOption, Boolean.TRUE)
122-
.build(),
123-
Collections.emptyMap());
124-
125-
fail("should never be reached!");
126-
127-
}
128-
129-
}
130-
13196
}

plugin-gradle/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,16 @@ set the `target` parameter as described in the [Custom rules](#custom) section.
307307
spotless {
308308
typescript {
309309
// using existing config files
310-
tsfmt().configFile('tslint', '/path/to/repo/tslint.json')
310+
tsfmt().tslintFile('/path/to/repo/tslint.json')
311311
}
312312
}
313313
```
314-
Supported config file types are the ones supported by [tsfmt config options](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L26).
315-
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.
314+
Supported config file types are `tsconfigFile`, `tslintFile`, `vscodeFile` and `tsfmtFile`. They are corresponding to the respective
315+
[tsfmt-parameters](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/index.ts#L27L34).
316316

317317
*Please note:*
318-
- The auto-discovery of config files (up the file tree) will not work when using prettier within spotless,
319-
hence you are required to provide absolute file paths for config files.
320-
- The config file type `editorconfig` is only read from its default location (the user'home).
321-
Any value you pass for the path will be ignored.
318+
The auto-discovery of config files (up the file tree) will not work when using tsfmt within spotless,
319+
hence you are required to provide resolvable file paths for config files.
322320

323321
... or alternatively provide the configuration inline ...
324322

@@ -333,11 +331,9 @@ spotless {
333331
}
334332
```
335333

336-
See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L8) for what is available.
337-
338-
*Please note:* If both `configFile` and `config` is provided, only `config` is used.
339-
334+
See [tsfmt's default config settings](https://github.com/vvakame/typescript-formatter/blob/7764258ad42ac65071399840d1b8701868510ca7/lib/utils.ts#L11L32) for what is available.
340335

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

342338
### Prerequisite: tsfmt requires a working NodeJS version
343339

@@ -408,6 +404,7 @@ spotless {
408404
}
409405
```
410406

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

413410
```gradle

0 commit comments

Comments
 (0)