MCPcopy
hub / github.com/prettier/prettier / printString

Function printString

src/utilities/print-string.js:11–49  ·  view source on GitHub ↗

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

(raw, options)

Source from the content-addressed store, hash-verified

9/** @import {Quote} from "./get-preferred-quote.js" */
10
11function printString(raw, options) {
12 assert.ok(/^(?<quote>["']).*\k<quote>$/s.test(raw));
13
14 // `rawContent` is the string exactly like it appeared in the input source
15 // code, without its enclosing quotes.
16 const rawContent = raw.slice(1, -1);
17
18 /** @type {Quote} */
19 let enclosingQuote;
20
21 if (
22 options.parser === "json" ||
23 options.parser === "jsonc" ||
24 options.parser === "json-stringify" ||
25 // This was added before we have the `jsonc` parser
26 // If `{quoteProps: "preserve"}` and `{singleQuote: false}` (default value),
27 // and `{parser: "json5"}`, double quotes are always used for strings.
28 // This effectively allows using the `json5` parser for “JSON with comments and trailing commas”.
29 // See https://github.com/prettier/prettier/pull/10323
30 // See https://github.com/prettier/prettier/pull/15831#discussion_r1431010636
31 (options.parser === "json5" &&
32 options.quoteProps === "preserve" &&
33 !options.singleQuote)
34 ) {
35 enclosingQuote = DOUBLE_QUOTE;
36 } else if (options.__isInHtmlAttribute) {
37 enclosingQuote = SINGLE_QUOTE;
38 } else {
39 enclosingQuote = getPreferredQuote(rawContent, options.singleQuote);
40 }
41
42 const originalQuote = raw.charAt(0);
43
44 if (originalQuote === enclosingQuote) {
45 return raw;
46 }
47
48 return makeString(rawContent, enclosingQuote);
49}
50
51export default printString;

Callers 8

genericPrintFunction · 0.85
adjustStringsFunction · 0.85
printJsonFunction · 0.85
isLoneShortArgumentFunction · 0.85
isKeySafeToUnquoteFunction · 0.85
printKeyFunction · 0.85
printFlowFunction · 0.85
printLiteralFunction · 0.85

Calls 3

getPreferredQuoteFunction · 0.90
makeStringFunction · 0.70
testMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…