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