diff --git a/src/document/printer.js b/src/document/printer.js index 6a9b09b77..7e5872380 100644 --- a/src/document/printer.js +++ b/src/document/printer.js @@ -294,6 +294,7 @@ function printDocToString(doc, options) { let shouldRemeasure = false; /** @type Command[] */ const lineSuffix = []; + let printedCursorCount = 0; while (cmds.length > 0) { const { ind, mode, doc } = cmds.pop(); @@ -313,7 +314,11 @@ function printDocToString(doc, options) { } case DOC_TYPE_CURSOR: + if (printedCursorCount >= 2) { + throw new Error("There are too many 'cursor' in doc."); + } out.push(CURSOR_PLACEHOLDER); + printedCursorCount++; break; case DOC_TYPE_INDENT: diff --git a/tests/unit/print-doc-to-string.js b/tests/unit/print-doc-to-string.js new file mode 100644 index 000000000..7f63be5b2 --- /dev/null +++ b/tests/unit/print-doc-to-string.js @@ -0,0 +1,8 @@ +import { cursor } from "../../src/document/builders.js"; +import { printDocToString } from "../../src/document/printer.js"; + +test("Should reject if too many cursor in doc", () => { + expect(() => { + printDocToString([cursor, cursor, cursor], { printWidth: 80, tabWidth: 2 }); + }).toThrow({ message: "There are too many 'cursor' in doc." }); +});