Skip to content

Commit 612a2dc

Browse files
committed
feat(no-unused-class-name): added support for class directives
1 parent 60e84ca commit 612a2dc

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/rules/no-unused-class-name.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
SourceLocation,
44
SvelteAttribute,
55
SvelteDirective,
6-
SvelteLiteral,
76
SvelteShorthandAttribute,
87
SvelteSpecialDirective,
98
SvelteSpreadAttribute,
@@ -74,20 +73,15 @@ function findClassesInAttribute(
7473
| SvelteStyleDirective
7574
| SvelteSpecialDirective,
7675
): string[] {
77-
// TODO: This only supports direct class attribute - what about shorthands, spread directives etc.
78-
if (
79-
!("key" in attribute) ||
80-
!("name" in attribute.key) ||
81-
!("value" in attribute) ||
82-
attribute.key.name !== "class"
83-
) {
84-
return []
76+
if (attribute.type === "SvelteAttribute" && attribute.key.name === "class") {
77+
return attribute.value.flatMap((value) =>
78+
value.type === "SvelteLiteral" ? value.value.split(" ") : [],
79+
)
80+
}
81+
if (attribute.type === "SvelteDirective" && attribute.kind === "Class") {
82+
return [attribute.key.name.name]
8583
}
86-
// TODO: Why multiple values?
87-
// TODO: Remove assertions
88-
return ((attribute as SvelteAttribute).value[0] as SvelteLiteral).value.split(
89-
" ",
90-
)
84+
return []
9185
}
9286

9387
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- message: Unused class "first".
2+
line: 1
3+
column: 1
4+
suggestions: null
5+
- message: Unused class "second".
6+
line: 3
7+
column: 1
8+
suggestions: null
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class:first={true}>Hello</div>
2+
3+
<span class:second={false}>World!</span>

0 commit comments

Comments
 (0)