Skip to content

Commit b57c7f1

Browse files
authored
Fix false positives for component in prefer-style-directive rule (#174)
1 parent 15cb179 commit b57c7f1

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

src/rules/prefer-style-directive.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export default createRule("prefer-style-directive", {
222222
parent: AST.SvelteStartTag
223223
},
224224
) {
225-
if (node.key.name !== "style") {
225+
if (!isHTMLElement(node.parent.parent) || node.key.name !== "style") {
226226
return
227227
}
228228
const root = parseStyleAttributeValue(node, context)
@@ -231,5 +231,23 @@ export default createRule("prefer-style-directive", {
231231
}
232232
},
233233
}
234+
235+
/** Checks whether the given node is the html element node. */
236+
function isHTMLElement(
237+
node:
238+
| AST.SvelteElement
239+
| AST.SvelteScriptElement
240+
| AST.SvelteStyleElement,
241+
) {
242+
if (node.type === "SvelteElement") {
243+
if (node.kind === "html") {
244+
return true
245+
}
246+
if (node.kind === "special") {
247+
return node.name.name === "svelte:element"
248+
}
249+
}
250+
return false
251+
}
234252
},
235253
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Can use style directives instead.",
4+
"line": 5,
5+
"column": 42
6+
}
7+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let expression = "div"
3+
</script>
4+
5+
<svelte:element this={expression} style="display:block" />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let expression = "div"
3+
</script>
4+
5+
<svelte:element this={expression} style:display="block" />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import Component from "./Component.svelte"
3+
</script>
4+
5+
<Component style="color: red;" />
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
let foo = Math.random()
3+
</script>
4+
5+
<div>
6+
{#if foo > 0.5}
7+
<svelte:self style="display:block" />
8+
{/if}
9+
</div>

0 commit comments

Comments
 (0)