@@ -847,46 +847,47 @@ export function handleNesting(ast: AstNode[]): AstNode[] {
847847 // ^^^^^^^^^^^^^^^^^ complex selector
848848 //
849849 else if ( ctx . parent . kind === 'compound' ) {
850- let path = ctx . path ( )
851- let grandParent = path [ path . length - 2 ]
850+ if ( parentAst [ 0 ] . kind === 'complex' ) {
851+ let path = ctx . path ( )
852+ let grandParent = path [ path . length - 2 ]
852853
853- if (
854- grandParent &&
855- grandParent . kind === 'complex' &&
856- // When our compound parent is part of a complex
857- // selector, and it's not the very first node, then we
858- // can't safely get rid of the `:is(…)` if the last
859- // selector is a complex selector as well, unless the
860- // `&` maps to a single selector or compound selector.
861- //
862- // ```css
863- // .foo .bar { /* Complex selector */
864- // .system &:focus { /* Complex selector + compound selecto*/
865- // --x: 1;
866- // }
867- // }
868- // .foo:hover { /* Compound selector */
869- // .system &:focus { /* Complex selector + compound selector */
870- // --x: 2;
871- // }
872- // }
873- // ```
874- //
875- // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
876- //
877- // ```css
878- // .system :is(.foo .bar):focus { /* Cannot drop the `:is(…)`, otherwise `.system` and `.foo` can be swapped in the DOM */
879- // --x: 1;
880- // }
881- // .system .foo:hover:focus {
882- // --x: 2;
883- // }
884- // ```
885- //
886- grandParent . nodes [ 0 ] !== ctx . parent &&
887- parentAst [ 0 ] . kind === 'complex'
888- ) {
889- return // Keep `:is(…)` semantics
854+ if (
855+ grandParent &&
856+ grandParent . kind === 'complex' &&
857+ // When our compound parent is part of a complex
858+ // selector, and it's not the very first node, then we
859+ // can't safely get rid of the `:is(…)` if the last
860+ // selector is a complex selector as well, unless the
861+ // `&` maps to a single selector or compound selector.
862+ //
863+ // ```css
864+ // .foo .bar { /* Complex selector */
865+ // .system &:focus { /* Complex selector + compound selecto*/
866+ // --x: 1;
867+ // }
868+ // }
869+ // .foo:hover { /* Compound selector */
870+ // .system &:focus { /* Complex selector + compound selector */
871+ // --x: 2;
872+ // }
873+ // }
874+ // ```
875+ //
876+ // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
877+ //
878+ // ```css
879+ // .system :is(.foo .bar):focus { /* Cannot drop the `:is(…)`, otherwise `.system` and `.foo` can be swapped in the DOM */
880+ // --x: 1;
881+ // }
882+ // .system .foo:hover:focus {
883+ // --x: 2;
884+ // }
885+ // ```
886+ //
887+ grandParent . nodes [ 0 ] !== ctx . parent
888+ ) {
889+ return // Keep `:is(…)` semantics
890+ }
890891 }
891892
892893 // `&*` and `&div` are invalid CSS so these should stay
0 commit comments