|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import { createRule } from "../utils" |
| 3 | + |
| 4 | +/** |
| 5 | + * Check whether the component is declared in a single line or not. |
| 6 | + */ |
| 7 | +function isSingleLine(node: AST.SvelteStartTag) { |
| 8 | + return node.loc.start.line === node.loc.end.line |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Group attributes line by line. |
| 13 | + */ |
| 14 | +function groupAttributesByLine(attributes: AST.SvelteStartTag["attributes"]) { |
| 15 | + const group: AST.SvelteStartTag["attributes"][] = [] |
| 16 | + for (const attr of attributes) { |
| 17 | + if (group[0]?.[0]?.loc.end.line === attr.loc.start.line) { |
| 18 | + group[0].push(attr) |
| 19 | + } else { |
| 20 | + group.unshift([attr]) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + return group.reverse() |
| 25 | +} |
| 26 | + |
| 27 | +export default createRule("max-attributes-per-line", { |
| 28 | + meta: { |
| 29 | + docs: { |
| 30 | + description: "enforce the maximum number of attributes per line", |
| 31 | + recommended: false, |
| 32 | + }, |
| 33 | + fixable: "whitespace", |
| 34 | + schema: [ |
| 35 | + { |
| 36 | + type: "object", |
| 37 | + properties: { |
| 38 | + multiline: { |
| 39 | + type: "number", |
| 40 | + minimum: 1, |
| 41 | + }, |
| 42 | + singleline: { |
| 43 | + type: "number", |
| 44 | + minimum: 1, |
| 45 | + }, |
| 46 | + }, |
| 47 | + additionalProperties: false, |
| 48 | + }, |
| 49 | + ], |
| 50 | + messages: { |
| 51 | + requireNewline: "'{{name}}' should be on a new line.", |
| 52 | + }, |
| 53 | + type: "layout", |
| 54 | + }, |
| 55 | + create(context) { |
| 56 | + const multilineMaximum = context.options[0]?.multiline ?? 1 |
| 57 | + const singlelineMaximum = context.options[0]?.singleline ?? 1 |
| 58 | + const sourceCode = context.getSourceCode() |
| 59 | + |
| 60 | + /** |
| 61 | + * Report attributes |
| 62 | + */ |
| 63 | + function report( |
| 64 | + attribute: AST.SvelteStartTag["attributes"][number] | undefined, |
| 65 | + ) { |
| 66 | + if (!attribute) { |
| 67 | + return |
| 68 | + } |
| 69 | + let name: string |
| 70 | + if ( |
| 71 | + attribute.type === "SvelteAttribute" || |
| 72 | + attribute.type === "SvelteShorthandAttribute" || |
| 73 | + attribute.type === "SvelteDirective" || |
| 74 | + attribute.type === "SvelteSpecialDirective" |
| 75 | + ) { |
| 76 | + name = sourceCode.text.slice(...attribute.key.range!) |
| 77 | + } else { |
| 78 | + // if (attribute.type === "SvelteSpreadAttribute") |
| 79 | + name = sourceCode.text.slice(...attribute.range) |
| 80 | + } |
| 81 | + context.report({ |
| 82 | + node: attribute, |
| 83 | + loc: attribute.loc, |
| 84 | + messageId: "requireNewline", |
| 85 | + data: { name }, |
| 86 | + fix(fixer) { |
| 87 | + // Find the closest token before the current attribute |
| 88 | + // that is not a white space |
| 89 | + const prevToken = sourceCode.getTokenBefore(attribute, { |
| 90 | + includeComments: true, |
| 91 | + })! |
| 92 | + |
| 93 | + const range: AST.Range = [prevToken.range[1], attribute.range[0]] |
| 94 | + |
| 95 | + return fixer.replaceTextRange(range, "\n") |
| 96 | + }, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + return { |
| 101 | + SvelteStartTag(node) { |
| 102 | + const numberOfAttributes = node.attributes.length |
| 103 | + |
| 104 | + if (!numberOfAttributes) return |
| 105 | + |
| 106 | + if (isSingleLine(node)) { |
| 107 | + if (numberOfAttributes > singlelineMaximum) { |
| 108 | + report(node.attributes[singlelineMaximum]) |
| 109 | + } |
| 110 | + } else { |
| 111 | + for (const attrs of groupAttributesByLine(node.attributes)) { |
| 112 | + report(attrs[multilineMaximum]) |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + } |
| 117 | + }, |
| 118 | +}) |
0 commit comments