|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import { parse as parseCss } from "postcss" |
| 3 | +import { createRule } from "../utils" |
| 4 | + |
| 5 | +export default createRule("prefer-style-directive", { |
| 6 | + meta: { |
| 7 | + docs: { |
| 8 | + description: "require style directives instead of style attribute", |
| 9 | + category: "Stylistic Issues", |
| 10 | + recommended: false, |
| 11 | + }, |
| 12 | + fixable: "code", |
| 13 | + schema: [], |
| 14 | + messages: { |
| 15 | + unexpected: "Can use style directives instead.", |
| 16 | + }, |
| 17 | + type: "suggestion", |
| 18 | + }, |
| 19 | + create(context) { |
| 20 | + const sourceCode = context.getSourceCode() |
| 21 | + return { |
| 22 | + "SvelteStartTag > SvelteAttribute"( |
| 23 | + node: AST.SvelteAttribute & { |
| 24 | + parent: AST.SvelteStartTag |
| 25 | + }, |
| 26 | + ) { |
| 27 | + if (node.key.name !== "style") { |
| 28 | + return |
| 29 | + } |
| 30 | + const mustacheTags = node.value.filter( |
| 31 | + (v) => v.type === "SvelteMustacheTag", |
| 32 | + ) |
| 33 | + const valueStartIndex = node.value[0].range[0] |
| 34 | + const cssCode = node.value |
| 35 | + .map((value) => { |
| 36 | + if (value.type === "SvelteMustacheTag") { |
| 37 | + return "_".repeat(value.range[1] - value.range[0]) |
| 38 | + } |
| 39 | + return sourceCode.getText(value) |
| 40 | + }) |
| 41 | + .join("") |
| 42 | + const root = parseCss(cssCode) |
| 43 | + root.walkDecls((decl) => { |
| 44 | + if ( |
| 45 | + node.parent.attributes.some( |
| 46 | + (attr) => |
| 47 | + attr.type === "SvelteStyleDirective" && |
| 48 | + attr.key.name.name === decl.prop, |
| 49 | + ) |
| 50 | + ) { |
| 51 | + // has style directive |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const declRange: AST.Range = [ |
| 56 | + valueStartIndex + decl.source!.start!.offset, |
| 57 | + valueStartIndex + decl.source!.end!.offset + 1, |
| 58 | + ] |
| 59 | + if ( |
| 60 | + mustacheTags.some( |
| 61 | + (tag) => |
| 62 | + (tag.range[0] < declRange[0] && declRange[0] < tag.range[1]) || |
| 63 | + (tag.range[0] < declRange[1] && declRange[1] < tag.range[1]), |
| 64 | + ) |
| 65 | + ) { |
| 66 | + // intersection |
| 67 | + return |
| 68 | + } |
| 69 | + const declValueStartIndex = |
| 70 | + declRange[0] + decl.prop.length + (decl.raws.between || "").length |
| 71 | + const declValueRange: AST.Range = [ |
| 72 | + declValueStartIndex, |
| 73 | + declValueStartIndex + (decl.raws.value?.value || decl.value).length, |
| 74 | + ] |
| 75 | + |
| 76 | + context.report({ |
| 77 | + node, |
| 78 | + messageId: "unexpected", |
| 79 | + *fix(fixer) { |
| 80 | + const styleDirective = `style:${ |
| 81 | + decl.prop |
| 82 | + }="${sourceCode.text.slice(...declValueRange)}"` |
| 83 | + if (root.nodes.length === 1 && root.nodes[0] === decl) { |
| 84 | + yield fixer.replaceTextRange(node.range, styleDirective) |
| 85 | + } else { |
| 86 | + yield fixer.removeRange(declRange) |
| 87 | + yield fixer.insertTextAfterRange( |
| 88 | + node.range, |
| 89 | + ` ${styleDirective}`, |
| 90 | + ) |
| 91 | + } |
| 92 | + }, |
| 93 | + }) |
| 94 | + }) |
| 95 | + }, |
| 96 | + } |
| 97 | + }, |
| 98 | +}) |
0 commit comments