Skip to content

Commit 8f72d51

Browse files
committed
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 #1637 (comment)
1 parent 9502fcf commit 8f72d51

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

bin/prettier.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ if (argv["help"] || (!filepatterns.length && !stdin)) {
249249
" --trailing-comma <none|es5|all>\n" +
250250
" Print trailing commas wherever possible. Defaults to none.\n" +
251251
" --parser <flow|babylon> Specify which parse to use. Defaults to babylon.\n" +
252-
" --cursor-offset <int> Instead of formatting the code, print where a cursor at the given position would move to after formatting.\n" +
252+
" --cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.\n" +
253253
" This option cannot be used with --range-start and --range-end\n" +
254254
" --range-start <int> Format code starting at a given character offset.\n" +
255255
" The range will extend backwards to the start of the first line containing the selected statement.\n" +
@@ -364,11 +364,11 @@ if (stdin) {
364364
}
365365

366366
function writeOutput(result) {
367+
// Don't use `console.log` here since it adds an extra newline at the end.
368+
process.stdout.write(result.formatted);
369+
367370
if (options.cursorOffset) {
368-
console.log(result.cursorOffset);
369-
} else {
370-
// Don't use `console.log` here since it adds an extra newline at the end.
371-
process.stdout.write(result.formatted);
371+
process.stderr.write(result.cursorOffset + "\n");
372372
}
373373
}
374374

0 commit comments

Comments
 (0)