Skip to content

Commit 2ce9803

Browse files
committed
Improve prettier's exception to be lint-friendly.
1 parent a8dcdb6 commit 2ce9803

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/src/main/java/com/diffplug/spotless/npm/PrettierMissingParserException.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,15 @@
1919
import java.util.Arrays;
2020
import java.util.Collections;
2121
import java.util.HashMap;
22+
import java.util.List;
2223
import java.util.Map;
2324
import java.util.Objects;
2425

2526
import javax.annotation.Nonnull;
2627

27-
class PrettierMissingParserException extends RuntimeException {
28+
import com.diffplug.spotless.Lint;
29+
30+
class PrettierMissingParserException extends RuntimeException implements Lint.Has {
2831
private static final long serialVersionUID = 1L;
2932

3033
private static final Map<String, String> EXTENSIONS_TO_PLUGINS;
@@ -87,6 +90,11 @@ public PrettierMissingParserException(@Nonnull File file, Exception cause) {
8790
this.file = Objects.requireNonNull(file);
8891
}
8992

93+
@Override
94+
public List<Lint> getLints() {
95+
return List.of(Lint.atUndefinedLine("no-parser", "Could not infer a parser. Maybe you need to include a prettier plugin in devDependencies? e.g. " + recommendPlugin(file)));
96+
}
97+
9098
private static String recommendPlugin(File file) {
9199
String pluginName = guessPlugin(file);
92100
return "A good candidate for file '" + file + "' is '" + pluginName + "\n"

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void suggestsMissingJavaCommunityPlugin(String prettierVersion) throws IOExcepti
226226
"}");
227227
setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty");
228228
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail();
229-
Assertions.assertThat(spotlessApply.getOutput()).contains("could not infer a parser");
229+
Assertions.assertThat(spotlessApply.getOutput()).contains("Could not infer a parser");
230230
Assertions.assertThat(spotlessApply.getOutput()).contains("prettier-plugin-java");
231231
}
232232

0 commit comments

Comments
 (0)