Skip to content

Commit 1fa6b4e

Browse files
committed
Return {formatted, cursor} from printDocToString() instead of mutating options
See #1637 (comment)
1 parent c4e08d6 commit 1fa6b4e

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ function formatWithCursor(text, opts, addAlignmentSize) {
7676
const astComments = attachComments(text, ast, opts);
7777
const doc = printAstToDoc(ast, opts, addAlignmentSize);
7878
opts.newLine = guessLineEnding(text);
79-
const str = printDocToString(doc, opts);
79+
const toStringResult = printDocToString(doc, opts);
80+
const str = toStringResult.formatted;
81+
const cursorOffsetResult = toStringResult.cursor;
8082
ensureAllCommentsPrinted(astComments);
8183
// Remove extra leading indentation as well as the added indentation after last newline
8284
if (addAlignmentSize > 0) {
@@ -86,7 +88,7 @@ function formatWithCursor(text, opts, addAlignmentSize) {
8688
if (cursorOffset !== undefined) {
8789
return {
8890
formatted: str,
89-
cursorOffset: opts.cursorOffsetResult + cursorOffset
91+
cursorOffset: cursorOffsetResult + cursorOffset
9092
};
9193
}
9294

src/doc-printer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,15 @@ function printDocToString(doc, options) {
420420
const cursorPlaceholderIndex = out.indexOf(cursor.placeholder);
421421
if (cursorPlaceholderIndex !== -1) {
422422
const beforeCursor = out.slice(0, cursorPlaceholderIndex).join("");
423-
options.cursorOffsetResult = beforeCursor.length;
424423
const afterCursor = out.slice(cursorPlaceholderIndex + 1).join("");
425424

426-
return beforeCursor + afterCursor;
425+
return {
426+
formatted: beforeCursor + afterCursor,
427+
cursor: beforeCursor.length
428+
};
427429
}
428430

429-
return out.join("");
431+
return { formatted: out.join("") };
430432
}
431433

432434
module.exports = { printDocToString };

0 commit comments

Comments
 (0)