Skip to content

Commit 367ffb7

Browse files
committed
Refactor to remove unneeded utilities
1 parent 2214b35 commit 367ffb7

File tree

7 files changed

+169
-132
lines changed

7 files changed

+169
-132
lines changed

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import {any} from './lib/any.js'
99
import {parse} from './lib/parse.js'
10-
import {root} from './lib/util.js'
10+
import {parent} from './lib/util.js'
1111

1212
/**
1313
* Check that the given `node` matches `selector`.
@@ -84,7 +84,13 @@ function createState(tree) {
8484
results: [],
8585
any,
8686
iterator: undefined,
87-
scopeNodes: tree ? (root(tree) ? tree.children : [tree]) : [],
87+
scopeNodes: tree
88+
? parent(tree) &&
89+
// Root in nlcst.
90+
(tree.type === 'RootNode' || tree.type === 'root')
91+
? tree.children
92+
: [tree]
93+
: [],
8894
one: false,
8995
shallow: false,
9096
index: false,

lib/attribute.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function attribute(query, node) {
3636
}
3737

3838
/**
39+
* Check whether an attribute exists.
40+
*
3941
* `[attr]`
4042
*
4143
* @param {RuleAttr} query
@@ -48,6 +50,8 @@ function exists(query, node) {
4850
}
4951

5052
/**
53+
* Check whether an attribute has an exact value.
54+
*
5155
* `[attr=value]`
5256
*
5357
* @param {RuleAttr} query
@@ -60,6 +64,11 @@ function exact(query, node) {
6064
}
6165

6266
/**
67+
* Check whether an attribute, as a list, contains a value.
68+
*
69+
* When the attribute value is not a list, checks that the serialized value
70+
* is the queried one.
71+
*
6372
* `[attr~=value]`
6473
*
6574
* @param {RuleAttr} query
@@ -86,6 +95,8 @@ function containsArray(query, node) {
8695
}
8796

8897
/**
98+
* Check whether an attribute has a substring as its start.
99+
*
89100
* `[attr^=value]`
90101
*
91102
* @param {RuleAttr} query
@@ -105,6 +116,8 @@ function begins(query, node) {
105116
}
106117

107118
/**
119+
* Check whether an attribute has a substring as its end.
120+
*
108121
* `[attr$=value]`
109122
*
110123
* @param {RuleAttr} query
@@ -124,6 +137,8 @@ function ends(query, node) {
124137
}
125138

126139
/**
140+
* Check whether an attribute contains a substring.
141+
*
127142
* `[attr*=value]`
128143
*
129144
* @param {RuleAttr} query

lib/name.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
/**
7+
* Check whether an element has a type.
8+
*
79
* @param {Rule} query
810
* @param {Node} node
911
*/

lib/nest.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
5649
function 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+
171177
class WalkIterator {
172178
/**
173179
* Handles typeIndex and typeCount properties for every walker.

0 commit comments

Comments
 (0)