Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function printTrailingComment(commentPath, print, options) {
const comment = commentPath.getValue();
const contents = printComment(commentPath, options);
if (!contents) {
return "";
}
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);
if (
hasNewline(options.originalText, options.locStart(comment), {
backwards: true,
})
) {
// This allows comments at the end of nested structures:
// {
// x: 1,
// y: 2
// // A comment
// }
// Those kinds of comments are almost always leading comments, but
// here it doesn't go "outside" the block and turns it into a
// trailing comment for `2`. We can simulate the above by checking
// if this a comment on its own line; normal trailing comments are
// always at the end of another expression.
const isLineBeforeEmpty = isPreviousLineEmpty(
const comment = commentPath.getValue();
const contents = printComment(commentPath, options);
if (!contents) {
return "";
}
const isBlock =
options.printer.isBlockComment && options.printer.isBlockComment(comment);
// Leading block comments should see if they need to stay on the
// same line or not.
if (isBlock) {
return concat([
contents,
hasNewline(options.originalText, options.locEnd(comment))
? hardline
: " ",
]);
}
return concat([contents, hardline]);
}
path.each((commentPath) => {
const comment = commentPath.getValue();
const { leading, trailing } = comment;
if (leading) {
const contents = printLeadingComment(commentPath, print, options);
if (!contents) {
return;
}
leadingParts.push(contents);
const text = options.originalText;
if (hasNewline(text, skipNewline(text, options.locEnd(comment)))) {
leadingParts.push(hardline);
}
} else if (trailing) {
trailingParts.push(printTrailingComment(commentPath, print, options));
}
}, "comments");
function isStringOnItsOwnLine(node, text, options) {
return (
(node.kind === "string" ||
(node.kind === "encapsed" &&
(node.type === "string" || node.type === "shell"))) &&
stringHasNewLines(node) &&
!hasNewline(text, options.locStart(node), { backwards: true })
);
}
(comment) => comment.leading && hasNewline(text, options.locEnd(comment))
)
function isNextLineEmptyAfterNamespace(text, node, locStart) {
let idx = locStart(node);
idx = skipEverythingButNewLine(text, idx);
idx = skipNewline(text, idx);
return hasNewline(text, idx);
}