Skip to content

Commit e2d3139

Browse files
committed
swap checks, only compute path when absolutely necessary
We are checking wether the to-be-replaced ast is a complex selector or not. If it is, only then do we care about the type of the grand parent selector.
1 parent dc1758a commit e2d3139

1 file changed

Lines changed: 40 additions & 39 deletions

File tree

  • packages/tailwindcss/src

packages/tailwindcss/src/ast.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)