Format prettier_server.js (#9583)

This PURELY formats the file by opening it in Zed and hitting save with
save-on-format on.

It's been bugging me that I can't change the file without the whole
thing getting reformatted, so here we are.

Release Notes:

- N/A
This commit is contained in:
Thorsten Ball 2024-03-20 19:49:14 +01:00 committed by GitHub
parent 3a2eb12f68
commit 3853991c20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 221 additions and 195 deletions

View File

@ -15,6 +15,10 @@
"JSON": {
"tab_size": 2,
"formatter": "prettier"
},
"JavaScript": {
"tab_size": 2,
"formatter": "prettier"
}
},
"formatter": "auto"

View File

@ -17,7 +17,9 @@ fs.stat(prettierContainerPath, (err, stats) => {
}
if (!stats.isDirectory()) {
process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
process.stderr.write(
`Path '${prettierContainerPath}' exists but is not a directory\n`,
);
process.exit(1);
}
});
@ -41,7 +43,9 @@ class Prettier {
process.stderr.write(`Failed to load prettier: ${e}\n`);
process.exit(1);
}
process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
process.stderr.write(
`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`,
);
process.stdin.resume();
handleBuffer(new Prettier(prettierPath, prettier, config));
})();
@ -63,7 +67,9 @@ async function handleBuffer(prettier) {
}
sendResponse({
id: message.id,
...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
...makeError(
`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`,
),
});
});
}
@ -97,7 +103,9 @@ async function* readStdin() {
if (messageLength === null) {
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
if (streamEnded) {
await handleStreamEnded("Unexpected end of stream: headers not found");
await handleStreamEnded(
"Unexpected end of stream: headers not found",
);
continue main_loop;
} else if (buffer.length > contentLengthHeaderName.length * 10) {
await handleStreamEnded(
@ -115,10 +123,14 @@ async function* readStdin() {
.map((header) => header.split(":"))
.filter((header) => header[2] === undefined)
.filter((header) => (header[1] || "").length > 0)
.find((header) => (header[0] || "").trim() === contentLengthHeaderName);
.find(
(header) => (header[0] || "").trim() === contentLengthHeaderName,
);
const contentLength = (contentLengthHeader || [])[1];
if (contentLength === undefined) {
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
await handleStreamEnded(
`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`,
);
continue main_loop;
}
headersLength = headers.length + headerSeparator.length * 2;
@ -163,20 +175,27 @@ async function handleMessage(message, prettier) {
if (method === "prettier/format") {
if (params === undefined || params.text === undefined) {
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
throw new Error(
`Message params.text is undefined: ${JSON.stringify(message)}`,
);
}
if (params.options === undefined) {
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
throw new Error(
`Message params.options is undefined: ${JSON.stringify(message)}`,
);
}
let resolvedConfig = {};
if (params.options.filepath !== undefined) {
resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
resolvedConfig =
(await prettier.prettier.resolveConfig(params.options.filepath)) || {};
}
const plugins = Array.isArray(resolvedConfig?.plugins) && resolvedConfig.plugins.length > 0 ?
resolvedConfig.plugins :
params.options.plugins;
const plugins =
Array.isArray(resolvedConfig?.plugins) &&
resolvedConfig.plugins.length > 0
? resolvedConfig.plugins
: params.options.plugins;
const options = {
...(params.options.prettierOptions || prettier.config),
@ -194,7 +213,8 @@ async function handleMessage(message, prettier) {
sendResponse({ id, result: { text: formattedText } });
} else if (method === "prettier/clear_cache") {
prettier.prettier.clearConfigCache();
prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
prettier.config =
(await prettier.prettier.resolveConfig(prettier.path)) || {};
sendResponse({ id, result: null });
} else if (method === "initialize") {
sendResponse({
@ -237,7 +257,9 @@ function loadPrettier(prettierPath) {
try {
resolve(require(prettierPath));
} catch (err) {
reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
reject(
`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`,
);
}
}
});