Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
comment,
options
) {
if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) {
return false;
}
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
);
const nextCharacter = text.charAt(nextCharIndex);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
return true;
}
// Comments before `else`/`else if` treat as a dangling comment
if (
precedingNode === enclosingNode.body &&
followingNode === enclosingNode.alternate
) {
addDanglingComment(enclosingNode, comment);
return true;
}
if (followingNode.kind === "if") {
addBlockOrNotComment(followingNode.body, comment);
return true;
}
}
// We unfortunately have no way using the AST or location of nodes to know
// if the comment is positioned before the condition parenthesis:
// while (a /* comment */) {}
// The only workaround I found is to look at the next character to see if
// it is a ).
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
);
const nextCharacter = text.charAt(nextCharIndex);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
return true;
}
if (followingNode.kind === "block") {
addBlockStatementFirstComment(followingNode, comment);
return true;
}
return false;
}
);
// we additionally need to check if this isn't a trailing argument comment,
// by checking the next character isn't ")"
if (
enclosingNode.type &&
commentIsBetweenArgumentsAndBody &&
text.charAt(nextCharIndex) !== ")"
) {
if (options.locEnd(comment) < options.locStart(enclosingNode.type)) {
// we need to store this as a dangling comment in case the type is nullable
// ie function(): ?string {} - the "nullable" attribute is part of the
// function node, not the type.
addDanglingComment(enclosingNode.type, comment);
return true;
}
addTrailingComment(enclosingNode.type, comment);
return true;
}
}
return false;
}
function handleNamespaceComments(
enclosingNode,
precedingNode,
followingNode,
comment
) {
if (
!followingNode &&
!precedingNode &&
enclosingNode &&
enclosingNode.kind === "namespace" &&
!enclosingNode.withBrackets
) {
addTrailingComment(enclosingNode, comment);
return true;
} else if (
!precedingNode &&
enclosingNode &&
enclosingNode.kind === "namespace" &&
!enclosingNode.withBrackets
) {
addDanglingComment(enclosingNode, comment);
return true;
}
return false;
}
function handleGoto(enclosingNode, comment) {
if (enclosingNode && ["label", "goto"].includes(enclosingNode.kind)) {
addTrailingComment(enclosingNode, comment);
return true;
}
return false;
}
function handleReturnComments(
text,
precedingNode,
enclosingNode,
followingNode,
comment
) {
if (enclosingNode && enclosingNode.kind === "return" && !enclosingNode.expr) {
addTrailingComment(enclosingNode, comment);
return true;
}
return false;
}
function handleArrayComments(
text,
precedingNode,
enclosingNode,
followingNode,
comment
) {
if (
!precedingNode &&
!followingNode &&
enclosingNode &&
enclosingNode.kind === "array"
) {
addTrailingComment(enclosingNode, comment);
return true;
}
return false;
}
precedingNode,
enclosingNode,
followingNode,
comment
) {
if (
!enclosingNode ||
!["function", "method", "parameter"].includes(enclosingNode.kind)
) {
return false;
}
if (
precedingNode.kind === "typereference" &&
followingNode.kind === "identifier"
) {
addTrailingComment(precedingNode, comment);
return true;
}
return false;
}