Repository: prettier/prettier
File: src/document/printer.js (JavaScript)

Code before:
      // reaching this scenario MIGHT be reasonable to consider a bug in the
      // plugin. However, we try to at least not crash if this ever happens;
      // instead we simply give up on returning a cursorRegionStart or
      // cursorRegionText.
      //
      // coreFormat has logic specifically to handle this scenario - where it
      // is supposed to preserve the cursor position but the printer gives it
      // no information about where the nodes around the cursor ended up -
      // although that logic is unavoidably slower (and has more potential to
      // return a perverse result) than the happy path where we help out
      // coreFormat by returning a cursorRegionStart and cursorRegionText here.
      return {
        formatted: out.filter((char) => char !== CURSOR_PLACEHOLDER).join(""),
      };
    }

    const beforeCursor = out.slice(0, cursorPlaceholderIndex).join("");
    const aroundCursor = out
      .slice(cursorPlaceholderIndex + 1, otherCursorPlaceholderIndex)
      .join("");
    const afterCursor = out.slice(otherCursorPlaceholderIndex + 1).join("");

    return {
      formatted: beforeCursor + aroundCursor + afterCursor,
      cursorRegionStart: beforeCursor.length,
      cursorRegionText: aroundCursor,
    };
  }

  return { formatted: out.join("") };
}

export { printDocToString };


Review comment (suggestion):
`printDocToString` is a public API, let's not change the signature.

Code after:
      // are if:
      // 1. a plugin added a cursor command itself, or
      // 2. one (but not both) of options.nodeAfterCursor and
      //    options.nodeAfterCursor pointed to a node within a subtree of the
      //    AST that the printer plugin used in printAstToDoc simply omits from
      //    the doc, or that it prints without recursively calling mainPrint,
      //    with the consequence that the logic for adding a cursor command in
      //    callPluginPrintFunction was never called for that node.
      // These are both weird scenarios that should be considered a bug if they
      // ever occur with one of Prettier's built-in plugins. If a third-party
      // plugin was used when printing the AST to a doc, the possibility of
      // reaching this scenario MIGHT be reasonable to consider a bug in the
      // plugin. However, we try to at least not crash if this ever happens;
      // instead we simply give up on returning a cursorNodeStart or
      // cursorNodeText.
      //
      // coreFormat has logic specifically to handle this scenario - where it
      // is supposed to preserve the cursor position but the printer gives it
      // no information about where the nodes around the cursor ended up -
      // although that logic is unavoidably slower (and has more potential to
      // return a perverse result) than the happy path where we help out
      // coreFormat by returning a cursorNodeStart and cursorNodeText here.
      return {
        formatted: out.filter((char) => char !== CURSOR_PLACEHOLDER).join(""),
      };
    }

    const beforeCursor = out.slice(0, cursorPlaceholderIndex).join("");
    const aroundCursor = out
      .slice(cursorPlaceholderIndex + 1, otherCursorPlaceholderIndex)
      .join("");
    const afterCursor = out.slice(otherCursorPlaceholderIndex + 1).join("");

    return {
      formatted: beforeCursor + aroundCursor + afterCursor,
      cursorNodeStart: beforeCursor.length,
      cursorNodeText: aroundCursor,
    };
  }

  return { formatted: out.join("") };
}

export { printDocToString };