@@ -18,13 +18,15 @@ const handle = zwitch('nestingOperator', {
1818 invalid : topScan , // `undefined` is the top query selector.
1919 handlers : {
2020 null : descendant , // `null` is the descendant combinator.
21- '>' : child ,
21+ '>' : directChild ,
2222 '+' : adjacentSibling ,
2323 '~' : generalSibling
2424 }
2525} )
2626
2727/**
28+ * Nest a rule.
29+ *
2830 * @param {Rule } query
2931 * @param {Node } node
3032 * @param {number | undefined } index
@@ -35,31 +37,16 @@ export function nest(query, node, index, parent, state) {
3537 handle ( query , node , index , parent , state )
3638}
3739
38- // Shouldn’t be called, parser gives correct data.
39- /**
40- * @param {unknown } query
41- * @returns {never }
42- */
43- /* c8 ignore next 4 */
44- function unknownNesting ( query ) {
45- // @ts -expect-error: `nestingOperator` guaranteed.
46- throw new Error ( 'Unexpected nesting `' + query . nestingOperator + '`' )
47- }
48-
4940/**
41+ * Top-most scan.
42+ *
5043 * @param {Rule } query
5144 * @param {Node } node
5245 * @param {number | undefined } index
5346 * @param {Parent | undefined } parent
5447 * @param {SelectState } state
5548 */
5649function topScan ( query , node , index , parent , state ) {
57- // Shouldn’t happen.
58- /* c8 ignore next 7 */
59- if ( parent ) {
60- throw new Error ( 'topScan is supposed to be called from the root node' )
61- }
62-
6350 if ( ! state . iterator ) {
6451 throw new Error ( 'Expected `iterator` to be defined' )
6552 }
@@ -75,6 +62,8 @@ function topScan(query, node, index, parent, state) {
7562}
7663
7764/**
65+ * Handle a descendant nesting operator (`a b`).
66+ *
7867 * @param {Rule } query
7968 * @param {Node } node
8069 * @param {number | undefined } index
@@ -90,36 +79,40 @@ function descendant(query, node, index, parent, state) {
9079
9180 const previous = state . iterator
9281
93- state . iterator = iterator
94- child ( query , node , index , parent , state )
82+ state . iterator = descendantIterator
83+ directChild ( query , node , index , parent , state )
9584
9685 /** @type {SelectIterator } */
97- function iterator ( query , node , index , parent , state ) {
86+ function descendantIterator ( query , node , index , parent , state ) {
9887 state . iterator = previous
9988 previous ( query , node , index , parent , state )
100- state . iterator = iterator
89+ state . iterator = descendantIterator
10190
10291 if ( state . one && state . found ) return
10392
104- child ( query , node , index , parent , state )
93+ directChild ( query , node , index , parent , state )
10594 }
10695}
10796
10897/**
98+ * Handle a direct child nesting operator (`a > b`).
99+ *
100+ * Also reused by normal descendant operators.
101+ *
109102 * @param {Rule } query
110103 * @param {Node } node
111104 * @param {number | undefined } _1
112105 * @param {Parent | undefined } _2
113106 * @param {SelectState } state
114107 */
115- function child ( query , node , _1 , _2 , state ) {
116- if ( ! parent ( node ) ) return
117- if ( node . children . length === 0 ) return
118-
108+ function directChild ( query , node , _1 , _2 , state ) {
109+ if ( ! parent ( node ) || node . children . length === 0 ) return
119110 new WalkIterator ( query , node , state ) . each ( undefined , undefined ) . done ( )
120111}
121112
122113/**
114+ * Handle an adjacent sibling nesting operator (`a + b`).
115+ *
123116 * @param {Rule } query
124117 * @param {Node } _
125118 * @param {number | undefined } index
@@ -145,6 +138,8 @@ function adjacentSibling(query, _, index, parent, state) {
145138}
146139
147140/**
141+ * Handle a general sibling nesting operator (`a ~ b`).
142+ *
148143 * @param {Rule } query
149144 * @param {Node } _
150145 * @param {number | undefined } index
@@ -168,6 +163,17 @@ function generalSibling(query, _, index, parent, state) {
168163 . done ( )
169164}
170165
166+ // Shouldn’t be called, parser gives correct data.
167+ /**
168+ * @param {unknown } query
169+ * @returns {never }
170+ */
171+ /* c8 ignore next 4 */
172+ function unknownNesting ( query ) {
173+ // @ts -expect-error: `nestingOperator` guaranteed.
174+ throw new Error ( 'Unexpected nesting `' + query . nestingOperator + '`' )
175+ }
176+
171177class WalkIterator {
172178 /**
173179 * Handles typeIndex and typeCount properties for every walker.
0 commit comments