Minor refactoring of printComments (#10671)

This commit is contained in:
fisker Cheung authored and GitHub committed 2021-04-12 18:02:06 +08:00
1 parent 22f9256faa
commit 62b9c63f21
1 file changed
+40 -56
+40 -56
View File
@@ -419,10 +419,10 @@ function breakTies(tiesToBreak, text, options) {
tiesToBreak.length = 0; tiesToBreak.length = 0;
} }
function printComment(commentPath, options) { function printComment(path, options) {
const comment = commentPath.getValue(); const comment = path.getValue();
comment.printed = true; comment.printed = true;
return options.printer.printComment(commentPath, options); return options.printer.printComment(path, options);
} }
function findExpressionIndexForComment(quasis, comment, options) { function findExpressionIndexForComment(quasis, comment, options) {
@@ -440,40 +440,45 @@ function findExpressionIndexForComment(quasis, comment, options) {
return 0; return 0;
} }
function printLeadingComment(commentPath, options) { function printLeadingComment(path, options) {
const comment = commentPath.getValue(); const comment = path.getValue();
const contents = printComment(commentPath, options); const parts = [printComment(path, options)];
/* istanbul ignore next */
if (!contents) { const { printer, originalText, locStart, locEnd } = options;
return ""; const isBlock = printer.isBlockComment && printer.isBlockComment(comment);
}
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);
// Leading block comments should see if they need to stay on the // Leading block comments should see if they need to stay on the
// same line or not. // same line or not.
if (isBlock) { if (isBlock) {
const lineBreak = hasNewline(options.originalText, options.locEnd(comment)) const lineBreak = hasNewline(originalText, locEnd(comment))
? hasNewline(options.originalText, options.locStart(comment), { ? hasNewline(originalText, locStart(comment), {
backwards: true, backwards: true,
}) })
? hardline ? hardline
: line : line
: " "; : " ";
return [contents, lineBreak]; parts.push(lineBreak);
} else {
parts.push(hardline);
} }
return [contents, hardline]; const index = skipNewline(
originalText,
skipSpaces(originalText, locEnd(comment))
);
if (index !== false && hasNewline(originalText, index)) {
parts.push(hardline);
}
return parts;
} }
function printTrailingComment(commentPath, options) { function printTrailingComment(path, options) {
const comment = commentPath.getValue(); const comment = path.getValue();
const contents = printComment(commentPath, options); const printed = printComment(path, options);
/* istanbul ignore next */
if (!contents) {
return "";
}
const { printer, originalText, locStart } = options; const { printer, originalText, locStart } = options;
const isBlock = printer.isBlockComment && printer.isBlockComment(comment); const isBlock = printer.isBlockComment && printer.isBlockComment(comment);
@@ -496,17 +501,17 @@ function printTrailingComment(commentPath, options) {
locStart locStart
); );
return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]); return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", printed]);
} }
let printed = [" ", contents]; let parts = [" ", printed];
// Trailing block comments never need a newline // Trailing block comments never need a newline
if (!isBlock) { if (!isBlock) {
printed = [lineSuffix(printed), breakParent]; parts = [lineSuffix(parts), breakParent];
} }
return printed; return parts;
} }
function printDanglingComments(path, options, sameIndent, filter) { function printDanglingComments(path, options, sameIndent, filter) {
@@ -517,15 +522,10 @@ function printDanglingComments(path, options, sameIndent, filter) {
return ""; return "";
} }
path.each((commentPath) => { path.each(() => {
const comment = commentPath.getValue(); const comment = path.getValue();
if ( if (!comment.leading && !comment.trailing && (!filter || filter(comment))) {
comment && parts.push(printComment(path, options));
!comment.leading &&
!comment.trailing &&
(!filter || filter(comment))
) {
parts.push(printComment(commentPath, options));
} }
}, "comments"); }, "comments");
@@ -558,33 +558,17 @@ function printCommentsSeparately(path, options, ignored) {
const leadingParts = []; const leadingParts = [];
const trailingParts = []; const trailingParts = [];
path.each(() => {
path.each((commentPath) => { const comment = path.getValue();
const comment = commentPath.getValue();
if (ignored && ignored.has(comment)) { if (ignored && ignored.has(comment)) {
return; return;
} }
const { leading, trailing } = comment; const { leading, trailing } = comment;
if (leading) { if (leading) {
const contents = printLeadingComment(commentPath, options); leadingParts.push(printLeadingComment(path, options));
/* istanbul ignore next */
if (!contents) {
return;
}
leadingParts.push(contents);
const text = options.originalText;
const index = skipNewline(
text,
skipSpaces(text, options.locEnd(comment))
);
if (index !== false && hasNewline(text, index)) {
leadingParts.push(hardline);
}
} else if (trailing) { } else if (trailing) {
trailingParts.push(printTrailingComment(commentPath, options)); trailingParts.push(printTrailingComment(path, options));
} }
}, "comments"); }, "comments");