Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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): fix cannnot detect duplicated class name
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