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
65 lines (62 loc) • 2.34 kB
JavaScript
import { all } from 'known-css-properties';
import { getPreviousWord } from '../util.get-previous-word.mjs';
const NEW_STATEMENT_INDICATORS = [":", "{", "@", "&"];
function fixUnclosedDelcarations(content) {
let cssString = "";
let isInsideComment = false;
for (let cursor = 0; cursor < content.length; cursor++) {
const currentChar = content[cursor];
if (!isInsideComment && currentChar === "/" && content[cursor + 1] === "*") {
isInsideComment = true;
}
if (!isInsideComment && currentChar === ":" && all.includes(getPreviousWord(content, cursor))) {
const correction = isDeclarationClosedCorrectly(content, cursor + 1);
if (!correction.isClosedCorrectly) {
cssString += content.substring(
cursor,
correction.canBeCorrectedAtIndex
);
cssString += "/*__auto-close__*/;";
cursor = correction.canBeCorrectedAtIndex - 1;
continue;
}
}
if (isInsideComment && currentChar === "*" && content[cursor + 1] === "/") {
isInsideComment = false;
}
cssString += currentChar;
}
return cssString;
}
function isDeclarationClosedCorrectly(content, startIndex) {
let isInsideComment = false;
let isEvaluatingFontName = false;
let firstNewlineIndex = void 0;
for (let correctionCursor = startIndex; correctionCursor < content.length; correctionCursor++) {
const nextChar = content[correctionCursor] ?? "";
if (!isInsideComment && nextChar === "/" && content[correctionCursor + 1] === "*") {
isInsideComment = true;
}
if (!firstNewlineIndex && nextChar === "\n") {
firstNewlineIndex = correctionCursor;
}
if (nextChar === ";") {
return { isClosedCorrectly: true };
}
if (nextChar === `'` || nextChar === `"`) {
isEvaluatingFontName = !isEvaluatingFontName;
}
if (!isInsideComment && !isEvaluatingFontName && NEW_STATEMENT_INDICATORS.includes(nextChar)) {
return {
isClosedCorrectly: false,
canBeCorrectedAtIndex: firstNewlineIndex || correctionCursor
};
}
if (isInsideComment && nextChar === "*" && content[correctionCursor + 1] === "/") {
isInsideComment = false;
}
}
return { isClosedCorrectly: true };
}
export { fixUnclosedDelcarations };
//# sourceMappingURL=util.fix-unclosed-declarations.mjs.map