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

Demo implementation of @csstools/selector-resolve-nested #7496

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

romainmenke
Copy link
Member

@romainmenke romainmenke commented Jan 24, 2024

Which issue, if any, is this issue related to?

See : #6234

Is there anything in the PR that needs further explanation?

I wanted to gain a better understanding of the complexity that CSS nesting introduces for all rules that analyse selectors.

I also want to validate how @csstools/selector-resolve-nested works to make sure it works correctly and its API has the right shape.

This is a draft PR as it isn't intended to be merged, but feedback is welcome!

Copy link

changeset-bot bot commented Jan 24, 2024

⚠️ No Changeset found

Latest commit: 5c8fda0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member Author

@romainmenke romainmenke left a comment

Choose a reason for hiding this comment

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

This currently errors, mostly because of : #7495

Comment on lines 36 to 71
function removeIgnoredNodes(selector, isIgnored) {
const clone = selector.clone();

clone.walk((node) => {
let value = '';

switch (node.type) {
case 'attribute':
case 'class':
case 'id':
case 'tag':
value = node.toString();
break;
case 'pseudo':
value = node.value;
break;

default:
return
}

if (!value) {
return;
}

if (isIgnored(value)) {
node.replaceWith(selectorParser.universal({
value: '*',
sourceIndex: node.sourceIndex,
source: node.source,
}));
}
});

return clone;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I removed all the custom logic for specificity and instead manipulated the AST so that ignored nodes were replaced with * which has zero specificity. This has obvious differences but I think it can be an interesting approach to ignoreSelector in this context.

*/
const specificitySum = (specificities) => {
const sum = zeroSpecificity();
function gatherSelectorsForRule(rule, result, isIgnored) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This should be a shared helper.

It walks the postcss AST to gather all the ancestor nodes and resolves all the nested selectors.

Comment on lines 123 to 133
if (typeGuards.isAtRule(child) && ast) {
// `.foo, #bar { @media screen { color: red; } }`
// equivalent to
// `@media screen { .foo, #bar { & { color: red; } } }`
// `@media screen { :is(.foo, #bar) { color: red; } }`
const childAST = selectorAST('&', result, child);

if (!childAST) return;

ast = selectorResolveNested.resolveNestedSelector(childAST, ast);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

for more info and demo's see : mrhenry/stylelint-mrhenry#66

lib/utils/selectorAST.mjs Outdated Show resolved Hide resolved
@jeddy3
Copy link
Member

jeddy3 commented Jan 28, 2024

This looks fantastic and is a significant step towards spec compliance!

Comment on lines +60 to +71
if (typeGuards.isAtRule(child) && ast) {
// `.foo, #bar { @media screen { color: red; } }`
// equivalent to
// `@media screen { .foo, #bar { & { color: red; } } }`
// `@media screen { :is(.foo, #bar) { color: red; } }`
const childAST = selectorAST('&', result, child);

if (!childAST) return;

ast = selectorResolveNested.resolveNestedSelector(childAST, ast);
}
}
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 : the specification has since changed again, this is no longer true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants