From b8e53176d42b496f84eaa520d9067c87e9080469 Mon Sep 17 00:00:00 2001 From: Romain Menke <11521496+romainmenke@users.noreply.github.com> Date: Tue, 4 Jul 2023 14:42:17 +0200 Subject: [PATCH] Fix `selector-type-case` performance (#7041) * 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> --- .changeset/friendly-candles-deny.md | 5 +++++ lib/rules/selector-type-case/index.js | 17 +++++++++++++---- lib/rules/selector-type-no-unknown/index.js | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 .changeset/friendly-candles-deny.md diff --git a/.changeset/friendly-candles-deny.md b/.changeset/friendly-candles-deny.md new file mode 100644 index 0000000000..60973810d9 --- /dev/null +++ b/.changeset/friendly-candles-deny.md @@ -0,0 +1,5 @@ +--- +"stylelint": patch +--- + +Fixed: `selector-type-case` performance diff --git a/lib/rules/selector-type-case/index.js b/lib/rules/selector-type-case/index.js index c2fae483d6..58aa4c7e78 100644 --- a/lib/rules/selector-type-case/index.js +++ b/lib/rules/selector-type-case/index.js @@ -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) => { @@ -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; diff --git a/lib/rules/selector-type-no-unknown/index.js b/lib/rules/selector-type-no-unknown/index.js index bb43695772..aa157cdde5 100644 --- a/lib/rules/selector-type-no-unknown/index.js +++ b/lib/rules/selector-type-no-unknown/index.js @@ -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) => {