|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import { createRule } from "../utils" |
| 3 | +import type { SvelteStyleInlineRoot, SvelteStyleRoot } from "../utils/css-utils" |
| 4 | +import { parseStyleAttributeValue } from "../utils/css-utils" |
| 5 | + |
| 6 | +export default createRule("no-dupe-style-properties", { |
| 7 | + meta: { |
| 8 | + docs: { |
| 9 | + description: "disallow duplicate style properties", |
| 10 | + category: "Possible Errors", |
| 11 | + recommended: true, |
| 12 | + }, |
| 13 | + schema: [], |
| 14 | + messages: { |
| 15 | + unexpected: "Duplicate property '{{name}}'.", |
| 16 | + }, |
| 17 | + type: "problem", |
| 18 | + }, |
| 19 | + create(context) { |
| 20 | + type StyleDecl = { |
| 21 | + prop: string |
| 22 | + loc: AST.SourceLocation |
| 23 | + } |
| 24 | + type StyleDeclSet = { |
| 25 | + decls: StyleDecl[] |
| 26 | + } |
| 27 | + |
| 28 | + return { |
| 29 | + SvelteStartTag(node: AST.SvelteStartTag) { |
| 30 | + const reported = new Set<StyleDecl>() |
| 31 | + const beforeDeclarations = new Map<string, StyleDecl>() |
| 32 | + for (const { decls } of iterateStyleDeclSetFromAttrs(node.attributes)) { |
| 33 | + for (const decl of decls) { |
| 34 | + const already = beforeDeclarations.get(decl.prop) |
| 35 | + if (already) { |
| 36 | + for (const report of [already, decl].filter( |
| 37 | + (n) => !reported.has(n), |
| 38 | + )) { |
| 39 | + context.report({ |
| 40 | + node, |
| 41 | + loc: report.loc, |
| 42 | + messageId: "unexpected", |
| 43 | + data: { name: report.prop }, |
| 44 | + }) |
| 45 | + reported.add(report) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + for (const decl of decls) { |
| 50 | + beforeDeclarations.set(decl.prop, decl) |
| 51 | + } |
| 52 | + } |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + /** Iterate the style decl set from attrs */ |
| 57 | + function* iterateStyleDeclSetFromAttrs( |
| 58 | + attrs: AST.SvelteStartTag["attributes"], |
| 59 | + ): Iterable<StyleDeclSet> { |
| 60 | + for (const attr of attrs) { |
| 61 | + if (attr.type === "SvelteStyleDirective") { |
| 62 | + yield { |
| 63 | + decls: [{ prop: attr.key.name.name, loc: attr.key.name.loc! }], |
| 64 | + } |
| 65 | + } else if (attr.type === "SvelteAttribute") { |
| 66 | + if (attr.key.name !== "style") { |
| 67 | + continue |
| 68 | + } |
| 69 | + const root = parseStyleAttributeValue(attr, context) |
| 70 | + if (!root) { |
| 71 | + continue |
| 72 | + } |
| 73 | + yield* iterateStyleDeclSetFromStyleRoot(root) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** Iterate the style decl set from style root */ |
| 79 | + function* iterateStyleDeclSetFromStyleRoot( |
| 80 | + root: SvelteStyleRoot | SvelteStyleInlineRoot, |
| 81 | + ): Iterable<StyleDeclSet> { |
| 82 | + for (const child of root.nodes) { |
| 83 | + if (child.type === "decl") { |
| 84 | + yield { |
| 85 | + decls: [ |
| 86 | + { |
| 87 | + prop: child.prop.name, |
| 88 | + get loc() { |
| 89 | + return child.prop.loc |
| 90 | + }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + } |
| 94 | + } else if (child.type === "inline") { |
| 95 | + const decls: StyleDecl[] = [] |
| 96 | + for (const root of child.getAllInlineStyles().values()) { |
| 97 | + for (const set of iterateStyleDeclSetFromStyleRoot(root)) { |
| 98 | + decls.push(...set.decls) |
| 99 | + } |
| 100 | + } |
| 101 | + yield { decls } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + }, |
| 106 | +}) |
0 commit comments