Autoformat prettier_server.js (#32661)

Formatted via format-on-save in Zed with prettier version 3.5.3

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-06-13 00:58:57 -06:00 committed by GitHub
parent cb573172a3
commit 20793fc251
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,9 +17,7 @@ fs.stat(prettierContainerPath, (err, stats) => {
} }
if (!stats.isDirectory()) { if (!stats.isDirectory()) {
process.stderr.write( process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
`Path '${prettierContainerPath}' exists but is not a directory\n`,
);
process.exit(1); process.exit(1);
} }
}); });
@ -43,11 +41,7 @@ class Prettier {
process.stderr.write(`Failed to load prettier: ${e}\n`); process.stderr.write(`Failed to load prettier: ${e}\n`);
process.exit(1); process.exit(1);
} }
process.stderr.write( process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(
config,
)}\n`,
);
process.stdin.resume(); process.stdin.resume();
handleBuffer(new Prettier(prettierPath, prettier, config)); handleBuffer(new Prettier(prettierPath, prettier, config));
})(); })();
@ -69,11 +63,7 @@ async function handleBuffer(prettier) {
} }
sendResponse({ sendResponse({
id: message.id, id: message.id,
...makeError( ...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
`error during message '${JSON.stringify(
errorMessage,
)}' handling: ${e}`,
),
}); });
}); });
} }
@ -107,9 +97,7 @@ async function* readStdin() {
if (messageLength === null) { if (messageLength === null) {
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) { while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
if (streamEnded) { if (streamEnded) {
await handleStreamEnded( await handleStreamEnded("Unexpected end of stream: headers not found");
"Unexpected end of stream: headers not found",
);
continue main_loop; continue main_loop;
} else if (buffer.length > contentLengthHeaderName.length * 10) { } else if (buffer.length > contentLengthHeaderName.length * 10) {
await handleStreamEnded( await handleStreamEnded(
@ -119,22 +107,16 @@ async function* readStdin() {
} }
await once(process.stdin, "readable"); await once(process.stdin, "readable");
} }
const headers = buffer const headers = buffer.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`)).toString("ascii");
.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`))
.toString("ascii");
const contentLengthHeader = headers const contentLengthHeader = headers
.split(headerSeparator) .split(headerSeparator)
.map((header) => header.split(":")) .map((header) => header.split(":"))
.filter((header) => header[2] === undefined) .filter((header) => header[2] === undefined)
.filter((header) => (header[1] || "").length > 0) .filter((header) => (header[1] || "").length > 0)
.find( .find((header) => (header[0] || "").trim() === contentLengthHeaderName);
(header) => (header[0] || "").trim() === contentLengthHeaderName,
);
const contentLength = (contentLengthHeader || [])[1]; const contentLength = (contentLengthHeader || [])[1];
if (contentLength === undefined) { if (contentLength === undefined) {
await handleStreamEnded( await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`,
);
continue main_loop; continue main_loop;
} }
headersLength = headers.length + headerSeparator.length * 2; headersLength = headers.length + headerSeparator.length * 2;
@ -179,28 +161,20 @@ async function handleMessage(message, prettier) {
if (method === "prettier/format") { if (method === "prettier/format") {
if (params === undefined || params.text === undefined) { if (params === undefined || params.text === undefined) {
throw new Error( throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
`Message params.text is undefined: ${JSON.stringify(message)}`,
);
} }
if (params.options === undefined) { if (params.options === undefined) {
throw new Error( throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
`Message params.options is undefined: ${JSON.stringify(message)}`,
);
} }
let resolvedConfig = {}; let resolvedConfig = {};
if (params.options.filepath) { if (params.options.filepath) {
resolvedConfig = resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
(await prettier.prettier.resolveConfig(params.options.filepath)) || {};
if (params.options.ignorePath) { if (params.options.ignorePath) {
const fileInfo = await prettier.prettier.getFileInfo( const fileInfo = await prettier.prettier.getFileInfo(params.options.filepath, {
params.options.filepath, ignorePath: params.options.ignorePath,
{ });
ignorePath: params.options.ignorePath,
},
);
if (fileInfo.ignored) { if (fileInfo.ignored) {
process.stderr.write( process.stderr.write(
`Ignoring file '${params.options.filepath}' based on rules in '${params.options.ignorePath}'\n`, `Ignoring file '${params.options.filepath}' based on rules in '${params.options.ignorePath}'\n`,
@ -218,8 +192,7 @@ async function handleMessage(message, prettier) {
} }
const plugins = const plugins =
Array.isArray(resolvedConfig?.plugins) && Array.isArray(resolvedConfig?.plugins) && resolvedConfig.plugins.length > 0
resolvedConfig.plugins.length > 0
? resolvedConfig.plugins ? resolvedConfig.plugins
: params.options.plugins; : params.options.plugins;
@ -239,8 +212,7 @@ async function handleMessage(message, prettier) {
sendResponse({ id, result: { text: formattedText } }); sendResponse({ id, result: { text: formattedText } });
} else if (method === "prettier/clear_cache") { } else if (method === "prettier/clear_cache") {
prettier.prettier.clearConfigCache(); prettier.prettier.clearConfigCache();
prettier.config = prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
(await prettier.prettier.resolveConfig(prettier.path)) || {};
sendResponse({ id, result: null }); sendResponse({ id, result: null });
} else if (method === "initialize") { } else if (method === "initialize") {
sendResponse({ sendResponse({
@ -283,9 +255,7 @@ function loadPrettier(prettierPath) {
try { try {
resolve(require(prettierPath)); resolve(require(prettierPath));
} catch (err) { } catch (err) {
reject( reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`,
);
} }
} }
}); });