|
| 1 | +--- |
| 2 | +pageClass: "rule-details" |
| 3 | +sidebarDepth: 0 |
| 4 | +title: "@ota-meshi/svelte/require-optimized-style-attribute" |
| 5 | +description: "require style attributes that can be optimized" |
| 6 | +--- |
| 7 | + |
| 8 | +# @ota-meshi/svelte/require-optimized-style-attribute |
| 9 | + |
| 10 | +> require style attributes that can be optimized |
| 11 | +
|
| 12 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> |
| 13 | + |
| 14 | +## :book: Rule Details |
| 15 | + |
| 16 | +This rule reports `style` attributes written in a format that cannot be optimized. |
| 17 | + |
| 18 | +Svelte parses the content written in the style attribute and tries to optimize it. (See [https://github.com/sveltejs/svelte/pull/810](https://github.com/sveltejs/svelte/pull/810)) |
| 19 | +If Svelte can be successfully optimized, Svelte can minimize the number of re-renders. |
| 20 | + |
| 21 | +e.g. |
| 22 | + |
| 23 | +template: |
| 24 | + |
| 25 | +```html |
| 26 | +<div |
| 27 | + style=" |
| 28 | + font-size: 12px; |
| 29 | + color: {color}; |
| 30 | + transform: translate({x}px, {y}px); |
| 31 | + " |
| 32 | +/> |
| 33 | +``` |
| 34 | + |
| 35 | +compiled: |
| 36 | + |
| 37 | +```js |
| 38 | +div.style.setProperty("font-size", "12px") // font-size style is not updated once it is initially set. |
| 39 | +div.style.setProperty("color", color) // color style is updated only when color variable is updated. |
| 40 | +div.style.setProperty("transform", `translate(${x}px, ${y}px)`) // transform style is updated only when x, or y variables are updated. |
| 41 | +``` |
| 42 | + |
| 43 | +However, if the optimization fails, it will be re-rendered triggered by the update of all variables described in the style attribute. |
| 44 | + |
| 45 | +e.g. |
| 46 | + |
| 47 | +template: |
| 48 | + |
| 49 | +```html |
| 50 | +<script> |
| 51 | + $: transformStyle = `transform: translate(${x}px, ${y}px)` |
| 52 | +</script> |
| 53 | + |
| 54 | +<div |
| 55 | + style=" |
| 56 | + font-size: 12px; |
| 57 | + color: {color}; |
| 58 | + {transformStyle} |
| 59 | + " |
| 60 | +/> |
| 61 | +``` |
| 62 | + |
| 63 | +compiled: |
| 64 | + |
| 65 | +```js |
| 66 | +// If any of variables color, x, or y are updated, all styles will be updated. |
| 67 | +div.setAttribute( |
| 68 | + "style", |
| 69 | + `font-size: 12px; color: ${color}; ${/* transformStyle */ ctx[0]}`, |
| 70 | +) |
| 71 | +``` |
| 72 | + |
| 73 | +Examples: |
| 74 | + |
| 75 | +<ESLintCodeBlock> |
| 76 | + |
| 77 | +<!--eslint-skip--> |
| 78 | + |
| 79 | +```svelte |
| 80 | +<script> |
| 81 | + /* eslint @ota-meshi/svelte/no-non-optimized-style-attributes: "error" */ |
| 82 | + let color = "blue" |
| 83 | + let x = 12, |
| 84 | + y = 12 |
| 85 | +</script> |
| 86 | +
|
| 87 | +<!-- ✓ GOOD --> |
| 88 | +<div |
| 89 | + style="font-size: 12px; color: {color}; transform: translate({x}px, {y}px);" |
| 90 | +/> |
| 91 | +<div style:pointer-events={pointerEvents ? null : "none"} /> |
| 92 | +
|
| 93 | +<!-- ✗ BAD --> |
| 94 | +<div style="font-size: 12px; color: {color}; {transformStyle}" /> |
| 95 | +<div style={pointerEvents === false ? "pointer-events:none;" : ""} /> |
| 96 | +
|
| 97 | +<div style="font-size: 12px; /* comment */ color: {color};" /> |
| 98 | +<div style="font-size: 12px; {key}: red;" /> |
| 99 | +``` |
| 100 | + |
| 101 | +</ESLintCodeBlock> |
| 102 | + |
| 103 | +## :wrench: Options |
| 104 | + |
| 105 | +Nothing. |
| 106 | + |
| 107 | +## :mag: Implementation |
| 108 | + |
| 109 | +- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/require-optimized-style-attribute.ts) |
| 110 | +- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/require-optimized-style-attribute.ts) |
0 commit comments