2018-05-01 15:42:59 +03:00
|
|
|
run_spec(__dirname, ["babylon", "typescript", "flow"]);
|
|
|
|
|
2018-05-27 14:36:12 +03:00
|
|
|
const prettier = require("prettier/local");
|
2017-06-02 01:52:29 +03:00
|
|
|
|
|
|
|
test("translates cursor correctly in basic case", () => {
|
2018-05-23 22:55:06 +03:00
|
|
|
expect(
|
|
|
|
prettier.formatWithCursor(" 1", { parser: "babylon", cursorOffset: 2 })
|
|
|
|
).toEqual({
|
2017-06-02 01:52:29 +03:00
|
|
|
formatted: "1;\n",
|
|
|
|
cursorOffset: 1
|
|
|
|
});
|
|
|
|
});
|
2017-06-06 04:51:53 +03:00
|
|
|
|
|
|
|
test("positions cursor relative to closest node, not SourceElement", () => {
|
|
|
|
const code = "return 15";
|
2018-05-23 22:55:06 +03:00
|
|
|
expect(
|
|
|
|
prettier.formatWithCursor(code, { parser: "babylon", cursorOffset: 15 })
|
|
|
|
).toEqual({
|
2017-06-06 04:51:53 +03:00
|
|
|
formatted: "return 15;\n",
|
|
|
|
cursorOffset: 7
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("keeps cursor inside formatted node", () => {
|
|
|
|
const code = "return 15";
|
2018-05-23 22:55:06 +03:00
|
|
|
expect(
|
|
|
|
prettier.formatWithCursor(code, { parser: "babylon", cursorOffset: 14 })
|
|
|
|
).toEqual({
|
2017-06-06 04:51:53 +03:00
|
|
|
formatted: "return 15;\n",
|
2018-05-01 15:42:59 +03:00
|
|
|
cursorOffset: 7
|
2017-06-06 04:51:53 +03:00
|
|
|
});
|
|
|
|
});
|
2017-06-14 19:05:06 +03:00
|
|
|
|
|
|
|
test("doesn't insert second placeholder for nonexistent TypeAnnotation", () => {
|
|
|
|
const code = `
|
|
|
|
foo('bar', cb => {
|
|
|
|
console.log('stuff')
|
|
|
|
})`;
|
2018-05-23 22:55:06 +03:00
|
|
|
expect(
|
|
|
|
prettier.formatWithCursor(code, { parser: "babylon", cursorOffset: 24 })
|
|
|
|
).toEqual({
|
2017-06-14 19:05:06 +03:00
|
|
|
formatted: `foo("bar", cb => {
|
|
|
|
console.log("stuff");
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
cursorOffset: 23
|
|
|
|
});
|
|
|
|
});
|