UNPKG

eslint-plugin-better-tailwindcss

Version:

auto-wraps tailwind classes after a certain print width or class count into multiple lines to improve readability.

122 lines 4.35 kB
import { ES_CONTAINER_TYPES_TO_INSERT_BRACES, ES_CONTAINER_TYPES_TO_REPLACE_QUOTES, getESMatcherFunctions, getLiteralsByESLiteralNode, hasESNodeParentExtension, isESStringLike } from "./es.js"; import { getLiteralNodesByMatchers } from "../utils/matchers.js"; import { addAttribute, deduplicateLiterals, getContent, getIndentation, getQuotes, getWhitespace, matchesName } from "../utils/utils.js"; export const VUE_CONTAINER_TYPES_TO_REPLACE_QUOTES = [ ...ES_CONTAINER_TYPES_TO_REPLACE_QUOTES ]; export const VUE_CONTAINER_TYPES_TO_INSERT_BRACES = [ ...ES_CONTAINER_TYPES_TO_INSERT_BRACES ]; export function getAttributesByVueStartTag(ctx, node) { return node.attributes; } export function getLiteralsByVueAttribute(ctx, attribute, selectors) { if (attribute.value === null) { return []; } const name = getVueAttributeName(attribute); const value = attribute.value; const literals = selectors.reduce((literals, selector) => { if (!matchesName(getVueBoundName(selector.name).toLowerCase(), name?.toLowerCase())) { return literals; } if (!selector.match) { literals.push(...getLiteralsByVueLiteralNode(ctx, value)); return literals; } literals.push(...getLiteralsByVueMatchers(ctx, value, selector.match)); return literals; }, []); return literals .filter(deduplicateLiterals) .map(addAttribute(name)); } function getLiteralsByVueLiteralNode(ctx, node) { if (!hasESNodeParentExtension(node)) { return []; } if (isVueLiteralNode(node)) { const literal = getStringLiteralByVueStringLiteral(ctx, node); return [literal]; } if (isESStringLike(node)) { return getLiteralsByVueESLiteralNode(ctx, node); } return []; } function getLiteralsByVueMatchers(ctx, node, matchers) { const matcherFunctions = getVueMatcherFunctions(matchers); const literalNodes = getLiteralNodesByMatchers(ctx, node, matcherFunctions); const literals = literalNodes.flatMap(literalNode => getLiteralsByVueLiteralNode(ctx, literalNode)); return literals.filter(deduplicateLiterals); } function getLiteralsByVueESLiteralNode(ctx, node) { const literals = 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 = getQuotes(raw); const content = getContent(raw, quotes); const whitespaces = getWhitespace(content); const indentation = 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 = VUE_CONTAINER_TYPES_TO_INSERT_BRACES.includes(node.parent.type); const multilineQuotes = 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 getESMatcherFunctions(matchers, { isStringLikeNode: isVueLiteralNode }); } //# sourceMappingURL=vue.js.map