eslint-plugin-readable-tailwind
Version:
auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.
76 lines • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLiteralsByJSXAttribute = getLiteralsByJSXAttribute;
exports.getAttributesByJSXElement = getAttributesByJSXElement;
const readable_tailwind_parsers_es_js_1 = require("./es.js");
const readable_tailwind_utils_matchers_js_1 = require("../utils/matchers.js");
const readable_tailwind_utils_regex_js_1 = require("../utils/regex.js");
const readable_tailwind_utils_utils_js_1 = require("../utils/utils.js");
function getLiteralsByJSXAttribute(ctx, attribute, attributes) {
const value = attribute.value;
const literals = attributes.reduce((literals, attributes) => {
if (!value) {
return literals;
}
if ((0, readable_tailwind_utils_matchers_js_1.isAttributesName)(attributes)) {
if (typeof attribute.name.name !== "string" || !(0, readable_tailwind_utils_utils_js_1.matchesName)(attributes.toLowerCase(), attribute.name.name.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByJSXAttributeValue(ctx, value));
}
else if ((0, readable_tailwind_utils_matchers_js_1.isAttributesRegex)(attributes)) {
literals.push(...(0, readable_tailwind_utils_regex_js_1.getLiteralsByESNodeAndRegex)(ctx, attribute, attributes));
}
else if ((0, readable_tailwind_utils_matchers_js_1.isAttributesMatchers)(attributes)) {
if (typeof attribute.name.name !== "string" || !(0, readable_tailwind_utils_utils_js_1.matchesName)(attributes[0].toLowerCase(), attribute.name.name.toLowerCase())) {
return literals;
}
literals.push(...(0, readable_tailwind_parsers_es_js_1.getLiteralsByESMatchers)(ctx, value, attributes[1]));
}
return literals;
}, []);
return (0, readable_tailwind_utils_utils_js_1.deduplicateLiterals)(literals);
}
function getAttributesByJSXElement(ctx, node) {
return node.attributes.reduce((acc, attribute) => {
if (isJSXAttribute(attribute)) {
acc.push(attribute);
}
return acc;
}, []);
}
function getLiteralsByJSXAttributeValue(ctx, value) {
if (!value) {
return [];
}
if ((0, readable_tailwind_parsers_es_js_1.isESSimpleStringLiteral)(value)) {
const stringLiteral = (0, readable_tailwind_parsers_es_js_1.getStringLiteralByESStringLiteral)(ctx, value);
if (stringLiteral) {
return [stringLiteral];
}
}
if (isJSXExpressionContainerWithESSimpleStringLiteral(value)) {
const stringLiteral = (0, readable_tailwind_parsers_es_js_1.getStringLiteralByESStringLiteral)(ctx, value.expression);
if (stringLiteral) {
return [stringLiteral];
}
}
if (isJSXExpressionContainerWithESTemplateLiteral(value)) {
return (0, readable_tailwind_parsers_es_js_1.getLiteralsByESTemplateLiteral)(ctx, value.expression);
}
return [];
}
function isJSXExpressionContainerWithESSimpleStringLiteral(node) {
return node.type === "JSXExpressionContainer" && "expression" in node &&
(0, readable_tailwind_parsers_es_js_1.isESNode)(node.expression) &&
(0, readable_tailwind_parsers_es_js_1.isESSimpleStringLiteral)(node.expression);
}
function isJSXExpressionContainerWithESTemplateLiteral(node) {
return node.type === "JSXExpressionContainer" && "expression" in node &&
(0, readable_tailwind_parsers_es_js_1.isESNode)(node.expression) &&
(0, readable_tailwind_parsers_es_js_1.isESTemplateLiteral)(node.expression);
}
function isJSXAttribute(node) {
return node.type === "JSXAttribute";
}
//# sourceMappingURL=jsx.js.map