eslint-plugin-readable-tailwind
Version:
auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.
162 lines • 7.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAttributesByVueStartTag = getAttributesByVueStartTag;
exports.getLiteralsByVueAttribute = getLiteralsByVueAttribute;
const readable_tailwind_parsers_es_js_1 = require("./es.js");
const readable_tailwind_types_rule_js_1 = require("../types/rule.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 getAttributesByVueStartTag(ctx, node) {
return node.attributes;
}
function getLiteralsByVueAttribute(ctx, attribute, attributes) {
if (attribute.value === null) {
return [];
}
const value = attribute.value;
const literals = attributes.reduce((literals, attributes) => {
if ((0, readable_tailwind_utils_matchers_js_1.isAttributesName)(attributes)) {
if (!(0, readable_tailwind_utils_utils_js_1.matchesName)(getVueBoundName(attributes).toLowerCase(), getVueAttributeName(attribute)?.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByVueLiteralNode(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 (!(0, readable_tailwind_utils_utils_js_1.matchesName)(getVueBoundName(attributes[0]).toLowerCase(), getVueAttributeName(attribute)?.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByVueMatchers(ctx, value, attributes[1]));
}
return literals;
}, []);
return (0, readable_tailwind_utils_utils_js_1.deduplicateLiterals)(literals);
}
function getLiteralsByVueLiteralNode(ctx, node) {
if (isVueLiteralNode(node)) {
const literal = getStringLiteralByVueStringLiteral(ctx, node);
return [literal];
}
if ((0, readable_tailwind_parsers_es_js_1.isESStringLike)(node)) {
return (0, readable_tailwind_parsers_es_js_1.getLiteralsByESLiteralNode)(ctx, node);
}
return [];
}
function getLiteralsByVueMatchers(ctx, node, matchers) {
const matcherFunctions = getVueMatcherFunctions(matchers);
const literalNodes = (0, readable_tailwind_utils_matchers_js_1.getLiteralNodesByMatchers)(ctx, node, matcherFunctions);
const literals = literalNodes.reduce((literals, literalNode) => {
literals.push(...getLiteralsByVueLiteralNode(ctx, literalNode));
return literals;
}, []);
return (0, readable_tailwind_utils_utils_js_1.deduplicateLiterals)(literals);
}
function getStringLiteralByVueStringLiteral(ctx, node) {
const content = node.value;
const raw = ctx.sourceCode.getText(node);
const quotes = (0, readable_tailwind_utils_utils_js_1.getQuotes)(raw);
const whitespaces = (0, readable_tailwind_utils_utils_js_1.getWhitespace)(content);
return {
...whitespaces,
...quotes,
content,
loc: node.loc,
node: node,
parent: node.parent,
range: [node.range[0], node.range[1]],
raw,
type: "StringLiteral"
};
}
function getVueBoundName(name) {
return name.startsWith(":") ? `v-bind:${name.slice(1)}` : name;
}
function getVueAttributeName(attribute) {
if (isVueAttribute(attribute)) {
return attribute.key.name;
}
if (isVueDirective(attribute)) {
if (attribute.key.argument?.type === "VIdentifier") {
return `v-${attribute.key.name.name}:${attribute.key.argument.name}`;
}
}
}
function isVueAttribute(attribute) {
return attribute.key.type === "VIdentifier";
}
function isVueDirective(attribute) {
return attribute.key.type === "VDirectiveKey";
}
function isVueLiteralNode(node) {
return node.type === "VLiteral";
}
function getVueMatcherFunctions(matchers) {
return matchers.reduce((matcherFunctions, matcher) => {
switch (matcher.match) {
case readable_tailwind_types_rule_js_1.MatcherType.String: {
matcherFunctions.push(node => {
if ((0, readable_tailwind_utils_matchers_js_1.isInsideConditionalExpressionTest)(node)) {
return false;
}
if ((0, readable_tailwind_utils_matchers_js_1.isInsideLogicalExpressionLeft)(node)) {
return false;
}
if (!(0, readable_tailwind_parsers_es_js_1.hasESNodeParentExtension)(node)) {
return false;
}
return (!(0, readable_tailwind_parsers_es_js_1.isESObjectKey)(node) &&
!(0, readable_tailwind_parsers_es_js_1.isInsideObjectValue)(node) &&
((0, readable_tailwind_parsers_es_js_1.isESStringLike)(node) || isVueLiteralNode(node)));
});
break;
}
case readable_tailwind_types_rule_js_1.MatcherType.ObjectKey: {
matcherFunctions.push(node => {
if ((0, readable_tailwind_utils_matchers_js_1.isInsideConditionalExpressionTest)(node)) {
return false;
}
if ((0, readable_tailwind_utils_matchers_js_1.isInsideLogicalExpressionLeft)(node)) {
return false;
}
if (!(0, readable_tailwind_parsers_es_js_1.hasESNodeParentExtension)(node)) {
return false;
}
if (!(0, readable_tailwind_parsers_es_js_1.isESObjectKey)(node)) {
return false;
}
const path = (0, readable_tailwind_utils_matchers_js_1.getObjectPath)(node);
return path && matcher.pathPattern ? (0, readable_tailwind_utils_matchers_js_1.matchesPathPattern)(path, matcher.pathPattern) : true;
});
break;
}
case readable_tailwind_types_rule_js_1.MatcherType.ObjectValue: {
matcherFunctions.push(node => {
if ((0, readable_tailwind_utils_matchers_js_1.isInsideConditionalExpressionTest)(node)) {
return false;
}
if ((0, readable_tailwind_utils_matchers_js_1.isInsideLogicalExpressionLeft)(node)) {
return false;
}
if (!(0, readable_tailwind_parsers_es_js_1.hasESNodeParentExtension)(node)) {
return false;
}
if ((0, readable_tailwind_parsers_es_js_1.isESObjectKey)(node)) {
return false;
}
const path = (0, readable_tailwind_utils_matchers_js_1.getObjectPath)(node);
const matchesPattern = path !== undefined &&
matcher.pathPattern
? (0, readable_tailwind_utils_matchers_js_1.matchesPathPattern)(path, matcher.pathPattern)
: true;
return (0, readable_tailwind_parsers_es_js_1.isInsideObjectValue)(node) && (0, readable_tailwind_parsers_es_js_1.isESStringLike)(node) && matchesPattern;
});
break;
}
}
return matcherFunctions;
}, []);
}
//# sourceMappingURL=vue.js.map