Skip to content

Commit d75906d

Browse files
committed
doc-print cursor placeholder in comments.printComments()
See #1637 (comment)
1 parent 8f72d51 commit d75906d

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/comments.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const breakParent = docBuilders.breakParent;
88
const indent = docBuilders.indent;
99
const lineSuffix = docBuilders.lineSuffix;
1010
const join = docBuilders.join;
11+
const cursor = docBuilders.cursor;
1112
const util = require("./util");
1213
const childNodesCacheKey = Symbol("child-nodes");
1314
const locStart = util.locStart;
@@ -924,13 +925,20 @@ function printDanglingComments(path, options, sameIndent) {
924925
return indent(concat([hardline, join(hardline, parts)]));
925926
}
926927

928+
function prependCursorPlaceholder(path, options, printed) {
929+
if (path.getNode() === options.cursorNode) {
930+
return concat([cursor, printed]);
931+
}
932+
return printed;
933+
}
934+
927935
function printComments(path, print, options, needsSemi) {
928936
const value = path.getValue();
929937
const printed = print(path);
930938
const comments = value && value.comments;
931939

932940
if (!comments || comments.length === 0) {
933-
return printed;
941+
return prependCursorPlaceholder(path, options, printed);
934942
}
935943

936944
const leadingParts = [];
@@ -957,7 +965,11 @@ function printComments(path, print, options, needsSemi) {
957965
}
958966
}, "comments");
959967

960-
return concat(leadingParts.concat(trailingParts));
968+
return prependCursorPlaceholder(
969+
path,
970+
options,
971+
concat(leadingParts.concat(trailingParts))
972+
);
961973
}
962974

963975
module.exports = {

src/printer.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const ifBreak = docBuilders.ifBreak;
2222
const breakParent = docBuilders.breakParent;
2323
const lineSuffixBoundary = docBuilders.lineSuffixBoundary;
2424
const addAlignmentToDoc = docBuilders.addAlignmentToDoc;
25-
const cursor = docBuilders.cursor;
2625

2726
const docUtils = require("./doc-utils");
2827
const willBreak = docUtils.willBreak;
@@ -4799,18 +4798,12 @@ function printAstToDoc(ast, options, addAlignmentSize) {
47994798
return genericPrint(path, options, printGenerically, args);
48004799
}
48014800

4802-
const withComments = comments.printComments(
4801+
return comments.printComments(
48034802
path,
48044803
p => genericPrint(p, options, printGenerically, args),
48054804
options,
48064805
args && args.needsSemi
48074806
);
4808-
4809-
if (path.getNode() === options.cursorNode) {
4810-
return concat([cursor, withComments]);
4811-
}
4812-
4813-
return withComments;
48144807
}
48154808

48164809
let doc = printGenerically(new FastPath(ast));

0 commit comments

Comments
 (0)