|
| 1 | +import { createRule } from "../utils" |
| 2 | +import type { |
| 3 | + SourceLocation, |
| 4 | + SvelteAttribute, |
| 5 | + SvelteDirective, |
| 6 | + SvelteShorthandAttribute, |
| 7 | + SvelteSpecialDirective, |
| 8 | + SvelteSpreadAttribute, |
| 9 | + SvelteStyleDirective, |
| 10 | +} from "svelte-eslint-parser/lib/ast" |
| 11 | +import type { AnyNode } from "postcss" |
| 12 | +import { |
| 13 | + default as selectorParser, |
| 14 | + type Node as SelectorNode, |
| 15 | +} from "postcss-selector-parser" |
| 16 | + |
| 17 | +export default createRule("no-unused-class-name", { |
| 18 | + meta: { |
| 19 | + docs: { |
| 20 | + description: |
| 21 | + "disallow the use of a class in the template without a corresponding style", |
| 22 | + category: "Best Practices", |
| 23 | + recommended: false, |
| 24 | + }, |
| 25 | + schema: [], |
| 26 | + messages: {}, |
| 27 | + type: "suggestion", |
| 28 | + }, |
| 29 | + create(context) { |
| 30 | + const classesUsedInTemplate: Record<string, SourceLocation> = {} |
| 31 | + |
| 32 | + return { |
| 33 | + SvelteElement(node) { |
| 34 | + if (node.kind !== "html") { |
| 35 | + return |
| 36 | + } |
| 37 | + const classes = node.startTag.attributes.flatMap(findClassesInAttribute) |
| 38 | + for (const className of classes) { |
| 39 | + classesUsedInTemplate[className] = node.startTag.loc |
| 40 | + } |
| 41 | + }, |
| 42 | + "Program:exit"() { |
| 43 | + const styleContext = context.parserServices.getStyleContext() |
| 44 | + if (["parse-error", "unknown-lang"].includes(styleContext.status)) { |
| 45 | + return |
| 46 | + } |
| 47 | + const classesUsedInStyle = |
| 48 | + styleContext.sourceAst != null |
| 49 | + ? findClassesInPostCSSNode(styleContext.sourceAst) |
| 50 | + : [] |
| 51 | + for (const className in classesUsedInTemplate) { |
| 52 | + if (!classesUsedInStyle.includes(className)) { |
| 53 | + context.report({ |
| 54 | + loc: classesUsedInTemplate[className], |
| 55 | + message: `Unused class "${className}".`, |
| 56 | + }) |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + } |
| 61 | + }, |
| 62 | +}) |
| 63 | + |
| 64 | +/** |
| 65 | + * Extract all class names used in a HTML element attribute. |
| 66 | + */ |
| 67 | +function findClassesInAttribute( |
| 68 | + attribute: |
| 69 | + | SvelteAttribute |
| 70 | + | SvelteShorthandAttribute |
| 71 | + | SvelteSpreadAttribute |
| 72 | + | SvelteDirective |
| 73 | + | SvelteStyleDirective |
| 74 | + | SvelteSpecialDirective, |
| 75 | +): string[] { |
| 76 | + if (attribute.type === "SvelteAttribute" && attribute.key.name === "class") { |
| 77 | + return attribute.value.flatMap((value) => |
| 78 | + value.type === "SvelteLiteral" ? value.value.trim().split(/\s+/u) : [], |
| 79 | + ) |
| 80 | + } |
| 81 | + if (attribute.type === "SvelteDirective" && attribute.kind === "Class") { |
| 82 | + return [attribute.key.name.name] |
| 83 | + } |
| 84 | + return [] |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Extract all class names used in a PostCSS node. |
| 89 | + */ |
| 90 | +function findClassesInPostCSSNode(node: AnyNode): string[] { |
| 91 | + if (node.type === "rule") { |
| 92 | + let classes = node.nodes.flatMap(findClassesInPostCSSNode) |
| 93 | + const processor = selectorParser() |
| 94 | + classes = classes.concat( |
| 95 | + findClassesInSelector(processor.astSync(node.selector)), |
| 96 | + ) |
| 97 | + return classes |
| 98 | + } |
| 99 | + if (node.type === "root" || node.type === "atrule") { |
| 100 | + return node.nodes.flatMap(findClassesInPostCSSNode) |
| 101 | + } |
| 102 | + return [] |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Extract all class names used in a PostCSS selector. |
| 107 | + */ |
| 108 | +function findClassesInSelector(node: SelectorNode): string[] { |
| 109 | + if (node.type === "class") { |
| 110 | + return [node.value] |
| 111 | + } |
| 112 | + if ( |
| 113 | + node.type === "pseudo" || |
| 114 | + node.type === "root" || |
| 115 | + node.type === "selector" |
| 116 | + ) { |
| 117 | + return node.nodes.flatMap(findClassesInSelector) |
| 118 | + } |
| 119 | + return [] |
| 120 | +} |
0 commit comments