-
-
Notifications
You must be signed in to change notification settings - Fork 929
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
ybiquitous
merged 4 commits into
main
from
fix-no-descending-specificity-false-positives
Jul 28, 2022
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
13f7303
Fix `no-descending-specificity` false positives for `:global()` pseud…
ybiquitous 423095a
Add test case about nested `:global` selector
ybiquitous 7b93cc0
Merge branch 'main' into fix-no-descending-specificity-false-positives
ybiquitous d68bcaa
Remove CSS Modules specific code
ybiquitous File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
|
@@ -63,6 +65,7 @@ const rule = (primary, secondaryOptions) => { | |
return; | ||
} | ||
|
||
/** @type {Map<string, Entry[]>} */ | ||
const comparisonContext = selectorContextLookup.getContext( | ||
ruleNode, | ||
findAtRuleContext(ruleNode), | ||
|
@@ -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); | ||
|
||
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({ | ||
|
@@ -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]; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] If |
||
|
||
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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thinkreferenceSelector
is a more readable name.