eslint-plugin-better-tailwindcss
Version:
auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.
187 lines • 7.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VUE_CONTAINER_TYPES_TO_INSERT_BRACES = exports.VUE_CONTAINER_TYPES_TO_REPLACE_QUOTES = void 0;
exports.getAttributesByVueStartTag = getAttributesByVueStartTag;
exports.getLiteralsByVueAttribute = getLiteralsByVueAttribute;
const es_js_1 = require("./es.js");
const rule_js_1 = require("../types/rule.js");
const matchers_js_1 = require("../utils/matchers.js");
const utils_js_1 = require("../utils/utils.js");
exports.VUE_CONTAINER_TYPES_TO_REPLACE_QUOTES = [
...es_js_1.ES_CONTAINER_TYPES_TO_REPLACE_QUOTES
];
exports.VUE_CONTAINER_TYPES_TO_INSERT_BRACES = [
...es_js_1.ES_CONTAINER_TYPES_TO_INSERT_BRACES
];
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, matchers_js_1.isAttributesName)(attributes)) {
if (!(0, utils_js_1.matchesName)(getVueBoundName(attributes).toLowerCase(), getVueAttributeName(attribute)?.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByVueLiteralNode(ctx, value));
}
else if ((0, matchers_js_1.isAttributesRegex)(attributes)) {
literals.push(...(0, es_js_1.getLiteralsByESNodeAndRegex)(ctx, attribute, attributes));
}
else if ((0, matchers_js_1.isAttributesMatchers)(attributes)) {
if (!(0, utils_js_1.matchesName)(getVueBoundName(attributes[0]).toLowerCase(), getVueAttributeName(attribute)?.toLowerCase())) {
return literals;
}
literals.push(...getLiteralsByVueMatchers(ctx, value, attributes[1]));
}
return literals;
}, []);
return (0, utils_js_1.deduplicateLiterals)(literals);
}
function getLiteralsByVueLiteralNode(ctx, node) {
if (!(0, es_js_1.hasESNodeParentExtension)(node)) {
return [];
}
if (isVueLiteralNode(node)) {
const literal = getStringLiteralByVueStringLiteral(ctx, node);
return [literal];
}
if ((0, es_js_1.isESStringLike)(node)) {
return getLiteralsByVueESLiteralNode(ctx, node);
}
return [];
}
function getLiteralsByVueMatchers(ctx, node, matchers) {
const matcherFunctions = getVueMatcherFunctions(matchers);
const literalNodes = (0, matchers_js_1.getLiteralNodesByMatchers)(ctx, node, matcherFunctions);
const literals = literalNodes.flatMap(literalNode => getLiteralsByVueLiteralNode(ctx, literalNode));
return (0, utils_js_1.deduplicateLiterals)(literals);
}
function getLiteralsByVueESLiteralNode(ctx, node) {
const literals = (0, es_js_1.getLiteralsByESLiteralNode)(ctx, node);
return literals.map(literal => {
const multilineQuotes = getMultilineQuotes(node);
return {
...literal,
...multilineQuotes
};
});
}
function getStringLiteralByVueStringLiteral(ctx, node) {
const raw = ctx.sourceCode.getText(node);
const line = ctx.sourceCode.lines[node.loc.start.line - 1];
const quotes = (0, utils_js_1.getQuotes)(raw);
const content = (0, utils_js_1.getContent)(raw, quotes);
const whitespaces = (0, utils_js_1.getWhitespace)(content);
const indentation = (0, utils_js_1.getIndentation)(line);
const multilineQuotes = getMultilineQuotes(node);
return {
...whitespaces,
...quotes,
...multilineQuotes,
content,
indentation,
loc: node.loc,
priorLiterals: [],
range: [node.range[0], node.range[1]],
raw,
supportsMultiline: true,
type: "StringLiteral"
};
}
function getMultilineQuotes(node) {
const surroundingBraces = exports.VUE_CONTAINER_TYPES_TO_INSERT_BRACES.includes(node.parent.type);
const multilineQuotes = exports.VUE_CONTAINER_TYPES_TO_REPLACE_QUOTES.includes(node.parent.type)
? ["`"]
: [];
return {
multilineQuotes,
surroundingBraces
};
}
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 rule_js_1.MatcherType.String: {
matcherFunctions.push((node) => {
if (!(0, es_js_1.isESNode)(node) ||
!(0, es_js_1.hasESNodeParentExtension)(node) ||
(0, matchers_js_1.isInsideConditionalExpressionTest)(node) ||
(0, matchers_js_1.isInsideLogicalExpressionLeft)(node) ||
(0, matchers_js_1.isInsideMemberExpression)(node) ||
(0, es_js_1.isESObjectKey)(node) ||
(0, es_js_1.isInsideObjectValue)(node)) {
return false;
}
return (0, es_js_1.isESStringLike)(node) || isVueLiteralNode(node);
});
break;
}
case rule_js_1.MatcherType.ObjectKey: {
matcherFunctions.push((node) => {
if (!(0, es_js_1.isESNode)(node) ||
!(0, es_js_1.hasESNodeParentExtension)(node) ||
!(0, es_js_1.isESObjectKey)(node) ||
(0, matchers_js_1.isInsideConditionalExpressionTest)(node) ||
(0, matchers_js_1.isInsideLogicalExpressionLeft)(node) ||
(0, matchers_js_1.isInsideMemberExpression)(node)) {
return false;
}
const path = (0, es_js_1.getESObjectPath)(node);
if (!path || !matcher.pathPattern) {
return true;
}
return (0, matchers_js_1.matchesPathPattern)(path, matcher.pathPattern);
});
break;
}
case rule_js_1.MatcherType.ObjectValue: {
matcherFunctions.push((node) => {
if (!(0, es_js_1.isESNode)(node) ||
!(0, es_js_1.hasESNodeParentExtension)(node) ||
!(0, es_js_1.isInsideObjectValue)(node) ||
(0, matchers_js_1.isInsideConditionalExpressionTest)(node) ||
(0, matchers_js_1.isInsideLogicalExpressionLeft)(node) ||
(0, matchers_js_1.isInsideMemberExpression)(node) ||
(0, es_js_1.isESObjectKey)(node) ||
!(0, es_js_1.isESStringLike)(node) && !isVueLiteralNode(node)) {
return false;
}
const path = (0, es_js_1.getESObjectPath)(node);
if (!path || !matcher.pathPattern) {
return true;
}
return (0, matchers_js_1.matchesPathPattern)(path, matcher.pathPattern);
});
break;
}
}
return matcherFunctions;
}, []);
}
//# sourceMappingURL=vue.js.map