UNPKG

stylelint-css-in-js-syntax

Version:

This plugin provides [ESLint](https://eslint.org/) rules that limit the line length of your comments. Furthermore, an **automatic fix** is included such that you can save time manually formatting your comments. As such it is recommended to apply this rule

46 lines (44 loc) 2.58 kB
function stringifyExpressions(quasis, expressions) { let cssString = ""; const refs = []; for (let i = 0; i < quasis.length; i++) { const currentQuasi = quasis[i]?.value.cooked; cssString += currentQuasi; const nextExpression = expressions[i]; if (nextExpression) { const nearestChar = cssString?.replace(/\n/g, "").trimEnd().at(-1); const hasCommentBefore = `${cssString?.replace(/\n/g, "").trimEnd().at(-2)}${nearestChar}` === "*/"; const hasCommentBeforeOnSameLine = `${cssString.trimEnd().at(-2)}${nearestChar}` === "*/"; const nextQuasi = quasis[i + 1]?.value.cooked.trimStart(); const currentQuasiEndsWithNewLine = /\n( |\t\r)*?/.test( currentQuasi ?? "" ); const base64Expression = Buffer.from(nextExpression).toString("base64"); const refIndexToExpression = (() => { const existingIndex = refs.indexOf(base64Expression); if (existingIndex !== -1) { return existingIndex; } else { refs.push(base64Expression); return refs.length - 1; } })(); const newlineCount = nextExpression.split("\n").length - 1; if ((!nearestChar || ["{", ";", "}"].includes(nearestChar) || hasCommentBefore && !hasCommentBeforeOnSameLine) && !nextQuasi?.startsWith("{") && !nextQuasi?.startsWith("&") && !nextQuasi?.startsWith(".") && !nextQuasi?.startsWith("[") && !nextQuasi?.startsWith("$") && nextQuasi !== "" && (currentQuasiEndsWithNewLine && !nextQuasi?.trim().startsWith(":") || nextQuasi?.startsWith("\n") || nextQuasi?.startsWith(";"))) { cssString += `ref-${refIndexToExpression}:ignore${"\n".repeat(newlineCount)}_${nextQuasi?.startsWith(";") || nextQuasi?.startsWith("{") ? "" : ";"}`; } else { const sanitizedExpression = sanitizeExpression( nextExpression ).replaceAll("\n", ""); cssString += `ref-${refIndexToExpression}_${sanitizedExpression}${"\n".repeat(newlineCount)}_`; } } } cssString += `/*refs ${refs.join(";")}*/`; return cssString; } function sanitizeExpression(expression) { return expression.replaceAll(" ", "").replaceAll("\n", "").replaceAll(/[^A-Za-z\d]/g, ($1) => `\\${$1}`).replaceAll("\\_", "\u02CD").replaceAll("\\.", ".").replaceAll("\\:", "\uA789").replaceAll("\\,", "\uFF0C").replaceAll("\\(", "\u2E28").replaceAll("\\)", "\u2E29").replaceAll("\\{", "\u2774").replaceAll("\\}", "\u2775").replaceAll("\\[", "\u2045").replaceAll("\\]", "\u2046"); } export { stringifyExpressions }; //# sourceMappingURL=util.stringify-expressions.mjs.map