Re: [csswg-drafts] [css-nesting-1] Syntax Invites Errors (#7834)

>  .bar & {}  /* 🤔 Wait, which mode am I in right now? */

To any CSS author that is clearly a rule, not a declaration, so I don't think that is confusing at all. We're saying now that non-idents, such a `.`, `#`, `>`, `+`, `:`, `&`, etc. would signify the beginning of a rule.

I agree with @LeaVerou and @fantasai that the reason we keep mentioning SASS (SCSS dialect) is not just author familiarity, but because it has good writing ergonomics, which compliment how people author CSS. It makes it really simple to write nested descendant rules and rules that start with other combinators, and the `&` character is easy to use for when you want to modify the subject selector in other ways (e.g. add a class to it, or prefix some other selectors to it). We should be aiming for something as close to this as we can, without boggling the parser or hurting parsing performance. Making everyone type a `&` or `@nest` before every selector is excessive, if we can avoid it. And we can.

I actually don't hate @LeaVerou's "crazy" idea. I could see that working. But I'd still rather just have that as the restriction on the first rule only, which is where we pretty much are at this point anyway. But it is a good framing: to disambiguate the beginning of nested rules from the declarations (which is the main reason to diverge from SCSS nesting at all), the first rule of the nest just can't start with a tag name selector. But it can start with a `&`, and every other kind of selector is fine. That isn't an unreasonable thing to remember, even when moving rules around or deleting them. Kind of like how you can omit the last semicolon of a rule, but if you move that rule or add one after it, you'll need to remember to put that semicolon in there after all. In this case, you'll need to remember that the first rule of the nest has to start with something other than a tag name. 

And if it helps your individual authoring style, precede all your tag-name-starting nested rules with a `&`. For the minority of rules that need the `&` elsewhere in the selector, and you don't have any other rules to put before it, I personally think preceding that selector (or the whole rule) with `@nest` is a little easier to read that wrapping the tag name with `:is()` but maybe that's just me. I could get used to either way.


-- 
GitHub Notification of comment by bradkemper
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/7834#issuecomment-1274197779 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 11 October 2022 07:16:50 UTC