From 7152a2032ae5145c4dca28079cdb73da4af103b3 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Thu, 14 Oct 2021 20:17:11 +1300 Subject: [PATCH] Include details about prettier process in info --- prettier-el.js | 54 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/prettier-el.js b/prettier-el.js index 308ac35..efc4922 100644 --- a/prettier-el.js +++ b/prettier-el.js @@ -49,7 +49,10 @@ const ignoreParser = "ignored"; const syncBeacon = Buffer.from("#prettier.el-sync#\n"); -/** @type{PrettierAPI} */ +/** @typedef {{ prettier: PrettierAPI, context: string, required: string, loadedAt: number }} */ +let PrettierMeta; + +/** @type{PrettierMeta} */ let globalPrettier; /** @@ -124,7 +127,7 @@ function makeU32(val) { * Find a globally installed Prettier or error if not found. Memoize results for * future lookups. * - * @return {!PrettierAPI | !Error} + * @return {!PrettierMeta | !Error} */ function getGlobalPrettier() { if (globalPrettier) { @@ -165,7 +168,12 @@ function getGlobalPrettier() { for (let i = 0; i < pathOptions.length; ++i) { if (pathOptions[i]) { try { - return globalRequire(pathOptions[i]); + return { + prettier: globalRequire(pathOptions[i]), + context: "/", + required: pathOptions[i], + loadedAt: Date.now(), + }; } catch (e) { if ( !(e instanceof Error) || @@ -253,7 +261,7 @@ function tryRequirePrettier(targetRequire) { * Find locally installed Prettier, or null if not found. * * @param {!string} directory The directory for which to find a local Prettier installation. - * @return {PrettierAPI} The Prettier package if found, or null if not found. + * @return {?PrettierMeta} The Prettier package if found, or null if not found. */ function getLocalPrettier(directory) { const targetRequire = createRequire(path["join"](directory, "package.json")); @@ -261,7 +269,12 @@ function getLocalPrettier(directory) { // Try loading prettier for non-PnP packages and return it if found. const prettier = tryRequirePrettier(targetRequire); if (prettier) { - return prettier; + return { + prettier, + context: directory, + required: "prettier", + loadedAt: Date.now(), + }; } // Try finding .pnp.[c]js and bail out if we can't find it. @@ -272,7 +285,17 @@ function getLocalPrettier(directory) { // Setup PnP API and retry loading prettier. targetRequire(pnpJs)["setup"](); - return tryRequirePrettier(targetRequire); + const pnpPrettier = tryRequirePrettier(targetRequire); + if (pnpPrettier) { + return { + prettier: pnpPrettier, + context: directory, + required: "prettier", + loadedAt: Date.now(), + }; + } + + return null; } /** @@ -283,7 +306,7 @@ function getLocalPrettier(directory) { * @param {!string} directory The directory for which to find the Prettier * package. * - * @return {!PrettierAPI | !Error} + * @return {!PrettierMeta | !Error} */ function getPrettierForDirectory(directory) { if (prettierCache.has(directory)) { @@ -319,7 +342,7 @@ function getPrettierForDirectory(directory) { * * @param {!string} filepath * - * @return {!PrettierAPI} The Prettier package found. + * @return {!PrettierMeta} The Prettier package found. */ function getPrettierForPath(filepath) { const result = path["isAbsolute"](filepath) @@ -415,7 +438,7 @@ function bestParser(prettier, parsers, options, filepath, inferParser) { const editorconfig = packet[1] === "E".charCodeAt(0); const filepath = packet.toString("utf-8", 2, newlineIndex1); - const prettier = getPrettierForPath(filepath); + const { prettier } = getPrettierForPath(filepath); if (filepath.length > 0) { prettier.resolveConfig.sync(filepath, { editorconfig, @@ -458,7 +481,7 @@ function bestParser(prettier, parsers, options, filepath, inferParser) { const body = fs["readFileSync"](filename, "utf8"); try { - const prettier = getPrettierForPath(filepath); + const { prettier } = getPrettierForPath(filepath); const timeBeforeFormat = Date.now(); @@ -582,7 +605,9 @@ function bestParser(prettier, parsers, options, filepath, inferParser) { ); const parsers = parseParsers(parsersString); - const prettier = getPrettierForPath(filepath); + const { prettier, context, required, loadedAt } = getPrettierForPath( + filepath + ); const options = prettier.resolveConfig.sync(filepath, { editorconfig }) || {}; @@ -591,7 +616,12 @@ function bestParser(prettier, parsers, options, filepath, inferParser) { options["parser"] = function (_text, _parsers, options) { optionsStr = JSON.stringify({ ["versions"]: Object.assign({}, process["versions"], { - ["prettier"]: prettier.version, + ["prettier"]: { + version: prettier.version, + context, + required, + loadedAt, + }, }), ["options"]: options, ["bestParser"]: bestParser(