Skip to content

Commit

Permalink
Fix selector-type-case performance (#7041)
Browse files Browse the repository at this point in the history
* Fix `selector-type-case` performance

* Create friendly-candles-deny.md

* update STARTS_A_TAG_NAME_REGEX

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>

---------

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
romainmenke and ybiquitous committed Jul 4, 2023
1 parent f82a24a commit b8e5317
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
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

0 comments on commit b8e5317

Please sign in to comment.