Files
test-repo/tests/format/js/cursor/jsfmt.spec.js
T
2022-03-17 21:34:49 +08:00

73 lines
1.7 KiB
JavaScript

import { outdent } from "outdent";
// TODO: Use this when we drop support for Node.js 12
// import prettier from "../../../config/prettier-entry.js";
const { prettier } = run_spec;
run_spec(import.meta, ["babel", "typescript", "flow"]);
test("translates cursor correctly in basic case", () => {
expect(
prettier.formatWithCursor(" 1", { parser: "babel", cursorOffset: 2 })
).toMatchObject({
formatted: "1;\n",
cursorOffset: 1,
});
});
test("positions cursor relative to closest node, not SourceElement", () => {
const code = "return 15";
expect(
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 15 })
).toMatchObject({
formatted: "return 15;\n",
cursorOffset: 7,
});
});
test("keeps cursor inside formatted node", () => {
const code = "return 15";
expect(
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 14 })
).toMatchObject({
formatted: "return 15;\n",
cursorOffset: 7,
});
});
test("doesn't insert second placeholder for nonexistent TypeAnnotation", () => {
const code =
"\n" +
outdent`
foo('bar', cb => {
console.log('stuff')
})
`;
expect(
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 24 })
).toMatchObject({
formatted:
outdent`
foo("bar", (cb) => {
console.log("stuff");
});
` + "\n",
cursorOffset: 25,
});
});
test("cursorOffset === rangeStart", () => {
const code = "1.0000\n2.0000\n3.0000";
expect(
prettier.formatWithCursor(code, {
parser: "babel",
cursorOffset: 7,
rangeStart: 7,
rangeEnd: 8,
})
).toMatchObject({
formatted: "1.0000\n2.0;\n3.0000",
cursorOffset: 7,
});
});