Skip to content

Commit ca4136e

Browse files
committed
feat: add support for prettier 3.x
This fixes diffplug#1751
1 parent 354a582 commit ca4136e

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed
Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const prettier = require("prettier");
22

33
app.post("/prettier/config-options", (req, res) => {
4-
var config_data = req.body;
5-
var prettier_config_path = config_data.prettier_config_path;
6-
var prettier_config_options = config_data.prettier_config_options || {};
4+
const config_data = req.body;
5+
const prettier_config_path = config_data.prettier_config_path;
6+
const prettier_config_options = config_data.prettier_config_options || {};
77

88
if (prettier_config_path) {
99
prettier
1010
.resolveConfig(undefined, { config: prettier_config_path })
1111
.then(options => {
12-
var mergedConfigOptions = mergeConfigOptions(options, prettier_config_options);
12+
const mergedConfigOptions = mergeConfigOptions(options, prettier_config_options);
1313
res.set("Content-Type", "application/json")
1414
res.json(mergedConfigOptions);
1515
})
@@ -20,12 +20,12 @@ app.post("/prettier/config-options", (req, res) => {
2020
res.json(prettier_config_options);
2121
});
2222

23-
app.post("/prettier/format", (req, res) => {
24-
var format_data = req.body;
23+
app.post("/prettier/format", async (req, res) => {
24+
const format_data = req.body;
2525

26-
var formatted_file_content = "";
26+
let formatted_file_content = "";
2727
try {
28-
formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
28+
formatted_file_content = await prettierFormat(format_data.file_content, format_data.config_options);
2929
} catch(err) {
3030
res.status(500).send("Error while formatting: " + err);
3131
return;
@@ -34,7 +34,20 @@ app.post("/prettier/format", (req, res) => {
3434
res.send(formatted_file_content);
3535
});
3636

37-
var mergeConfigOptions = function(resolved_config_options, config_options) {
37+
const prettierFormat = async function(file_content, config_options) {
38+
const result = prettier.format(file_content, config_options);
39+
40+
// Check if result is a Promise (version 3.0.0 and above)
41+
if (typeof result.then === 'function') {
42+
return result;
43+
}
44+
45+
// If it's not a Promise (meaning it's a string), wrap it in a Promise (< 3.0.0)
46+
return Promise.resolve(result);
47+
}
48+
49+
50+
const mergeConfigOptions = function(resolved_config_options, config_options) {
3851
if (resolved_config_options !== undefined && config_options !== undefined) {
3952
return extend(resolved_config_options, config_options);
4053
}
@@ -46,15 +59,15 @@ var mergeConfigOptions = function(resolved_config_options, config_options) {
4659
}
4760
};
4861

49-
var extend = function() {
62+
const extend = function() {
5063
// Variables
51-
var extended = {};
52-
var i = 0;
53-
var length = arguments.length;
64+
const extended = {};
65+
let i = 0;
66+
const length = arguments.length;
5467

5568
// Merge the object into the extended object
56-
var merge = function(obj) {
57-
for (var prop in obj) {
69+
const merge = function (obj) {
70+
for (const prop in obj) {
5871
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
5972
extended[prop] = obj[prop];
6073
}
@@ -63,9 +76,8 @@ var extend = function() {
6376

6477
// Loop through each object and conduct a merge
6578
for (; i < length; i++) {
66-
var obj = arguments[i];
79+
const obj = arguments[i];
6780
merge(obj);
6881
}
69-
7082
return extended;
7183
};

testlib/src/test/java/com/diffplug/spotless/npm/PrettierFormatterStepTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.File;
1919
import java.util.Collections;
20+
import java.util.Map;
2021

2122
import org.junit.jupiter.api.Nested;
2223
import org.junit.jupiter.api.Test;
@@ -38,17 +39,27 @@ class PrettierFormatterStepTest extends ResourceHarness {
3839
@Nested
3940
class PrettierFormattingOfFileTypesIsWorking extends NpmFormatterStepCommonTests {
4041

41-
@ParameterizedTest(name = "{index}: prettier can be applied to {0}")
42+
@ParameterizedTest(name = "{index}: prettier 2.x can be applied to {0}")
4243
@ValueSource(strings = {"html", "typescript", "json", "javascript-es5", "javascript-es6", "css", "scss", "markdown", "yaml"})
43-
void formattingUsingConfigFile(String fileType) throws Exception {
44+
void formattingUsingPrettier2WithConfigFile(String fileType) throws Exception {
45+
runTestUsingPrettier(fileType, PrettierFormatterStep.defaultDevDependencies());
46+
}
47+
48+
@ParameterizedTest(name = "{index}: prettier 3.x can be applied to {0}")
49+
@ValueSource(strings = {"html", "typescript", "json", "javascript-es5", "javascript-es6", "css", "scss", "markdown", "yaml"})
50+
void formattingUsingPrettier3WithConfigFile(String fileType) throws Exception {
51+
runTestUsingPrettier(fileType, ImmutableMap.of("prettier", "3.0.0"));
52+
}
53+
54+
private void runTestUsingPrettier(String fileType, Map<String, String> dependencies) throws Exception {
4455
String filedir = "npm/prettier/filetypes/" + fileType + "/";
4556

4657
final File prettierRc = createTestFile(filedir + ".prettierrc.yml");
4758
final String dirtyFile = filedir + fileType + ".dirty";
4859
final String cleanFile = filedir + fileType + ".clean";
4960

5061
final FormatterStep formatterStep = PrettierFormatterStep.create(
51-
PrettierFormatterStep.defaultDevDependencies(),
62+
dependencies,
5263
TestProvisioner.mavenCentral(),
5364
projectDir(),
5465
buildDir(),

0 commit comments

Comments
 (0)