Skip to content

Commit

Permalink
Fix selector-anb-no-unmatchable performance (#6925)
Browse files Browse the repository at this point in the history
* Fix `selector-anb-no-unmatchable` performance

* Update lib/utils/hasANPlusBNotationPseudoClasses.js

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

* Create modern-numbers-impress.md

---------

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
romainmenke and ybiquitous committed Jun 17, 2023
1 parent f72a6a9 commit 4e3abf7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-numbers-impress.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `selector-anb-no-unmatchable` performance
3 changes: 3 additions & 0 deletions lib/rules/selector-anb-no-unmatchable/index.js
Expand Up @@ -10,6 +10,7 @@ const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const hasANPlusBNotationPseudoClasses = require('../../utils/hasANPlusBNotationPseudoClasses');

const ruleName = 'selector-anb-no-unmatchable';

Expand Down Expand Up @@ -50,6 +51,8 @@ const rule = (primary) => {
}

ruleNode.selectors.forEach((selector) => {
if (!hasANPlusBNotationPseudoClasses(selector)) return;

let cssTreeSelector;

try {
Expand Down
22 changes: 22 additions & 0 deletions lib/utils/hasANPlusBNotationPseudoClasses.js
@@ -0,0 +1,22 @@
'use strict';

const {
aNPlusBNotationPseudoClasses,
aNPlusBOfSNotationPseudoClasses,
} = require('../reference/selectors');

const classes = [
...aNPlusBNotationPseudoClasses.values(),
...aNPlusBOfSNotationPseudoClasses.values(),
].join('|');
const HAS_A_N_PLUS_B_NOTATION_PSEUDO_CLASSES = new RegExp(`\\b:(?:${classes})\\(`, 'i');

/**
* Check if a selector contains any pseudo class function that might contain an An+B notation
*
* @param {string} selector
* @returns {boolean}
*/
module.exports = function hasANPlusBNotationPseudoClasses(selector) {
return HAS_A_N_PLUS_B_NOTATION_PSEUDO_CLASSES.test(selector);
};

0 comments on commit 4e3abf7

Please sign in to comment.