eslint-plugin-better-tailwindcss
Version:
auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.
72 lines • 2.27 kB
JavaScript
import { getIndentation, getWhitespace } from "../utils/utils.js";
export function getLiteralsByCSSAtRule(ctx, node) {
const literals = [];
if (node.name !== "apply") {
return [];
}
if (node.prelude?.type === "AtrulePrelude" || node.prelude?.type === "Raw") {
const literal = getLiteralsByAtrule(ctx, node);
if (literal) {
literals.push(literal);
}
}
return literals;
}
function getLiteralsByAtrule(ctx, node) {
// @ts-expect-error - CSS Tree types are different
const raw = ctx.sourceCode.getText(node);
const match = raw.match(/^(?<leadingApply>@apply[\t ](?!\r?\n)|@apply(?=\s))(?<content>.+?)(?<trailingSemicolon>;?\s*)$/s);
if (!match?.groups?.leadingApply || !match.groups.content || match.groups.trailingSemicolon === undefined) {
return;
}
const { content, leadingApply, trailingSemicolon } = match.groups;
const startOffset = leadingApply.length;
const endOffset = trailingSemicolon.length;
const loc = getLoc(ctx, node, startOffset, endOffset);
const range = getRange(ctx, node, startOffset, endOffset);
if (!loc) {
return;
}
const line = ctx.sourceCode.lines[node.loc.start.line - 1];
const indentation = getIndentation(line);
const whitespaces = getWhitespace(content);
const type = "CSSClassListLiteral";
return {
...whitespaces,
content,
indentation,
isInterpolated: false,
leadingApply,
loc,
range,
raw: content,
supportsMultiline: true,
trailingSemicolon,
type
};
}
function getLoc(ctx, node, startOffset, endOffset) {
if (!node.loc) {
return;
}
return {
end: {
column: node.loc.end.column - endOffset,
line: node.loc.end.line
},
start: {
column: node.loc.start.column + startOffset,
line: node.loc.start.line
}
};
}
function getRange(ctx, node, startOffset, endOffset) {
const range = ctx.sourceCode
// @ts-expect-error - CSS Tree types are different
.getRange(node);
return [
range[0] + startOffset,
range[1] - endOffset
];
}
//# sourceMappingURL=css.js.map