2021-12-02 10:19:32 +08:00
|
|
|
import { outdent } from "outdent";
|
2022-07-20 23:18:24 +08:00
|
|
|
import prettier from "../../config/prettier-entry.js";
|
2020-08-21 09:43:24 +08:00
|
|
|
|
|
|
|
|
const {
|
2021-01-29 21:48:34 +02:00
|
|
|
__debug: { parse, formatAST, formatDoc, printToDoc, printDocToString },
|
2023-03-07 08:54:11 +08:00
|
|
|
doc: { builders },
|
2021-12-02 10:19:32 +08:00
|
|
|
} = prettier;
|
2020-08-21 09:43:24 +08:00
|
|
|
|
|
|
|
|
const code = outdent`
|
|
|
|
|
const foo = 'bar'
|
|
|
|
|
`;
|
|
|
|
|
const formatted = 'const foo = "bar";\n';
|
|
|
|
|
const options = { parser: "babel", originalText: code };
|
|
|
|
|
|
|
|
|
|
describe("API", () => {
|
2022-05-04 11:15:48 +08:00
|
|
|
let ast;
|
|
|
|
|
let doc;
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
({ ast } = await parse(code, options));
|
|
|
|
|
doc = await printToDoc(code, options);
|
|
|
|
|
});
|
|
|
|
|
|
2020-08-21 09:43:24 +08:00
|
|
|
test("prettier.parse", () => {
|
|
|
|
|
expect(Array.isArray(ast.program.body)).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-06 09:27:54 +08:00
|
|
|
test("prettier.formatAST", async () => {
|
|
|
|
|
const { formatted: formatResultFromAST } = await formatAST(ast, options);
|
2020-08-21 09:43:24 +08:00
|
|
|
expect(formatResultFromAST).toBe(formatted);
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-06 09:27:54 +08:00
|
|
|
test("prettier.printDocToString", async () => {
|
|
|
|
|
const { formatted: stringFromDoc } = await printDocToString(doc, options);
|
2020-08-21 09:43:24 +08:00
|
|
|
expect(stringFromDoc).toBe(formatted);
|
|
|
|
|
});
|
2021-01-29 21:48:34 +02:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("prettier.formatDoc", async () => {
|
|
|
|
|
const formatResultFromDoc = await formatDoc(doc, options);
|
|
|
|
|
expect(formatResultFromDoc).toMatchSnapshot();
|
|
|
|
|
|
|
|
|
|
const doc2 = new Function(
|
|
|
|
|
`{ ${Object.keys(builders)} }`,
|
|
|
|
|
`return ${formatResultFromDoc}`,
|
|
|
|
|
)(builders);
|
|
|
|
|
|
2022-05-06 09:27:54 +08:00
|
|
|
const { formatted: stringFromDoc2 } = await printDocToString(doc2, options);
|
2021-01-29 21:48:34 +02:00
|
|
|
expect(stringFromDoc2).toBe(formatted);
|
2022-04-29 14:22:24 +08:00
|
|
|
|
|
|
|
|
const formatResultFromDoc2 = await formatDoc(doc2, options);
|
2021-01-29 21:48:34 +02:00
|
|
|
expect(formatResultFromDoc2).toBe(formatResultFromDoc);
|
|
|
|
|
});
|
2021-01-30 18:25:43 +02:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("prettier.formatDoc prints things as expected", async () => {
|
2021-02-05 01:03:40 +02:00
|
|
|
const {
|
|
|
|
|
indent,
|
|
|
|
|
hardline,
|
|
|
|
|
literalline,
|
|
|
|
|
fill,
|
2021-02-28 20:52:57 +02:00
|
|
|
ifBreak,
|
2021-02-05 01:03:40 +02:00
|
|
|
indentIfBreak,
|
|
|
|
|
group,
|
|
|
|
|
line,
|
2021-02-10 17:08:54 +02:00
|
|
|
label,
|
2021-02-05 01:03:40 +02:00
|
|
|
} = builders;
|
2021-01-30 18:25:43 +02:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
expect(await formatDoc([indent(hardline), indent(literalline)])).toBe(
|
2021-01-30 18:25:43 +02:00
|
|
|
"[indent(hardline), indent(literalline)]",
|
|
|
|
|
);
|
|
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
expect(
|
|
|
|
|
await formatDoc(fill(["foo", hardline, "bar", literalline, "baz"])),
|
|
|
|
|
).toBe('fill(["foo", hardline, "bar", literalline, "baz"])');
|
2021-01-30 18:25:43 +02:00
|
|
|
|
2021-02-05 01:03:40 +02:00
|
|
|
expect(
|
2022-04-29 14:22:24 +08:00
|
|
|
await formatDoc(indentIfBreak(group(["1", line, "2"]), { groupId: "Q" })),
|
2021-02-05 01:03:40 +02:00
|
|
|
).toBe('indentIfBreak(group(["1", line, "2"]), { groupId: "Q" })');
|
2021-02-10 17:08:54 +02:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
expect(await formatDoc(label("foo", group(["1", line, "2"])))).toBe(
|
2021-02-10 17:08:54 +02:00
|
|
|
'label("foo", group(["1", line, "2"]))',
|
|
|
|
|
);
|
2021-02-28 20:52:57 +02:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
expect(
|
|
|
|
|
await formatDoc([ifBreak("a", "b"), ifBreak("a"), ifBreak("", "b")]),
|
|
|
|
|
).toBe('[ifBreak("a", "b"), ifBreak("a"), ifBreak("", "b")]');
|
2021-01-30 18:25:43 +02:00
|
|
|
});
|
2020-08-21 09:43:24 +08:00
|
|
|
});
|