diff --git a/package.json b/package.json index ed15282..5003d4a 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,6 @@ "registry": "https://registry.npmjs.org/" }, "dependencies": { - "make-synchronized": "^0.2.1" + "make-synchronized": "^0.2.5" } } diff --git a/readme.md b/readme.md index e93a596..dba10dd 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index ed72a85..00c2f22 100644 --- a/test.js +++ b/test.js @@ -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 { @@ -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);