How to use the prettier.addDanglingComment 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
text,
      comment,
      options
    );
    // 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
if (
        enclosingNode.implements.some((implementsNode) => {
          if (followingNode && followingNode === implementsNode) {
            addDanglingComment(followingNode, comment);
            return true;
          }
        })
      ) {
        return true;
      }
    }

    // For an empty class where the body is only made up of comments, we
    // need to attach this as a dangling comment on the class node itself
    if (!(enclosingNode.body && enclosingNode.body.length > 0)) {
      addDanglingComment(enclosingNode, comment);
      return true;
    }
  }
  return false;
}
github prettier / plugin-php / src / comments.js View on Github external
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;
  }

  // For comments positioned after the condition parenthesis in an if statement
  // before the consequent without brackets on, such as
  // if (a) /* comment */ true,
  // we look at the next character to see if the following node
  // is the consequent for the if statement
  if (enclosingNode.body === followingNode) {
    addLeadingComment(followingNode, comment);
    return true;
github prettier / plugin-php / src / comments.js View on Github external
function handleClassComments(enclosingNode, followingNode, comment) {
  if (
    enclosingNode &&
    ["class", "interface", "trait"].includes(enclosingNode.kind)
  ) {
    // for extends nodes that have leading comments, we can store them as
    // dangling comments so we can handle them in the printer

    if (followingNode && enclosingNode.extends) {
      if (!Array.isArray(enclosingNode.extends)) {
        if (followingNode === enclosingNode.extends) {
          addDanglingComment(followingNode, comment);
          return true;
        }
      } else {
        if (
          enclosingNode.extends.some((extendsNode) => {
            if (followingNode && followingNode === extendsNode) {
              addDanglingComment(followingNode, comment);
              return true;
            }
          })
        ) {
          return true;
        }
      }
    }
github prettier / plugin-php / src / comments.js View on Github external
if (!followingNode.leadingComments) {
      followingNode.leadingComments = [];
    }

    if (!followingNode.leadingComments.includes(comment)) {
      followingNode.leadingComments.push(comment);
    }

    return true;
  } else if (
    !enclosingNode &&
    !followingNode &&
    precedingNode &&
    precedingNode.kind === "inline"
  ) {
    addDanglingComment(precedingNode, comment);
    return true;
  }
  return false;
}
github prettier / plugin-php / src / comments.js View on Github external
function handleHalt(precedingNode, enclosingNode, followingNode, comment) {
  if (enclosingNode && enclosingNode.kind === "halt") {
    addDanglingComment(enclosingNode, comment);
    return true;
  }

  if (precedingNode && precedingNode.kind === "halt") {
    addDanglingComment(precedingNode, comment);
    return true;
  }

  return false;
}
github prettier / plugin-php / src / comments.js View on Github external
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
followingNode,
  comment
) {
  if (!enclosingNode || enclosingNode.kind !== "declare") {
    return false;
  }

  if (precedingNode && precedingNode.kind === "noop") {
    return false;
  }

  if (!followingNode || enclosingNode.directives[0] === followingNode) {
    if (enclosingNode.mode === "none") {
      addTrailingComment(enclosingNode, comment);
    } else {
      addDanglingComment(enclosingNode, comment);
    }

    return true;
  }

  if (followingNode && precedingNode) {
    addLeadingComment(followingNode, comment);

    return true;
  }

  return false;
}