UNPKG

eslint-plugin-better-tailwindcss

Version:

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

205 lines 8.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SVELTE_CONTAINER_TYPES_TO_INSERT_BRACES = exports.SVELTE_CONTAINER_TYPES_TO_REPLACE_QUOTES = void 0; exports.getAttributesBySvelteTag = getAttributesBySvelteTag; exports.getLiteralsBySvelteAttribute = getLiteralsBySvelteAttribute; 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.SVELTE_CONTAINER_TYPES_TO_REPLACE_QUOTES = [ ...es_js_1.ES_CONTAINER_TYPES_TO_REPLACE_QUOTES, "SvelteMustacheTag" ]; exports.SVELTE_CONTAINER_TYPES_TO_INSERT_BRACES = []; function getAttributesBySvelteTag(ctx, node) { return node.attributes.reduce((acc, attribute) => { if (isSvelteAttribute(attribute)) { acc.push(attribute); } return acc; }, []); } function getLiteralsBySvelteAttribute(ctx, attribute, attributes) { // skip shorthand attributes #42 if (!Array.isArray(attribute.value)) { return []; } const literals = attributes.reduce((literals, attributes) => { if ((0, matchers_js_1.isAttributesRegex)(attributes)) { literals.push(...(0, es_js_1.getLiteralsByESNodeAndRegex)(ctx, attribute, attributes)); } for (const value of attribute.value) { if ((0, matchers_js_1.isAttributesName)(attributes)) { if (!(0, utils_js_1.matchesName)(attributes.toLowerCase(), attribute.key.name.toLowerCase())) { continue; } literals.push(...getLiteralsBySvelteLiteralNode(ctx, value)); } else if ((0, matchers_js_1.isAttributesMatchers)(attributes)) { if (!(0, utils_js_1.matchesName)(attributes[0].toLowerCase(), attribute.key.name.toLowerCase())) { continue; } literals.push(...getLiteralsBySvelteMatchers(ctx, value, attributes[1])); } } return literals; }, []); return (0, utils_js_1.deduplicateLiterals)(literals); } function getLiteralsBySvelteMatchers(ctx, node, matchers) { const matcherFunctions = getSvelteMatcherFunctions(matchers); const literalNodes = (0, matchers_js_1.getLiteralNodesByMatchers)(ctx, node, matcherFunctions); const literals = literalNodes.flatMap(literalNode => getLiteralsBySvelteLiteralNode(ctx, literalNode)); return (0, utils_js_1.deduplicateLiterals)(literals); } function getLiteralsBySvelteLiteralNode(ctx, node) { if (isSvelteStringLiteral(node)) { const stringLiteral = getStringLiteralBySvelteStringLiteral(ctx, node); if (stringLiteral) { return [stringLiteral]; } } if (isSvelteMustacheTag(node)) { return getLiteralsBySvelteLiteralNode(ctx, node.expression); } if ((0, es_js_1.isESStringLike)(node)) { return getLiteralsBySvelteESLiteralNode(ctx, node); } return []; } function getLiteralsBySvelteESLiteralNode(ctx, node) { const literals = (0, es_js_1.getLiteralsByESLiteralNode)(ctx, node); return literals.map(literal => { if (!(0, es_js_1.hasESNodeParentExtension)(node)) { return literal; } const multilineQuotes = getMultilineQuotes(node); return { ...literal, ...multilineQuotes }; }); } function getStringLiteralBySvelteStringLiteral(ctx, node) { const raw = ctx.sourceCode.getText(node, 1, 1); const braces = getBracesByString(ctx, raw); const isInterpolated = getIsInterpolated(ctx, raw); const quotes = (0, utils_js_1.getQuotes)(raw); const content = (0, utils_js_1.getContent)(raw, quotes, braces); const whitespaces = (0, utils_js_1.getWhitespace)(content); const line = ctx.sourceCode.lines[isInterpolated ? node.parent.loc.start.line - 1 : node.loc.start.line - 1]; const indentation = (0, utils_js_1.getIndentation)(line); const multilineQuotes = getMultilineQuotes(node); return { ...whitespaces, ...quotes, ...braces, ...multilineQuotes, content, indentation, isInterpolated, loc: node.loc, range: [node.range[0] - 1, node.range[1] + 1], // include quotes in range raw, supportsMultiline: true, type: "StringLiteral" }; } function getBracesByString(ctx, raw) { const closingBraces = raw.trim().startsWith("}") ? "}" : undefined; const openingTemplateBraces = raw.trim().endsWith("${") ? "${" : undefined; const openingBrace = raw.trim().endsWith("{") ? "{" : undefined; const openingBraces = openingTemplateBraces ?? openingBrace; return { closingBraces, openingBraces }; } function getIsInterpolated(ctx, raw) { const braces = getBracesByString(ctx, raw); return !!braces.closingBraces || !!braces.openingBraces; } function getMultilineQuotes(node) { const surroundingBraces = exports.SVELTE_CONTAINER_TYPES_TO_INSERT_BRACES.includes(node.parent.type); const multilineQuotes = exports.SVELTE_CONTAINER_TYPES_TO_REPLACE_QUOTES.includes(node.parent.type) ? ["'", "\"", "`"] : []; return { multilineQuotes, surroundingBraces }; } function isSvelteAttribute(attribute) { return "key" in attribute && "name" in attribute.key && typeof attribute.key.name === "string"; } function isSvelteStringLiteral(node) { return node.type === "SvelteLiteral"; } function isSvelteMustacheTag(node) { return node.type === "SvelteMustacheTag" && "kind" in node && node.kind === "text"; } function getSvelteMatcherFunctions(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.isInsideBinaryExpression)(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) || isSvelteStringLiteral(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.isInsideBinaryExpression)(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.isInsideBinaryExpression)(node) || (0, matchers_js_1.isInsideConditionalExpressionTest)(node) || (0, matchers_js_1.isInsideLogicalExpressionLeft)(node) || (0, es_js_1.isESObjectKey)(node) || !(0, es_js_1.isESStringLike)(node) && !isSvelteStringLiteral(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=svelte.js.map