Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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/forty-signs-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

feat(no-unused-class-name): Support Regex for allowedClassNames option
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
feat(no-unused-class-name): Support Regex for allowedClassNames option
feat(no-unused-class-name): support regex for `allowedClassNames` option

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 got it!

2 changes: 1 addition & 1 deletion docs/rules/no-unused-class-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ This rule is aimed at reducing unused classes in the HTML template. While `svelt
"svelte/no-unused-class-name": [
"error",
{
"allowedClassNames": ["class-name-one", "class-name-two"]
"allowedClassNames": ["class-name-one", "class-name-two", "/^regex-.*$/"] // You can also use regex to match class names
}
]
}
Expand Down
13 changes: 12 additions & 1 deletion packages/eslint-plugin-svelte/src/rules/no-unused-class-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { AnyNode } from 'postcss';
import type { Node as SelectorNode } from 'postcss-selector-parser';
import { findClassesInAttribute } from '../utils/ast-utils.js';
import type { SourceCode } from '../types.js';
import { isRegExp, toRegExp } from 'src/utils/regexp.js';

export default createRule('no-unused-class-name', {
meta: {
Expand Down Expand Up @@ -57,7 +58,17 @@ export default createRule('no-unused-class-name', {
? findClassesInPostCSSNode(styleContext.sourceAst, sourceCode.parserServices)
: [];
for (const className in classesUsedInTemplate) {
if (!allowedClassNames.includes(className) && !classesUsedInStyle.includes(className)) {
if (
!allowedClassNames.includes(className) &&
!allowedClassNames.some((allowedClassName: string) => {
if (!isRegExp(allowedClassName)) {
return false;
}

return toRegExp(allowedClassName).test(className);
}) &&
!classesUsedInStyle.includes(className)
Copy link
Member

@baseballyama baseballyama Jun 26, 2025

Choose a reason for hiding this comment

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

I think this is simpler?

Suggested change
!allowedClassNames.includes(className) &&
!allowedClassNames.some((allowedClassName: string) => {
if (!isRegExp(allowedClassName)) {
return false;
}
return toRegExp(allowedClassName).test(className);
}) &&
!classesUsedInStyle.includes(className)
allowedClassNames.none((allowedClassName) => {
if (isRegExp(allowedClassName)) {
return toRegExp(allowedClassName).test(className);
}
return allowedClassName === className;
}) &&
!classesUsedInStyle.includes(className)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think this code is enough. (All Test cases passed)

06cbf3f

) {
context.report({
loc: classesUsedInTemplate[className],
message: `Unused class "${className}".`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"options": [{ "allowedClassNames": ["div-class"] }]
"options": [{ "allowedClassNames": ["div-class", "/^p-\\d{1,2}$/"] }]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
line: 3
column: 1
suggestions: null
- message: Unused class "p-100".
line: 5
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div class="div-class">Hello</div>

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

<span class="p-100">Regex!</span>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"options": [{ "allowedClassNames": ["div-class", "span-class"] }]
"options": [{ "allowedClassNames": ["div-class", "span-class", "/^p-\\d{1,2}$/"] }]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div class="div-class">Hello</div>

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

<span class="p-2">Regex!</span>