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 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/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
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
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 { toRegExp } from '../utils/regexp.js';

export default createRule('no-unused-class-name', {
meta: {
Expand Down Expand Up @@ -57,7 +58,12 @@ 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.some((allowedClassName: string) =>
toRegExp(allowedClassName).test(className)
) &&
!classesUsedInStyle.includes(className)
) {
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>
Loading