From 48bbd9cb6b9789b5a1f5b9814be82ba07b44d079 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Wed, 14 Jun 2017 12:05:06 -0400 Subject: [PATCH] Don't insert a cursor placeholder for a value-less path (#2136) * Test that no cursor placeholder is inserted for a value-less path This was taken from https://github.com/prettier/prettier/issues/2131 * Don't insert a cursor placeholder for a value-less path This fixes https://github.com/prettier/prettier/issues/2131 --- src/comments.js | 2 +- tests/cursor/jsfmt.spec.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/comments.js b/src/comments.js index 515ed12d..d2b25707 100644 --- a/src/comments.js +++ b/src/comments.js @@ -934,7 +934,7 @@ function printDanglingComments(path, options, sameIndent) { } function prependCursorPlaceholder(path, options, printed) { - if (path.getNode() === options.cursorNode) { + if (path.getNode() === options.cursorNode && path.getValue()) { return concat([cursor, printed]); } return printed; diff --git a/tests/cursor/jsfmt.spec.js b/tests/cursor/jsfmt.spec.js index e5a0dc39..e92b0d4e 100644 --- a/tests/cursor/jsfmt.spec.js +++ b/tests/cursor/jsfmt.spec.js @@ -22,3 +22,17 @@ test("keeps cursor inside formatted node", () => { cursorOffset: 14 // TODO fix this }); }); + +test("doesn't insert second placeholder for nonexistent TypeAnnotation", () => { + const code = ` +foo('bar', cb => { + console.log('stuff') +})`; + expect(prettier.formatWithCursor(code, { cursorOffset: 24 })).toEqual({ + formatted: `foo("bar", cb => { + console.log("stuff"); +}); +`, + cursorOffset: 23 + }); +});