eslint-plugin-readable-tailwind
Version:
auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.
158 lines • 6.28 kB
JavaScript
import { getLiteralsByESLiteralNode, hasESNodeParentExtension, isESObjectKey, isESStringLike, isInsideObjectValue } from "./es.js";
import { MatcherType } from "../types/rule.js";
import { getLiteralNodesByMatchers, getObjectPath, isAttributesMatchers, isAttributesName, isAttributesRegex, isInsideConditionalExpressionTest, isInsideLogicalExpressionLeft, matchesPathPattern } from "../utils/matchers.js";
import { getLiteralsByESNodeAndRegex } from "../utils/regex.js";
import { deduplicateLiterals, getQuotes, getWhitespace, matchesName } from "../utils/utils.js";
export function getAttributesByVueStartTag(ctx, node) {
return node.attributes;
}
export function getLiteralsByVueAttribute(ctx, attribute, attributes) {
if (attribute.value === null) {
return [];
}
const value = attribute.value;
const literals = attributes.reduce((literals, attributes) => {
if (isAttributesName(attributes)) {
if (!matchesName(getVueBoundName(attributes).toLowerCase(), getVueAttributeName(attribute)?.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByVueLiteralNode(ctx, value));
}
else if (isAttributesRegex(attributes)) {
literals.push(...getLiteralsByESNodeAndRegex(ctx, attribute, attributes));
}
else if (isAttributesMatchers(attributes)) {
if (!matchesName(getVueBoundName(attributes[0]).toLowerCase(), getVueAttributeName(attribute)?.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByVueMatchers(ctx, value, attributes[1]));
}
return literals;
}, []);
return deduplicateLiterals(literals);
}
function getLiteralsByVueLiteralNode(ctx, node) {
if (isVueLiteralNode(node)) {
const literal = getStringLiteralByVueStringLiteral(ctx, node);
return [literal];
}
if (isESStringLike(node)) {
return getLiteralsByESLiteralNode(ctx, node);
}
return [];
}
function getLiteralsByVueMatchers(ctx, node, matchers) {
const matcherFunctions = getVueMatcherFunctions(matchers);
const literalNodes = getLiteralNodesByMatchers(ctx, node, matcherFunctions);
const literals = literalNodes.reduce((literals, literalNode) => {
literals.push(...getLiteralsByVueLiteralNode(ctx, literalNode));
return literals;
}, []);
return deduplicateLiterals(literals);
}
function getStringLiteralByVueStringLiteral(ctx, node) {
const content = node.value;
const raw = ctx.sourceCode.getText(node);
const quotes = getQuotes(raw);
const whitespaces = 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 MatcherType.String: {
matcherFunctions.push(node => {
if (isInsideConditionalExpressionTest(node)) {
return false;
}
if (isInsideLogicalExpressionLeft(node)) {
return false;
}
if (!hasESNodeParentExtension(node)) {
return false;
}
return (!isESObjectKey(node) &&
!isInsideObjectValue(node) &&
(isESStringLike(node) || isVueLiteralNode(node)));
});
break;
}
case MatcherType.ObjectKey: {
matcherFunctions.push(node => {
if (isInsideConditionalExpressionTest(node)) {
return false;
}
if (isInsideLogicalExpressionLeft(node)) {
return false;
}
if (!hasESNodeParentExtension(node)) {
return false;
}
if (!isESObjectKey(node)) {
return false;
}
const path = getObjectPath(node);
return path && matcher.pathPattern ? matchesPathPattern(path, matcher.pathPattern) : true;
});
break;
}
case MatcherType.ObjectValue: {
matcherFunctions.push(node => {
if (isInsideConditionalExpressionTest(node)) {
return false;
}
if (isInsideLogicalExpressionLeft(node)) {
return false;
}
if (!hasESNodeParentExtension(node)) {
return false;
}
if (isESObjectKey(node)) {
return false;
}
const path = getObjectPath(node);
const matchesPattern = path !== undefined &&
matcher.pathPattern
? matchesPathPattern(path, matcher.pathPattern)
: true;
return isInsideObjectValue(node) && isESStringLike(node) && matchesPattern;
});
break;
}
}
return matcherFunctions;
}, []);
}
//# sourceMappingURL=vue.js.map