@import {Quote} from "./get-preferred-quote.js"
(raw, options)
| 9 | /** @import {Quote} from "./get-preferred-quote.js" */ |
| 10 | |
| 11 | function 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 | |
| 51 | export default printString; |
no test coverage detected
searching dependent graphs…