Skip to content

Commit

Permalink
Add __debug apis to cjs version (#13731)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 26, 2022
1 parent b722335 commit 106c009
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,29 @@ const functionNames = [

const prettier = Object.create(null);
for (const name of functionNames) {
prettier[name] = function (...args) {
return prettierPromise.then((prettier) => prettier[name](...args));
prettier[name] = async (...args) => {
const prettier = await prettierPromise;
return prettier[name](...args);
};
}

const debugApiFunctionNames = [
"parse",
"formatAST",
"formatDoc",
"printToDoc",
"printDocToString",
];

const debugApis = Object.create(null);
for (const name of debugApiFunctionNames) {
debugApis[name] = async (...args) => {
const prettier = await prettierPromise;
return prettier.__debug[name](...args);
};
}
prettier.__debug = debugApis;

if (process.env.NODE_ENV === "production") {
prettier.util = require("./common/util-shared.js");
prettier.doc = require("./document/index.js");
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/__tests__/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "node:path";
import createEsmUtils from "esm-utils";
import fastGlob from "fast-glob";
import { projectRoot } from "../env.js";
import prettier from "../../config/prettier-entry.js";
import createSandBox from "../../config/utils/create-sandbox.cjs";
import * as coreOptions from "../../../src/main/core-options.js";
import codeSamples from "../../../website/playground/codeSamples.mjs";
Expand Down Expand Up @@ -95,3 +96,34 @@ test("global objects", async () => {
expect(globalObjects).toStrictEqual({});
}
});

test("Commonjs version", () => {
const prettierCommonjsVersion = require(path.join(
distDirectory,
"index.cjs"
));

expect(Object.keys(prettierCommonjsVersion).sort()).toEqual(
Object.keys(prettier)
.filter((key) => key !== "__internal")
.sort()
);
expect(typeof prettierCommonjsVersion.format).toBe("function");

expect(Object.keys(prettierCommonjsVersion.doc)).toEqual(
Object.keys(prettier.doc)
);
expect(typeof prettierCommonjsVersion.doc.builders.fill).toBe("function");

expect(Object.keys(prettierCommonjsVersion.util)).toEqual(
Object.keys(prettier.util)
);
expect(typeof prettierCommonjsVersion.util.getStringWidth).toBe("function");

expect(Object.keys(prettierCommonjsVersion.__debug).sort()).toEqual(
Object.keys(prettier.__debug).sort()
);
expect(typeof prettierCommonjsVersion.__debug.parse).toBe("function");

expect(prettierCommonjsVersion.version).toBe(prettier.version);
});

0 comments on commit 106c009

Please sign in to comment.