From 8f679c2fd87e6c3208a6e941b82c16e8ad8f145b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 10 Mar 2023 10:03:20 +0800 Subject: [PATCH] Reuse `getPreferredQuote` (#14474) --- src/language-html/printer-html.js | 11 +++-------- src/language-html/utils/index.js | 11 ----------- src/language-markdown/printer-markdown.js | 13 ++----------- 3 files changed, 5 insertions(+), 30 deletions(-) diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 15941e3f8..0eeaaec6e 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -5,12 +5,9 @@ import { fill, group, hardline } from "../document/builders.js"; import { cleanDoc, replaceEndOfLine } from "../document/utils.js"; import UnexpectedNodeError from "../utils/unexpected-node-error.js"; +import getPreferredQuote from "../utils/get-preferred-quote.js"; import clean from "./clean.js"; -import { - countChars, - unescapeQuoteEntities, - getTextValueParts, -} from "./utils/index.js"; +import { unescapeQuoteEntities, getTextValueParts } from "./utils/index.js"; import preprocess from "./print-preprocess.js"; import { insertPragma } from "./pragma.js"; import { locStart, locEnd } from "./loc.js"; @@ -95,9 +92,7 @@ function genericPrint(path, options, print) { return node.rawName; } const value = unescapeQuoteEntities(node.value); - const singleQuoteCount = countChars(value, "'"); - const doubleQuoteCount = countChars(value, '"'); - const quote = singleQuoteCount < doubleQuoteCount ? "'" : '"'; + const { quote } = getPreferredQuote(value, '"'); return [ node.rawName, "=", diff --git a/src/language-html/utils/index.js b/src/language-html/utils/index.js index e1f783dfd..5225cb1be 100644 --- a/src/language-html/utils/index.js +++ b/src/language-html/utils/index.js @@ -546,16 +546,6 @@ function dedentString(text, minIndent = getMinIndentation(text)) { .join("\n"); } -function countChars(text, char) { - let counter = 0; - for (let i = 0; i < text.length; i++) { - if (text[i] === char) { - counter++; - } - } - return counter; -} - function unescapeQuoteEntities(text) { return text.replaceAll("'", "'").replaceAll(""", '"'); } @@ -629,7 +619,6 @@ export { htmlTrimPreserveIndentation, getLeadingAndTrailingHtmlWhitespace, canHaveInterpolation, - countChars, dedentString, forceBreakChildren, forceBreakContent, diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index d33eb7e98..fa88b7a2d 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -2,6 +2,7 @@ import collapseWhiteSpace from "collapse-white-space"; import getMinNotPresentContinuousCount from "../utils/get-min-not-present-continuous-count.js"; import getMaxContinuousCount from "../utils/get-max-continuous-count.js"; import getStringWidth from "../utils/get-string-width.js"; +import getPreferredQuote from "../utils/get-preferred-quote.js"; import { breakParent, join, @@ -808,17 +809,7 @@ function printTitle(title, options, printSpace = true) { if (title.includes('"') && title.includes("'") && !title.includes(")")) { return `(${title})`; // avoid escaped quotes } - // faster than using RegExps: https://jsperf.com/performance-of-match-vs-split - const singleCount = title.split("'").length - 1; - const doubleCount = title.split('"').length - 1; - const quote = - singleCount > doubleCount - ? '"' - : doubleCount > singleCount - ? "'" - : options.singleQuote - ? "'" - : '"'; + const { quote } = getPreferredQuote(title, options.singleQuote ? "'" : '"'); title = title.replaceAll("\\", "\\\\"); title = title.replaceAll(new RegExp(`(${quote})`, "g"), "\\$1"); return `${quote}${title}${quote}`;