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 no-descending-specificity false positives for pseudo-classes #6195

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions lib/rules/no-descending-specificity/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ testRule({
{
code: ':where(a :is(b, i)) {} :where(a) {}',
},
{
code: 'a b :global(.foo) {} a :global(.bar) {}',
},
{
code: ':global(.foo) {} :global { .bar {} }',
},
],

reject: [
Expand Down
49 changes: 32 additions & 17 deletions lib/rules/no-descending-specificity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const meta = {
url: 'https://stylelint.io/user-guide/rules/list/no-descending-specificity',
};

/** @typedef {{ selector: string, specificity: import('@csstools/selector-specificity').Specificity }} Entry */

/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
Expand Down Expand Up @@ -63,6 +65,7 @@ const rule = (primary, secondaryOptions) => {
return;
}

/** @type {Map<string, Entry[]>} */
const comparisonContext = selectorContextLookup.getContext(
ruleNode,
findAtRuleContext(ruleNode),
Expand Down Expand Up @@ -92,23 +95,24 @@ const rule = (primary, secondaryOptions) => {
/**
* @param {import('postcss-selector-parser').Root} selectorNode
* @param {import('postcss').Rule} ruleNode
* @param {Map<any, any>} comparisonContext
* @param {Map<string, Entry[]>} comparisonContext
*/
function checkSelector(selectorNode, ruleNode, comparisonContext) {
const selector = selectorNode.toString();
const referenceSelectorNode = lastCompoundSelectorWithoutPseudoClasses(selectorNode);
const referenceSelector = lastCompoundSelectorWithoutPseudoClasses(selectorNode);
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] lastCompoundSelectorWithoutPseudoClasses() returns a string, not a node. So I think referenceSelector is a more readable name.


if (referenceSelector === undefined) return;

const selectorSpecificity = calculate(selectorNode);
const entry = { selector, specificity: selectorSpecificity };
const priorComparableSelectors = comparisonContext.get(referenceSelector);

if (!comparisonContext.has(referenceSelectorNode)) {
comparisonContext.set(referenceSelectorNode, [entry]);
if (priorComparableSelectors === undefined) {
comparisonContext.set(referenceSelector, [entry]);

return;
}

/** @type {Array<typeof entry>} */
const priorComparableSelectors = comparisonContext.get(referenceSelectorNode);

for (const priorEntry of priorComparableSelectors) {
if (compare(selectorSpecificity, priorEntry.specificity) < 0) {
report({
Expand All @@ -128,6 +132,7 @@ const rule = (primary, secondaryOptions) => {

/**
* @param {import('postcss-selector-parser').Root} selectorNode
* @returns {string | undefined}
*/
function lastCompoundSelectorWithoutPseudoClasses(selectorNode) {
const firstChild = selectorNode.nodes[0];
Expand All @@ -137,17 +142,27 @@ function lastCompoundSelectorWithoutPseudoClasses(selectorNode) {
const nodesAfterLastCombinator = nodesByCombinator[nodesByCombinator.length - 1];

assert(nodesAfterLastCombinator);
const nodesWithoutPseudoClasses = nodesAfterLastCombinator
.filter((node) => {
return (
node.type !== 'pseudo' ||
node.value.startsWith('::') ||
pseudoElements.has(node.value.replace(/:/g, ''))
);
})
.join('');

return nodesWithoutPseudoClasses.toString();
const nodesWithoutPseudoClasses = nodesAfterLastCombinator.filter((node) => {
return (
isCssModulesGlobal(node) ||
node.type !== 'pseudo' ||
node.value.startsWith('::') ||
pseudoElements.has(node.value.replace(/:/g, ''))
);
});

if (nodesWithoutPseudoClasses.length === 0) return undefined;
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] If nodesWithoutPseudoClasses is empty, the old version of this function returns an empty string (''). This makes it more difficult to handle irregular values. So the current code returns undefined instead.


return nodesWithoutPseudoClasses.join('');
}

/**
* @param {import('postcss-selector-parser').Node} node
* @returns {boolean}
*/
function isCssModulesGlobal(node) {
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
return node.type === 'pseudo' && node.value === ':global';
}

rule.ruleName = ruleName;
Expand Down