Skip to content

Commit

Permalink
Fix selector-type-no-unknown false positives for idents in function…
Browse files Browse the repository at this point in the history
…al pseudo-classes (#8191)
  • Loading branch information
elskhn authored Dec 14, 2024
1 parent b988d34 commit 6405840
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-weeks-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `selector-type-no-unknown` false positives for idents in functional pseudo-classes
3 changes: 3 additions & 0 deletions lib/rules/selector-type-no-unknown/__tests__/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ testRule({
{
code: '::view-transition-group(qux) {}',
},
{
code: ':active-view-transition-type(foo) {}',
},
{
code: 'input:not(*) {}',
},
Expand Down
19 changes: 18 additions & 1 deletion lib/rules/selector-type-no-unknown/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const IGNORED_PSEUDO_ELEMENTS = new Set([
'::view-transition-old',
]);

const IGNORED_PSEUDO_CLASSES = new Set([
':active-view-transition-type',
':dir',
':-moz-locale-dir',
':state',
':lang',
]);

/** @type {import('stylelint').CoreRules[ruleName]} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
Expand Down Expand Up @@ -89,7 +97,9 @@ const rule = (primary, secondaryOptions) => {

if (optionsMatches(secondaryOptions, 'ignoreTypes', tagNode.value)) return;

if (isArgumentOfIgnoredPseudoElement(tagNode)) return;
if (isArgumentOfIgnoredPseudoElement(tagNode) || isArgumentOfIgnoredPseudoClass(tagNode)) {
return;
}

const tagName = tagNode.value;
const tagNameLowerCase = tagName.toLowerCase();
Expand Down Expand Up @@ -127,6 +137,13 @@ function isArgumentOfIgnoredPseudoElement(tag) {
return selectorParser.isPseudoElement(node) && IGNORED_PSEUDO_ELEMENTS.has(node.value);
}

/** @param {import('postcss-selector-parser').Tag} tag */
function isArgumentOfIgnoredPseudoClass(tag) {
const node = tag.parent?.parent;

return selectorParser.isPseudoClass(node) && IGNORED_PSEUDO_CLASSES.has(node.value);
}

rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
Expand Down
19 changes: 18 additions & 1 deletion lib/rules/selector-type-no-unknown/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ const IGNORED_PSEUDO_ELEMENTS = new Set([
'::view-transition-old',
]);

const IGNORED_PSEUDO_CLASSES = new Set([
':active-view-transition-type',
':dir',
':-moz-locale-dir',
':state',
':lang',
]);

/** @type {import('stylelint').CoreRules[ruleName]} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
Expand Down Expand Up @@ -86,7 +94,9 @@ const rule = (primary, secondaryOptions) => {

if (optionsMatches(secondaryOptions, 'ignoreTypes', tagNode.value)) return;

if (isArgumentOfIgnoredPseudoElement(tagNode)) return;
if (isArgumentOfIgnoredPseudoElement(tagNode) || isArgumentOfIgnoredPseudoClass(tagNode)) {
return;
}

const tagName = tagNode.value;
const tagNameLowerCase = tagName.toLowerCase();
Expand Down Expand Up @@ -124,6 +134,13 @@ function isArgumentOfIgnoredPseudoElement(tag) {
return parser.isPseudoElement(node) && IGNORED_PSEUDO_ELEMENTS.has(node.value);
}

/** @param {import('postcss-selector-parser').Tag} tag */
function isArgumentOfIgnoredPseudoClass(tag) {
const node = tag.parent?.parent;

return parser.isPseudoClass(node) && IGNORED_PSEUDO_CLASSES.has(node.value);
}

rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
Expand Down

0 comments on commit 6405840

Please sign in to comment.