Repository navigation
Expand file tree
/
Copy pathprint-string.js
More file actions
45 lines (40 loc) · 1.68 KB
/
Copy pathprint-string.js
File metadata and controls
45 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import getPreferredQuote from "./get-preferred-quote.js";
import makeString from "./make-string.js";
/** @typedef {import("./get-preferred-quote.js").Quote} Quote */
function printString(raw, options) {
// `rawContent` is the string exactly like it appeared in the input source
// code, without its enclosing quotes.
const rawContent = raw.slice(1, -1);
/** @type {Quote} */
const enclosingQuote =
options.parser === "json" ||
options.parser === "jsonc" ||
// This was added before we have the `jsonc` parser
// If `{quoteProps: "preserve"}` and `{singleQuote: false}` (default value),
// and `{parser: "json5"}`, double quotes are always used for strings.
// This effectively allows using the `json5` parser for “JSON with comments and trailing commas”.
// See https://github.com/prettier/prettier/pull/10323
// See https://github.com/prettier/prettier/pull/15831#discussion_r1431010636
(options.parser === "json5" &&
options.quoteProps === "preserve" &&
!options.singleQuote)
? '"'
: options.__isInHtmlAttribute
? "'"
: getPreferredQuote(rawContent, options.singleQuote);
// It might sound unnecessary to use `makeString` even if the string already
// is enclosed with `enclosingQuote`, but it isn't. The string could contain
// unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
// sure that we consistently output the minimum amount of escaped quotes.
return makeString(
rawContent,
enclosingQuote,
!(
options.parser === "css" ||
options.parser === "less" ||
options.parser === "scss" ||
options.__embeddedInHtml
),
);
}
export default printString;