prettier/tests/cursor/jsfmt.spec.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-12-27 16:05:19 +03:00
run_spec(__dirname, ["babel", "typescript", "flow"]);
const prettier = require("prettier/local");
Add `cursorOffset` option for cursor translation (#1637) * Add `formatWithCursor` API with `cursorOffset` option This addresses https://github.com/prettier/prettier/issues/93 by adding a new option, `cursorOffset`, that tells prettier to determine the location of the cursor after the code has been formatted. This is accessible through the API via a new function, `formatWithCursor`, which returns a `{formatted: string, cursorOffset: ?number}`. Here's a usage example: ```js require("prettier").formatWithCursor(" 1", { cursorOffset: 2 }); // -> { formatted: '1;\n', cursorOffset: 1 } ``` * Add `--cursor-offset` CLI option It will print out the offset instead of the formatted output. This makes it easier to test. For example: echo ' 1' | prettier --stdin --cursor-offset 2 # prints 1 * Add basic test of cursor translation * Document `cursorOffset` option and `formatWithCursor()` * Print translated cursor offset to stderr when --cursor-offset is given This lets us continue to print the formatted code, while also communicating the updated cursor position. See https://github.com/prettier/prettier/pull/1637#discussion_r119735496 * doc-print cursor placeholder in comments.printComments() See https://github.com/prettier/prettier/pull/1637#discussion_r119735149 * Compare array index to -1 instead of >= 0 to determine element presence See https://github.com/prettier/prettier/pull/1637#discussion_r119736623 * Return {formatted, cursor} from printDocToString() instead of mutating options See https://github.com/prettier/prettier/pull/1637#discussion_r119737354
2017-06-02 01:52:29 +03:00
test("translates cursor correctly in basic case", () => {
expect(
2018-12-27 16:05:19 +03:00
prettier.formatWithCursor(" 1", { parser: "babel", cursorOffset: 2 })
).toEqual({
Add `cursorOffset` option for cursor translation (#1637) * Add `formatWithCursor` API with `cursorOffset` option This addresses https://github.com/prettier/prettier/issues/93 by adding a new option, `cursorOffset`, that tells prettier to determine the location of the cursor after the code has been formatted. This is accessible through the API via a new function, `formatWithCursor`, which returns a `{formatted: string, cursorOffset: ?number}`. Here's a usage example: ```js require("prettier").formatWithCursor(" 1", { cursorOffset: 2 }); // -> { formatted: '1;\n', cursorOffset: 1 } ``` * Add `--cursor-offset` CLI option It will print out the offset instead of the formatted output. This makes it easier to test. For example: echo ' 1' | prettier --stdin --cursor-offset 2 # prints 1 * Add basic test of cursor translation * Document `cursorOffset` option and `formatWithCursor()` * Print translated cursor offset to stderr when --cursor-offset is given This lets us continue to print the formatted code, while also communicating the updated cursor position. See https://github.com/prettier/prettier/pull/1637#discussion_r119735496 * doc-print cursor placeholder in comments.printComments() See https://github.com/prettier/prettier/pull/1637#discussion_r119735149 * Compare array index to -1 instead of >= 0 to determine element presence See https://github.com/prettier/prettier/pull/1637#discussion_r119736623 * Return {formatted, cursor} from printDocToString() instead of mutating options See https://github.com/prettier/prettier/pull/1637#discussion_r119737354
2017-06-02 01:52:29 +03:00
formatted: "1;\n",
cursorOffset: 1
});
});
test("positions cursor relative to closest node, not SourceElement", () => {
const code = "return 15";
expect(
2018-12-27 16:05:19 +03:00
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 15 })
).toEqual({
formatted: "return 15;\n",
cursorOffset: 7
});
});
test("keeps cursor inside formatted node", () => {
const code = "return 15";
expect(
2018-12-27 16:05:19 +03:00
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 14 })
).toEqual({
formatted: "return 15;\n",
cursorOffset: 7
});
});
test("doesn't insert second placeholder for nonexistent TypeAnnotation", () => {
const code = `
foo('bar', cb => {
console.log('stuff')
})`;
expect(
2018-12-27 16:05:19 +03:00
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 24 })
).toEqual({
formatted: `foo("bar", cb => {
console.log("stuff");
});
`,
cursorOffset: 23
});
});