Skip to content

Keep all parent selectors inside :not() - #179

Open
wahidrizka wants to merge 1 commit into
postcss:mainfrom
wahidrizka:fix-not-ampersand
Open

wahidrizka wants to merge 1 commit into
postcss:mainfrom
wahidrizka:fix-not-ampersand

Conversation

@wahidrizka

@wahidrizka wahidrizka commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

As discussed in #176.

With a selector list as the parent, & inside :not() was replaced with one parent selector per rule, which turns "neither .a nor .b" into "not .a, or not .b":

.a, .b {
  .x:not(&) { color: red }
}
/* before */
.x:not(.a), .x:not(.b) { color: red }

/* after, like Sass 1.105.1 */
.x:not(.a, .b) { color: red }

Now & inside :not() gets the whole parent list in its own selector list, also inside :is(), :where() or :has() within :not(), and several & there take every combination, like top-level & since #176. Outside :not() nothing changes, so .x:is(&) still gives .x:is(.a), .x:is(.b). :not(:nth-child(2n of &)) also keeps the old output, because the part after of is not a plain selector list.

To check it, I compared the output with Sass on distinct generated nested rules that use & inside :not() in different ways. On main, 1,333 of 2,699 rules gave a different set of selectors, now none do (another seed: 1,324 of 2,651, now none). On 4,000 more distinct general rules, 143 more now match Sass and none stopped matching. In Chrome 154, the output now styles the same elements as native nesting in all 6 cases I tried, main differed in 5.

(Edited: my first numbers counted some generated rules twice. These count each distinct rule once.)

The new test fails on main. bnt --coverage 100, oxlint and oxfmt --check pass.

With a selector list as the parent, & inside :not() was replaced with
one parent selector per rule, so `.a, .b { .x:not(&) {} }` gave
`.x:not(.a), .x:not(.b)`, which also matches `.x.a`. Sass and native
CSS nesting put the whole list inside: `.x:not(.a, .b)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant