Skip to content

feat(no-unused-class-name): Support Regex for allowedClassNames option #1257

New issue

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

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

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 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/fast-bottles-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

feat: support regex for allowedClassNames of no-unused-class-name rule
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 @@ -57,7 +57,18 @@ 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) => {
const regex = /^\/(.*)\/$/.exec(allowedClassName);
if (regex == null || regex[1] == null) {
return false;
}

return new RegExp(regex[1]).test(className);
}) &&
!classesUsedInStyle.includes(className)
) {
Copy link
Member

Choose a reason for hiding this comment

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

Please use isRegExp from utils/regexp.js to check the className is regexp or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks!

fixed. e4c983b

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>