Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include details about prettier process in info #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions prettier-el.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -253,15 +261,20 @@ 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"));

// 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.
Expand All @@ -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;
}

/**
Expand All @@ -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)) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 }) || {};
Expand All @@ -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(
Expand Down