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

Conversation

tbashiyy
Copy link
Contributor

Motivation

When I use this rule for project which is using unocss, I want to add unocss's dynamic-rules to allowedClassNames
ex) /^m-(\d+)$/

Copy link

changeset-bot bot commented Jun 26, 2025

🦋 Changeset detected

Latest commit: 893b19f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tbashiyy tbashiyy changed the title feat: Support Regex for no-unused-class-names allowedClassNames feat: Support Regex for no-unused-class-name allowedClassNames option Jun 26, 2025
@baseballyama baseballyama self-assigned this Jun 26, 2025
Copy link
Contributor

github-actions bot commented Jun 26, 2025

Try the Instant Preview in Online Playground

ESLint Online Playground

Install the Instant Preview to Your Local

npm i https://pkg.pr.new/eslint-plugin-svelte@893b19f

Published Instant Preview Packages:

View Commit

@tbashiyy tbashiyy changed the title feat: Support Regex for no-unused-class-name allowedClassNames option feat(no-unused-class-name): Support Regex for allowedClassNames option Jun 26, 2025
Comment on lines 60 to 71
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

@tbashiyy tbashiyy requested a review from baseballyama June 26, 2025 12:30
Comment on lines 62 to 70
!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

'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!

@baseballyama
Copy link
Member

CI is failed.

@@ -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.

Copy link
Member

@baseballyama baseballyama left a comment

Choose a reason for hiding this comment

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

Thank you!

@baseballyama baseballyama merged commit e94a3be into sveltejs:main Jun 26, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants