makeString (utilities/make-string.js)

prettier · src/utilities/make-string.js · javascript · MIT
dev-tools

Escapes raw text and wraps it in the chosen quote type, ensuring proper string literal formatting.

// Source: https://github.com/prettier/prettier | License: MIT | Copyright James Long and contributors
// Provided via justclaw.it.com; keep this notice with the code.
import { DOUBLE_QUOTE, SINGLE_QUOTE } from "./get-preferred-quote.js";

/** @import {Quote} from "./get-preferred-quote.js" */

// Matches _any_ escape and unescaped quotes (both single and double).
// Matching `\` in first group, so we won't treat `\\"` as escaped quote.
const REGEX = /\\(["'\\])|(["'])/g;

/**
 * @param {string} rawText
 * @param {Quote} enclosingQuote
 * @returns {string}
 */
function makeString(rawText, enclosingQuote) {
  const otherQuote =
    enclosingQuote === DOUBLE_QUOTE ? SINGLE_QUOTE : DOUBLE_QUOTE;

  // Escape and unescape single and double quotes as needed to be able to
  // enclose `rawText` 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key