Skip to content

Commit 7283752

Browse files
committed
Refactor code-style
1 parent cef3307 commit 7283752

12 files changed

+2193
-1691
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export function selectAll(selector, tree) {
8484
* @param {Node | null | undefined} tree
8585
* Tree to search.
8686
* @returns {SelectState}
87+
* State.
8788
*/
8889
function createState(selector, tree) {
8990
return {

lib/attribute.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import {unreachable} from 'devlop'
88
import {zwitch} from 'zwitch'
9+
import {indexable} from './util.js'
910

1011
/** @type {(query: AstAttribute, node: Node) => boolean} */
1112
const handle = zwitch('operator', {
@@ -48,7 +49,7 @@ export function attribute(query, node) {
4849
* @returns {boolean}
4950
*/
5051
function exists(query, node) {
51-
// @ts-expect-error: Looks like a record.
52+
indexable(node)
5253
return node[query.name] !== null && node[query.name] !== undefined
5354
}
5455

@@ -63,8 +64,7 @@ function exists(query, node) {
6364
*/
6465
function exact(query, node) {
6566
const queryValue = attributeValue(query)
66-
67-
// @ts-expect-error: Looks like a record.
67+
indexable(node)
6868
return exists(query, node) && String(node[query.name]) === queryValue
6969
}
7070

@@ -81,8 +81,7 @@ function exact(query, node) {
8181
* @returns {boolean}
8282
*/
8383
function containsArray(query, node) {
84-
/** @type {unknown} */
85-
// @ts-expect-error: Looks like a record.
84+
indexable(node)
8685
const value = node[query.name]
8786

8887
if (value === null || value === undefined) return false
@@ -111,8 +110,7 @@ function containsArray(query, node) {
111110
* @returns {boolean}
112111
*/
113112
function begins(query, node) {
114-
/** @type {unknown} */
115-
// @ts-expect-error: Looks like a record.
113+
indexable(node)
116114
const value = node[query.name]
117115
const queryValue = attributeValue(query)
118116

lib/parse.js

-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import {createParser} from 'css-selector-parser'
66

77
const cssSelectorParse = createParser({syntax: 'selectors-4'})
88

9-
// .
10-
// const parser = new CssSelectorParser()
11-
12-
// parser.registerAttrEqualityMods('~', '^', '$', '*')
13-
// parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
14-
// parser.registerNestingOperators('>', '+', '~')
15-
169
/**
1710
* @param {string} selector
1811
* @returns {AstSelector}

lib/pseudo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {parent} from './util.js'
1313
import {walk} from './walk.js'
1414

1515
/** @type {import('nth-check').default} */
16-
// @ts-expect-error
16+
// @ts-expect-error: `nth-check` types are wrong.
1717
const nthCheck = fauxEsmNthCheck.default || fauxEsmNthCheck
1818

1919
/** @type {(rule: AstPseudoClass, node: Node, index: number | undefined, parent: Parent | undefined, state: SelectState) => boolean} */

lib/util.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,32 @@
33
* @typedef {import('unist').Parent} Parent
44
*/
55

6+
import {unreachable} from 'devlop'
7+
8+
/**
9+
* TypeScript helper to check if something is indexable (any object is
10+
* indexable in JavaScript).
11+
*
12+
* @param {unknown} value
13+
* Thing to check.
14+
* @returns {asserts value is Record<string, unknown>}
15+
* Nothing.
16+
* @throws {Error}
17+
* When `value` is not an object.
18+
*/
19+
export function indexable(value) {
20+
// Always called when something is an object, this is just for TS.
21+
/* c8 ignore next 3 */
22+
if (!value || typeof value !== 'object') {
23+
unreachable('Expected object')
24+
}
25+
}
26+
627
/**
728
* @param {Node} node
829
* @returns {node is Parent}
930
*/
1031
export function parent(node) {
11-
// @ts-expect-error: looks like a record.
32+
indexable(node)
1233
return Array.isArray(node.children)
1334
}

lib/walk.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function one(state, currentRules, node, index, parentNode) {
8484
* @param {SelectState} state
8585
* @param {Nest} nest
8686
* @param {Parent} node
87-
* @returns {void}
87+
* @returns {undefined}
8888
*/
8989
function all(state, nest, node) {
9090
const fromParent = combine(nest.descendant, nest.directChild)
@@ -256,7 +256,7 @@ function add(nest, field, rule) {
256256
* Counts.
257257
* @param {Node} node
258258
* Node.
259-
* @returns {void}
259+
* @returns {undefined}
260260
* Nothing.
261261
*/
262262
function count(counts, node) {

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
"test/**/*.js"
100100
],
101101
"rules": {
102+
"import/no-unassigned-import": "off",
103+
"max-nested-callbacks": "off",
102104
"no-await-in-loop": "off"
103105
}
104106
}

test/all.js

+104-86
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,116 @@ import test from 'node:test'
33
import {u} from 'unist-builder'
44
import {selectAll} from '../index.js'
55

6-
test('all together now', () => {
7-
assert.deepEqual(
8-
selectAll(
9-
'a > b[d]:nth-of-type(odd)',
10-
u('root', [
11-
u('a', [
12-
u('b', {d: 1}, 'Alpha'),
13-
u('c', 'Bravo'),
14-
u('b', 'Charlie'),
15-
u('c', 'Delta'),
16-
u('b', 'Echo'),
17-
u('c', 'Foxtrot')
6+
test('all together now', async function (t) {
7+
await t.test('should expose the public api', async function () {
8+
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
9+
'matches',
10+
'select',
11+
'selectAll'
12+
])
13+
})
14+
15+
await t.test('#1', async function () {
16+
assert.deepEqual(
17+
selectAll(
18+
'a > b[d]:nth-of-type(odd)',
19+
u('root', [
20+
u('a', [
21+
u('b', {d: 1}, 'Alpha'),
22+
u('c', 'Bravo'),
23+
u('b', 'Charlie'),
24+
u('c', 'Delta'),
25+
u('b', 'Echo'),
26+
u('c', 'Foxtrot')
27+
])
1828
])
19-
])
20-
),
21-
[u('b', {d: 1}, 'Alpha')]
22-
)
29+
),
30+
[u('b', {d: 1}, 'Alpha')]
31+
)
32+
})
2333

24-
assert.deepEqual(
25-
selectAll(
26-
'[d] ~ c:nth-of-type(even)',
27-
u('root', [
28-
u('a', [
29-
u('b', 'Alpha'),
30-
u('c', 'Bravo'),
31-
u('b', {d: 1}, 'Charlie'),
32-
u('c', 'Delta'),
33-
u('b', 'Echo'),
34-
u('c', 'Foxtrot'),
35-
u('b', 'Golf'),
36-
u('c', 'Hotel')
34+
await t.test('#2', async function () {
35+
assert.deepEqual(
36+
selectAll(
37+
'[d] ~ c:nth-of-type(even)',
38+
u('root', [
39+
u('a', [
40+
u('b', 'Alpha'),
41+
u('c', 'Bravo'),
42+
u('b', {d: 1}, 'Charlie'),
43+
u('c', 'Delta'),
44+
u('b', 'Echo'),
45+
u('c', 'Foxtrot'),
46+
u('b', 'Golf'),
47+
u('c', 'Hotel')
48+
])
3749
])
38-
])
39-
),
40-
[u('c', 'Delta'), u('c', 'Hotel')]
41-
)
50+
),
51+
[u('c', 'Delta'), u('c', 'Hotel')]
52+
)
53+
})
4254

43-
assert.deepEqual(
44-
selectAll(
45-
'[d] + c:nth-of-type(even)',
46-
u('root', [
47-
u('a', [
48-
u('b', 'Alpha'),
49-
u('c', 'Bravo'),
50-
u('b', {d: 1}, 'Charlie'),
51-
u('c', 'Delta'),
52-
u('b', 'Echo'),
53-
u('c', 'Foxtrot'),
54-
u('b', 'Golf'),
55-
u('c', 'Hotel')
55+
await t.test('#3', async function () {
56+
assert.deepEqual(
57+
selectAll(
58+
'[d] + c:nth-of-type(even)',
59+
u('root', [
60+
u('a', [
61+
u('b', 'Alpha'),
62+
u('c', 'Bravo'),
63+
u('b', {d: 1}, 'Charlie'),
64+
u('c', 'Delta'),
65+
u('b', 'Echo'),
66+
u('c', 'Foxtrot'),
67+
u('b', 'Golf'),
68+
u('c', 'Hotel')
69+
])
5670
])
57-
])
58-
),
59-
[u('c', 'Delta')]
60-
)
71+
),
72+
[u('c', 'Delta')]
73+
)
74+
})
6175

62-
assert.deepEqual(
63-
selectAll(
64-
'[d], :nth-of-type(even), [e]',
65-
u('root', [
66-
u('a', [
67-
u('b', {e: 3}, 'Alpha'),
68-
u('c', 'Bravo'),
69-
u('b', {d: 1}, 'Charlie'),
70-
u('c', 'Delta'),
71-
u('b', 'Echo'),
72-
u('c', {d: 2, e: 4}, 'Foxtrot'),
73-
u('b', 'Golf'),
74-
u('c', 'Hotel')
76+
await t.test('#4', async function () {
77+
assert.deepEqual(
78+
selectAll(
79+
'[d], :nth-of-type(even), [e]',
80+
u('root', [
81+
u('a', [
82+
u('b', {e: 3}, 'Alpha'),
83+
u('c', 'Bravo'),
84+
u('b', {d: 1}, 'Charlie'),
85+
u('c', 'Delta'),
86+
u('b', 'Echo'),
87+
u('c', {d: 2, e: 4}, 'Foxtrot'),
88+
u('b', 'Golf'),
89+
u('c', 'Hotel')
90+
])
7591
])
76-
])
77-
),
78-
[
79-
u('b', {e: 3}, 'Alpha'),
80-
u('b', {d: 1}, 'Charlie'),
81-
u('c', 'Delta'),
82-
u('c', {d: 2, e: 4}, 'Foxtrot'),
83-
u('b', 'Golf'),
84-
u('c', 'Hotel')
85-
]
86-
)
92+
),
93+
[
94+
u('b', {e: 3}, 'Alpha'),
95+
u('b', {d: 1}, 'Charlie'),
96+
u('c', 'Delta'),
97+
u('c', {d: 2, e: 4}, 'Foxtrot'),
98+
u('b', 'Golf'),
99+
u('c', 'Hotel')
100+
]
101+
)
102+
})
87103

88-
assert.deepEqual(
89-
selectAll(
90-
'a:not([b])',
91-
u('root', [
92-
u('a', {id: 'w', b: 'a'}),
93-
u('a', {id: 'x'}),
94-
u('a', {id: 'y', b: 'a'}),
95-
u('a', {id: 'z'})
96-
])
97-
),
98-
[u('a', {id: 'x'}), u('a', {id: 'z'})]
99-
)
104+
await t.test('#5', async function () {
105+
assert.deepEqual(
106+
selectAll(
107+
'a:not([b])',
108+
u('root', [
109+
u('a', {id: 'w', b: 'a'}),
110+
u('a', {id: 'x'}),
111+
u('a', {id: 'y', b: 'a'}),
112+
u('a', {id: 'z'})
113+
])
114+
),
115+
[u('a', {id: 'x'}), u('a', {id: 'z'})]
116+
)
117+
})
100118
})

test/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/* eslint-disable import/no-unassigned-import */
1+
import './all.js'
22
import './matches.js'
33
import './select.js'
44
import './select-all.js'
5-
import './all.js'
6-
/* eslint-enable import/no-unassigned-import */

0 commit comments

Comments
 (0)