Skip to content

fix(no-unused-class-name): detect duplicated class names #1260

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-colts-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix(no-unused-class-name): detect duplicated class names
12 changes: 8 additions & 4 deletions packages/eslint-plugin-svelte/src/rules/no-unused-class-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export default createRule('no-unused-class-name', {
return {};
}
const allowedClassNames = context.options[0]?.allowedClassNames ?? [];
const classesUsedInTemplate: Record<string, AST.SourceLocation> = {};
const classesUsedInTemplate: {
className: string;
loc: AST.SourceLocation;
}[] = [];
Comment on lines +39 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better in terms of performance?

Suggested change
const classesUsedInTemplate: {
className: string;
loc: AST.SourceLocation;
}[] = [];
const classesUsedInTemplate: Record<string, AST.SourceLocation[] = {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think performance is no changes for now.
Because this code just execute by loop. (No access by key.)

If data structure is changed, I think code will be complex a little for now.


return {
SvelteElement(node) {
Expand All @@ -45,7 +48,7 @@ export default createRule('no-unused-class-name', {
}
const classes = node.startTag.attributes.flatMap(findClassesInAttribute);
for (const className of classes) {
classesUsedInTemplate[className] = node.startTag.loc;
classesUsedInTemplate.push({ className, loc: node.startTag.loc });
}
},
'Program:exit'() {
Expand All @@ -57,15 +60,16 @@ export default createRule('no-unused-class-name', {
styleContext.status === 'success'
? findClassesInPostCSSNode(styleContext.sourceAst, sourceCode.parserServices)
: [];
for (const className in classesUsedInTemplate) {

for (const { className, loc } of classesUsedInTemplate) {
if (
!allowedClassNames.some((allowedClassName: string) =>
toRegExp(allowedClassName).test(className)
) &&
!classesUsedInStyle.includes(className)
) {
context.report({
loc: classesUsedInTemplate[className],
loc,
message: `Unused class "${className}".`
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- message: Unused class "div-class".
line: 1
column: 1
suggestions: null
- message: Unused class "div-class".
line: 3
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="div-class">Hello</div>

<span class="div-class">World!</span>
Loading