How to use the prettier.addTrailingComment function in prettier

To help you get started, we’ve selected a few prettier examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github prettier / plugin-php / src / comments.js View on Github external
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;
  }
github prettier / plugin-php / src / comments.js View on Github external
}
  // 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;
}
github prettier / plugin-php / src / comments.js View on Github external
);
    // 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;
}
github prettier / plugin-php / src / comments.js View on Github external
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;
}
github prettier / plugin-php / src / comments.js View on Github external
function handleGoto(enclosingNode, comment) {
  if (enclosingNode && ["label", "goto"].includes(enclosingNode.kind)) {
    addTrailingComment(enclosingNode, comment);
    return true;
  }
  return false;
}
github prettier / plugin-php / src / comments.js View on Github external
function handleReturnComments(
  text,
  precedingNode,
  enclosingNode,
  followingNode,
  comment
) {
  if (enclosingNode && enclosingNode.kind === "return" && !enclosingNode.expr) {
    addTrailingComment(enclosingNode, comment);
    return true;
  }

  return false;
}
github prettier / plugin-php / src / comments.js View on Github external
function handleArrayComments(
  text,
  precedingNode,
  enclosingNode,
  followingNode,
  comment
) {
  if (
    !precedingNode &&
    !followingNode &&
    enclosingNode &&
    enclosingNode.kind === "array"
  ) {
    addTrailingComment(enclosingNode, comment);
    return true;
  }

  return false;
}
github prettier / plugin-php / src / comments.js View on Github external
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;
}