@@ -36,19 +36,7 @@ function is_global_block_selector(simple_selector) {
3636 */
3737function is_unscoped_global ( path ) {
3838 // remove every at rule or stylesheet and the current rule in case is passed in from `ComplexSelector`
39- const parents = path . filter ( ( parent ) => parent . type !== 'Atrule' && parent . type !== 'StyleSheet' ) ;
40-
41- let unscoped_global = true ;
42-
43- // no parents means we are on top
44- if ( parents . length > 0 ) {
45- // let's check that everything in the path is not a global block
46- unscoped_global = parents . every ( ( parent ) => {
47- return parent . type !== 'Rule' || parent . metadata . is_global_block ;
48- } ) ;
49- }
50-
51- return unscoped_global ;
39+ return path . filter ( ( node ) => node . type === 'Rule' ) . every ( ( node ) => node . metadata . is_global ) ;
5240}
5341
5442/**
@@ -203,14 +191,6 @@ const css_visitors = {
203191 Rule ( node , context ) {
204192 node . metadata . parent_rule = context . state . rule ;
205193
206- // if this rule has a ComplexSelector whose RelativeSelector children are all
207- // `:global(...)`, and the rule contains declarations (rather than just
208- // nested rules) then the component as a whole includes global CSS
209- context . state . has_global . value ||=
210- node . block . children . filter ( ( child ) => child . type === 'Declaration' ) . length > 0 &&
211- node . prelude . children . some ( ( selector ) => selector . children . every ( is_global ) ) &&
212- is_unscoped_global ( context . path ) ;
213-
214194 node . metadata . is_global_block = node . prelude . children . some ( ( selector ) => {
215195 let is_global_block = false ;
216196
@@ -278,16 +258,29 @@ const css_visitors = {
278258 }
279259 }
280260
281- context . next ( {
282- ...context . state ,
283- rule : node
284- } ) ;
261+ const state = { ...context . state , rule : node } ;
285262
286- node . metadata . has_local_selectors = node . prelude . children . some ( ( selector ) => {
287- return selector . children . some (
288- ( { metadata } ) => ! metadata . is_global && ! metadata . is_global_like
289- ) ;
290- } ) ;
263+ // visit selector list first, to populate child selector metadata
264+ context . visit ( node . prelude , state ) ;
265+
266+ node . metadata . is_global = node . prelude . children . some ( ( selector ) =>
267+ selector . children . every ( ( { metadata } ) => metadata . is_global || metadata . is_global_like )
268+ ) ;
269+
270+ node . metadata . has_local_selectors = node . prelude . children . some ( ( selector ) =>
271+ selector . children . some ( ( { metadata } ) => ! metadata . is_global && ! metadata . is_global_like )
272+ ) ;
273+
274+ // if this rule has a ComplexSelector whose RelativeSelector children are all
275+ // `:global(...)`, and the rule contains declarations (rather than just
276+ // nested rules) then the component as a whole includes global CSS
277+ context . state . has_global . value ||=
278+ node . metadata . is_global &&
279+ node . block . children . filter ( ( child ) => child . type === 'Declaration' ) . length > 0 &&
280+ is_unscoped_global ( context . path ) ;
281+
282+ // visit block list, so parent rule metadata is populated
283+ context . visit ( node . block , state ) ;
291284 } ,
292285 NestingSelector ( node , context ) {
293286 const rule = /** @type {AST.CSS.Rule } */ ( context . state . rule ) ;
0 commit comments