Skip to content

Commit

Permalink
feat: add support for prettier 3.x
Browse files Browse the repository at this point in the history
This fixes diffplug#1751
  • Loading branch information
simschla committed Jul 18, 2023
1 parent 95d57ef commit f9c827f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
46 changes: 29 additions & 17 deletions lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const prettier = require("prettier");

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

if (prettier_config_path) {
prettier
.resolveConfig(undefined, { config: prettier_config_path })
.then(options => {
var mergedConfigOptions = mergeConfigOptions(options, prettier_config_options);
const mergedConfigOptions = mergeConfigOptions(options, prettier_config_options);
res.set("Content-Type", "application/json")
res.json(mergedConfigOptions);
})
Expand All @@ -20,12 +20,12 @@ app.post("/prettier/config-options", (req, res) => {
res.json(prettier_config_options);
});

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

var formatted_file_content = "";
let formatted_file_content = "";
try {
formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
formatted_file_content = await prettierFormat(format_data.file_content, format_data.config_options);
} catch(err) {
res.status(500).send("Error while formatting: " + err);
return;
Expand All @@ -34,7 +34,20 @@ app.post("/prettier/format", (req, res) => {
res.send(formatted_file_content);
});

var mergeConfigOptions = function(resolved_config_options, config_options) {
const prettierFormat = async function(file_content, config_options) {
const result = prettier.format(file_content, config_options);

// Check if result is a Promise (version 3.0.0 and above)
if (typeof result.then === 'function') {
return result;
}

// If it's not a Promise (meaning it's a string), wrap it in a Promise (< 3.0.0)
return Promise.resolve(result);
}


const mergeConfigOptions = function(resolved_config_options, config_options) {
if (resolved_config_options !== undefined && config_options !== undefined) {
return extend(resolved_config_options, config_options);
}
Expand All @@ -46,15 +59,15 @@ var mergeConfigOptions = function(resolved_config_options, config_options) {
}
};

var extend = function() {
const extend = function() {
// Variables
var extended = {};
var i = 0;
var length = arguments.length;
const extended = {};
let i = 0;
const length = arguments.length;

// Merge the object into the extended object
var merge = function(obj) {
for (var prop in obj) {
const merge = function (obj) {
for (const prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
extended[prop] = obj[prop];
}
Expand All @@ -63,9 +76,8 @@ var extend = function() {

// Loop through each object and conduct a merge
for (; i < length; i++) {
var obj = arguments[i];
const obj = arguments[i];
merge(obj);
}

return extended;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -38,17 +39,27 @@ class PrettierFormatterStepTest extends ResourceHarness {
@Nested
class PrettierFormattingOfFileTypesIsWorking extends NpmFormatterStepCommonTests {

@ParameterizedTest(name = "{index}: prettier can be applied to {0}")
@ParameterizedTest(name = "{index}: prettier 2.x can be applied to {0}")
@ValueSource(strings = {"html", "typescript", "json", "javascript-es5", "javascript-es6", "css", "scss", "markdown", "yaml"})
void formattingUsingConfigFile(String fileType) throws Exception {
void formattingUsingPrettier2WithConfigFile(String fileType) throws Exception {
runTestUsingPrettier(fileType, PrettierFormatterStep.defaultDevDependencies());
}

@ParameterizedTest(name = "{index}: prettier 3.x can be applied to {0}")
@ValueSource(strings = {"html", "typescript", "json", "javascript-es5", "javascript-es6", "css", "scss", "markdown", "yaml"})
void formattingUsingPrettier3WithConfigFile(String fileType) throws Exception {
runTestUsingPrettier(fileType, ImmutableMap.of("prettier", "3.0.0"));
}

private void runTestUsingPrettier(String fileType, Map<String, String> dependencies) throws Exception {
String filedir = "npm/prettier/filetypes/" + fileType + "/";

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

final FormatterStep formatterStep = PrettierFormatterStep.create(
PrettierFormatterStep.defaultDevDependencies(),
dependencies,
TestProvisioner.mavenCentral(),
projectDir(),
buildDir(),
Expand Down

0 comments on commit f9c827f

Please sign in to comment.