Skip to content

Commit

Permalink
Make functions like prettier.{__debug,doc,util} functional
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 23, 2024
1 parent 1520df8 commit 596a127
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"make-synchronized": "^0.2.1"
"make-synchronized": "^0.2.5"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ synchronizedPrettier.format("foo( )", { parser: "babel" });
// => 'foo();\n'
```

This package is a simple wrapper of [`make-synchronized`](https://github.com/fisker/make-synchronized), currently only the functions and primitive values exported from `prettier` is functional, functions not exported directly (eg: `prettier.__debug.parse`) doesn't work, but it can be supported, if you want more functionality, please [open an issue](https://github.com/prettier/prettier-synchronized/issues/new).
This package is a simple wrapper of [`make-synchronized`](https://github.com/fisker/make-synchronized).

For more complex use cases, it more reasonable to extract into a separate file, and run with [`make-synchronized`](https://github.com/fisker/make-synchronized), example

Expand Down
27 changes: 23 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import prettier from "prettier";
import synchronizedPrettier, { createSynchronizedPrettier } from "./index.cjs";
import fakePrettier from "./a-fake-prettier-to-test.cjs";

const code = await fs.readFile("./index.cjs", "utf8");

test("format", async () => {
const code = await fs.readFile("./index.cjs", "utf8");
const formatOptions = { parser: "meriyah" };
assert.equal(
synchronizedPrettier.format(code, formatOptions),
await prettier.format(code, formatOptions),
const formattedBySynchronizedPrettier = synchronizedPrettier.format(
code,
formatOptions,
);
assert.equal(typeof formattedBySynchronizedPrettier, "string");

const formattedByPrettier = await prettier.format(code, formatOptions);
assert.equal(formattedBySynchronizedPrettier, formattedByPrettier);

let error;
try {
Expand All @@ -27,6 +32,20 @@ test("version", () => {
assert.equal(synchronizedPrettier.version, prettier.version);
});

test("functions not exported directly", async () => {
const parseOptions = { parser: "meriyah" };
const { ast: astParsedBySynchronizedPrettier } =
synchronizedPrettier.__debug.parse(code, parseOptions);

assert.equal(ast.type, "Program");

const { ast: astParsedByPrettier } = await prettier.__debug.parse(
code,
parseOptions,
);
assert.deepEqual(ast, astParsedByPrettier);
});

{
const fakePrettierRelatedPath = "./a-fake-prettier-to-test.cjs";
const fakePrettierUrl = new URL(fakePrettierRelatedPath, import.meta.url);
Expand Down

0 comments on commit 596a127

Please sign in to comment.