src/language-yaml/print/mapping-item.js/printMappingItem
function printMappingItem(node, parentNode, path, print, options) {
  const { key, value } = node;

  const isEmptyMappingKey = isEmptyNode(key);
  const isEmptyMappingValue = isEmptyNode(value);

  if (isEmptyMappingKey && isEmptyMappingValue) {
    return ": ";
  }

  const printedKey = print("key");
  const spaceBeforeColon = needsSpaceInFrontOfMappingValue(node) ? " " : "";

  if (isEmptyMappingValue) {
    if (node.type === "flowMappingItem" && parentNode.type === "flowMapping") {
      return printedKey;
    }

    if (
      node.type === "mappingItem" &&
      isAbsolutelyPrintedAsSingleLineNode(key.content, options) &&
      !hasTrailingComment(key.content) &&
      (!parentNode.tag || parentNode.tag.value !== "tag:yaml.org,2002:set")
    ) {
      return [printedKey, spaceBeforeColon, ":"];
    }

    return ["? ", alignWithSpaces(2, printedKey)];
  }

  const printedValue = print("value");
  if (isEmptyMappingKey) {
    return [": ", alignWithSpaces(2, printedValue)];
  }

  // force explicit Key
  if (hasLeadingComments(value) || !isInlineNode(key.content)) {
    return [
      "? ",
      alignWithSpaces(2, printedKey),
      hardline,
      join(
        "",
        path
          .map(print, "value", "leadingComments")
          .map((comment) => [comment, hardline])
      ),
      ": ",
      alignWithSpaces(2, printedValue),
    ];
  }

  // force singleline
  if (
    isSingleLineNode(key.content) &&
    !hasLeadingComments(key.content) &&
    !hasMiddleComments(key.content) &&
    !hasTrailingComment(key.content) &&
    !hasEndComments(key) &&
    !hasLeadingComments(value.content) &&
    !hasMiddleComments(value.content) &&
    !hasEndComments(value) &&
    isAbsolutelyPrintedAsSingleLineNode(value.content, options)
  ) {
    return [printedKey, spaceBeforeColon, ": ", printedValue];
  }

  const groupId = Symbol("mappingKey");
  const groupedKey = group([
    ifBreak("? "),
    group(alignWithSpaces(2, printedKey), { id: groupId }),
  ]);

  // Construct both explicit and implicit mapping values.
  const explicitMappingValue = [
    hardline,
    ": ",
    alignWithSpaces(2, printedValue),
  ];
  /** @type {Doc[]} */
  // In the implicit case, it's convenient to treat everything from the key's colon
  // as part of the mapping value
  const implicitMappingValueParts = [spaceBeforeColon, ":"];
  if (
    hasLeadingComments(value.content) ||
    (hasEndComments(value) &&
      value.content &&
      !isNode(value.content, ["mapping", "sequence"])) ||
    (parentNode.type === "mapping" &&
      hasTrailingComment(key.content) &&
      isInlineNode(value.content)) ||
    (isNode(value.content, ["mapping", "sequence"]) &&
      value.content.tag === null &&
      value.content.anchor === null)
  ) {
    implicitMappingValueParts.push(hardline);
  } else if (value.content) {
    implicitMappingValueParts.push(line);
  }
  implicitMappingValueParts.push(printedValue);
  const implicitMappingValue = alignWithSpaces(
    options.tabWidth,
    implicitMappingValueParts
  );

  // If a key is definitely single-line, forcibly use implicit style to avoid edge cases (very long
  // keys) that would otherwise trigger explicit style as if it was multiline.
  // In those cases, explicit style makes the line even longer and causes confusion.
  if (
    isAbsolutelyPrintedAsSingleLineNode(key.content, options) &&
    !hasLeadingComments(key.content) &&
    !hasMiddleComments(key.content) &&
    !hasEndComments(key)
  ) {
    return conditionalGroup([[printedKey, implicitMappingValue]]);
  }

  // Use explicit mapping syntax if the key breaks, implicit otherwise
  return conditionalGroup([
    [
      groupedKey,
      ifBreak(explicitMappingValue, implicitMappingValue, { groupId }),
    ],
  ]);
}