Repository: prettier/prettier
File: src/document/utils/assert-doc.js (JavaScript)

Code before:
        }

        for (const doc of docs) {
          assertDoc(doc);
        }
      };

const assertDocFillParts =
  process.env.NODE_ENV === "production"
    ? noop
    : /**
       * @param {Doc[]} parts
       */
      function (parts) {
        assertDocArray(parts);
        if (parts.length > 1 && cleanDoc(parts.at(-1)) === "") {
          // stripTrailingHardline can transform trailing hardline into empty string.
          // The trailing empty string is not a problem even if it's a line element.
          parts = parts.slice(0, -1);
        }
        for (const [i, doc] of parts.entries()) {
          if (i % 2 === 0) {
            continue;
          }
          if (!isValidSeparator(doc)) {
            const type = getDocType(doc);
            throw new Error(
              `Unexpected non-line-break doc at ${i}. Doc type is ${type}.`,
            );
          }
        }
      };

/**
 * @param {Doc} doc
 * @returns {boolean}
 */
function isValidSeparator(doc) {
  let hasLine = false;
  let hasUnexpectedString = false;
  function rec(doc) {
    switch (getDocType(doc)) {
      case DOC_TYPE_LINE:
        hasLine = true;
        return;
      case DOC_TYPE_STRING:
        if (doc === "{' '}" || doc === '{" "}' || doc === " ") {
          // As of now, we can include `{' '}` in line part of `fill()`.
          // This sometimes causes overflows https://github.com/prettier/prettier/issues/2553
          // We don't have a good way to handle this case.
          return;

Review comment (bug):
Using `InvalidDocError` is not straightforward because `InvalidDocError` considers such documents valid. Error message gets weird and very hard to understand (message: "doc is valid").
I use just Error here.

Code after:
        }

        for (const doc of docs) {
          assertDoc(doc);
        }
      };

const assertDocFillParts =
  process.env.NODE_ENV === "production"
    ? noop
    : /**
       * @param {Doc[]} parts
       */
      function (parts) {
        assertDocArray(parts);
        if (parts.length > 1 && isEmptyDoc(parts.at(-1))) {
          // stripTrailingHardline can transform trailing hardline into empty string.
          // The trailing empty string is not a problem even if it's a line element.
          parts = parts.slice(0, -1);
        }
        for (const [i, doc] of parts.entries()) {
          if (i % 2 === 1 && !isValidSeparator(doc)) {
            const type = getDocType(doc);
            throw new Error(
              `Unexpected non-line-break doc at ${i}. Doc type is ${type}.`,
            );
          }
        }
      };

/**
 * @param {Doc} doc
 * @returns {boolean}
 */
function isValidSeparator(doc) {
  let hasLine = false;
  let hasUnexpectedString = false;
  function rec(doc) {
    switch (getDocType(doc)) {
      case DOC_TYPE_LINE:
        hasLine = true;
        return;
      case DOC_TYPE_STRING:
        if (doc === "{' '}" || doc === '{" "}' || doc === " ") {
          // As of now, we can include `{' '}` in line part of `fill()`.
          // This sometimes causes overflows https://github.com/prettier/prettier/issues/2553
          // We don't have a good way to handle this case.
          return;
        }
        hasUnexpectedString = true;
        return;