Skip to content
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

Fix selector-type-case performance #7041

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/friendly-candles-deny.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `selector-type-case` performance
17 changes: 13 additions & 4 deletions lib/rules/selector-type-case/index.js
Expand Up @@ -22,6 +22,10 @@ const meta = {
fixable: true,
};

const STARTS_A_TAG_NAME_REGEX = /(?:[^.#[:a-zA-Z-]|^)[a-zA-Z]/;
const ANY_UPPER_CASE_REGEX = /[A-Z]/;
const ANY_LOWER_CASE_REGEX = /[a-z]/;

/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions, context) => {
return (root, result) => {
Expand All @@ -48,11 +52,16 @@ const rule = (primary, secondaryOptions, context) => {
root.walkRules((ruleNode) => {
let hasComments = ruleNode.raws.selector && ruleNode.raws.selector.raw;
const selector = hasComments ? hasComments : ruleNode.selector;
const selectors = ruleNode.selectors;

if (!isStandardSyntaxRule(ruleNode)) {
return;
}
if (!STARTS_A_TAG_NAME_REGEX.test(selector)) return;

if (primary === 'lower' && !ANY_UPPER_CASE_REGEX.test(selector)) return;

if (primary === 'upper' && !ANY_LOWER_CASE_REGEX.test(selector)) return;

if (!isStandardSyntaxRule(ruleNode)) return;

const { selectors } = ruleNode;

if (selectors.some((s) => isKeyframeSelector(s))) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/selector-type-no-unknown/index.js
Expand Up @@ -24,7 +24,7 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/selector-type-no-unknown',
};

const STARTS_A_TAG_NAME_REGEX = /(?:[^.#[:\w-]|^)\w/;
const STARTS_A_TAG_NAME_REGEX = /(?:[^.#[:a-zA-Z-]|^)[a-zA-Z]/;

/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
Expand Down