Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 'src/utils/regexp.js';
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 relative path to specify the file.


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