Files
test-repo/tests/cursor/jsfmt.spec.js
T
Joseph Frazier c863cbeac8 Translate cursor relative to nearest node, not SourceElement (#1989)
* Add test demonstrating SourceElement-relative cursor translation

See https://github.com/prettier/prettier/issues/1981#issuecomment-306326739

* Translate cursor relative to nearest node, not SourceElement

This partially address https://github.com/prettier/prettier/issues/1981

See https://github.com/prettier/prettier/issues/1981#issuecomment-306326739

* Add test demonstrating incorrect cursor translation

Since the `cursorOffset` option (introduced in #1637) works by tracking
the cursor position relative to an AST node (rather than a CST token),
it can produce incorrect results.

See https://github.com/prettier/prettier/issues/1981
2017-06-05 18:51:53 -07:00

25 lines
710 B
JavaScript

const prettier = require("../..");
test("translates cursor correctly in basic case", () => {
expect(prettier.formatWithCursor(" 1", { cursorOffset: 2 })).toEqual({
formatted: "1;\n",
cursorOffset: 1
});
});
test("positions cursor relative to closest node, not SourceElement", () => {
const code = "return 15";
expect(prettier.formatWithCursor(code, { cursorOffset: 15 })).toEqual({
formatted: "return 15;\n",
cursorOffset: 7
});
});
test("keeps cursor inside formatted node", () => {
const code = "return 15";
expect(prettier.formatWithCursor(code, { cursorOffset: 14 })).toEqual({
formatted: "return 15;\n",
cursorOffset: 14 // TODO fix this
});
});