2022-03-17 21:32:14 +08:00
|
|
|
import { outdent } from "outdent";
|
2022-05-10 15:43:02 +08:00
|
|
|
import getPrettier from "../../../config/get-prettier.js";
|
2018-05-01 13:42:59 +01:00
|
|
|
|
2024-01-19 17:20:24 +08:00
|
|
|
runFormatTest(import.meta, ["babel", "typescript", "flow"]);
|
2017-06-01 18:52:29 -04:00
|
|
|
|
2022-05-10 15:43:02 +08:00
|
|
|
let prettier;
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
prettier = await getPrettier();
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("translates cursor correctly in basic case", async () => {
|
2018-05-23 16:55:06 -03:00
|
|
|
expect(
|
2022-04-29 14:22:24 +08:00
|
|
|
await prettier.formatWithCursor(" 1", { parser: "babel", cursorOffset: 2 }),
|
2021-01-27 12:30:10 +02:00
|
|
|
).toMatchObject({
|
2017-06-01 18:52:29 -04:00
|
|
|
formatted: "1;\n",
|
2020-03-27 23:40:45 +02:00
|
|
|
cursorOffset: 1,
|
2017-06-01 18:52:29 -04:00
|
|
|
});
|
|
|
|
|
});
|
2017-06-05 21:51:53 -04:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("positions cursor relative to closest node, not SourceElement", async () => {
|
2017-06-05 21:51:53 -04:00
|
|
|
const code = "return 15";
|
2018-05-23 16:55:06 -03:00
|
|
|
expect(
|
2022-04-29 14:22:24 +08:00
|
|
|
await prettier.formatWithCursor(code, {
|
|
|
|
|
parser: "babel",
|
|
|
|
|
cursorOffset: 15,
|
|
|
|
|
}),
|
2021-01-27 12:30:10 +02:00
|
|
|
).toMatchObject({
|
2017-06-05 21:51:53 -04:00
|
|
|
formatted: "return 15;\n",
|
2020-03-27 23:40:45 +02:00
|
|
|
cursorOffset: 7,
|
2017-06-05 21:51:53 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("keeps cursor inside formatted node", async () => {
|
2017-06-05 21:51:53 -04:00
|
|
|
const code = "return 15";
|
2018-05-23 16:55:06 -03:00
|
|
|
expect(
|
2022-04-29 14:22:24 +08:00
|
|
|
await prettier.formatWithCursor(code, {
|
|
|
|
|
parser: "babel",
|
|
|
|
|
cursorOffset: 14,
|
|
|
|
|
}),
|
2021-01-27 12:30:10 +02:00
|
|
|
).toMatchObject({
|
2017-06-05 21:51:53 -04:00
|
|
|
formatted: "return 15;\n",
|
2020-03-27 23:40:45 +02:00
|
|
|
cursorOffset: 7,
|
2017-06-05 21:51:53 -04:00
|
|
|
});
|
|
|
|
|
});
|
2017-06-14 12:05:06 -04:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("doesn't insert second placeholder for nonexistent TypeAnnotation", async () => {
|
2022-03-17 20:13:28 +08:00
|
|
|
const code =
|
|
|
|
|
"\n" +
|
|
|
|
|
outdent`
|
|
|
|
|
foo('bar', cb => {
|
|
|
|
|
console.log('stuff')
|
|
|
|
|
})
|
|
|
|
|
`;
|
2018-05-23 16:55:06 -03:00
|
|
|
expect(
|
2022-04-29 14:22:24 +08:00
|
|
|
await prettier.formatWithCursor(code, {
|
|
|
|
|
parser: "babel",
|
|
|
|
|
cursorOffset: 24,
|
|
|
|
|
}),
|
2021-01-27 12:30:10 +02:00
|
|
|
).toMatchObject({
|
2022-03-17 20:13:28 +08:00
|
|
|
formatted:
|
|
|
|
|
outdent`
|
|
|
|
|
foo("bar", (cb) => {
|
|
|
|
|
console.log("stuff");
|
|
|
|
|
});
|
|
|
|
|
` + "\n",
|
2020-03-27 23:40:45 +02:00
|
|
|
cursorOffset: 25,
|
2017-06-14 12:05:06 -04:00
|
|
|
});
|
|
|
|
|
});
|
2020-08-21 09:09:15 +08:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
test("cursorOffset === rangeStart", async () => {
|
2020-08-21 09:09:15 +08:00
|
|
|
const code = "1.0000\n2.0000\n3.0000";
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-04-29 14:22:24 +08:00
|
|
|
await prettier.formatWithCursor(code, {
|
2020-08-21 09:09:15 +08:00
|
|
|
parser: "babel",
|
|
|
|
|
cursorOffset: 7,
|
|
|
|
|
rangeStart: 7,
|
|
|
|
|
rangeEnd: 8,
|
|
|
|
|
}),
|
2021-01-27 12:30:10 +02:00
|
|
|
).toMatchObject({
|
2020-08-21 09:09:15 +08:00
|
|
|
formatted: "1.0000\n2.0;\n3.0000",
|
|
|
|
|
cursorOffset: 7,
|
|
|
|
|
});
|
|
|
|
|
});
|