Skip to content

Commit 6f7b407

Browse files
committed
Add strict to tsconfig.json
1 parent 658764a commit 6f7b407

11 files changed

+150
-31
lines changed

lib/any.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ import {test} from './test.js'
1818
import {root} from './util.js'
1919

2020
var type = zwitch('type', {
21+
// @ts-expect-error: hush.
2122
unknown: unknownType,
2223
invalid: invalidType,
24+
// @ts-expect-error: hush.
2325
handlers: {selectors, ruleSet, rule}
2426
})
2527

2628
/**
2729
* @param {Selectors|RuleSet|Rule} query
28-
* @param {Node} node
30+
* @param {Node|undefined} node
2931
* @param {SelectState} state
32+
* @returns {Array.<Node>}
3033
*/
3134
export function any(query, node, state) {
32-
// @ts-ignore zwitch types are off.
35+
// @ts-expect-error: fine.
3336
return query && node ? type(query, node, state) : []
3437
}
3538

@@ -90,7 +93,7 @@ function rule(query, tree, state) {
9093
/** @type {SelectIterator} */
9194
function iterator(query, node, index, parent, state) {
9295
if (test(query, node, index, parent, state)) {
93-
if ('rule' in query) {
96+
if (query.rule) {
9497
nest(query.rule, node, index, parent, configure(query.rule, state))
9598
} else {
9699
collect(node)
@@ -136,7 +139,7 @@ function invalidType() {
136139
}
137140

138141
/**
139-
* @param {boolean} one
142+
* @param {boolean|undefined} one
140143
*/
141144
function collector(one) {
142145
/** @type {Array.<Node>} */

lib/attribute.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
import {zwitch} from 'zwitch'
88

99
var handle = zwitch('operator', {
10+
// @ts-expect-error: hush.
1011
unknown: unknownOperator,
12+
// @ts-expect-error: hush.
1113
invalid: exists,
1214
handlers: {
15+
// @ts-expect-error: hush.
1316
'=': exact,
17+
// @ts-expect-error: hush.
1418
'^=': begins,
19+
// @ts-expect-error: hush.
1520
'$=': ends,
21+
// @ts-expect-error: hush.
1622
'*=': containsString,
23+
// @ts-expect-error: hush.
1724
'~=': containsArray
1825
}
1926
})
@@ -39,6 +46,7 @@ export function attribute(query, node) {
3946
* @param {Node} node
4047
*/
4148
function exists(query, node) {
49+
// @ts-expect-error: Looks like a record.
4250
return node[query.name] !== null && node[query.name] !== undefined
4351
}
4452

@@ -49,6 +57,7 @@ function exists(query, node) {
4957
* @param {Node} node
5058
*/
5159
function exact(query, node) {
60+
// @ts-expect-error: Looks like a record.
5261
return exists(query, node) && String(node[query.name]) === query.value
5362
}
5463

@@ -59,6 +68,8 @@ function exact(query, node) {
5968
* @param {Node} node
6069
*/
6170
function containsArray(query, node) {
71+
/** @type {unknown} */
72+
// @ts-expect-error: Looks like a record.
6273
var value = node[query.name]
6374

6475
if (value === null || value === undefined) return false
@@ -82,9 +93,12 @@ function containsArray(query, node) {
8293
* @param {Node} node
8394
*/
8495
function begins(query, node) {
96+
/** @type {unknown} */
97+
// @ts-expect-error: Looks like a record.
8598
var value = node[query.name]
8699

87100
return (
101+
query.value &&
88102
typeof value === 'string' &&
89103
value.slice(0, query.value.length) === query.value
90104
)
@@ -97,9 +111,12 @@ function begins(query, node) {
97111
* @param {Node} node
98112
*/
99113
function ends(query, node) {
114+
/** @type {unknown} */
115+
// @ts-expect-error: Looks like a record.
100116
var value = node[query.name]
101117

102118
return (
119+
query.value &&
103120
typeof value === 'string' &&
104121
value.slice(-query.value.length) === query.value
105122
)
@@ -112,8 +129,10 @@ function ends(query, node) {
112129
* @param {Node} node
113130
*/
114131
function containsString(query, node) {
132+
/** @type {unknown} */
133+
// @ts-expect-error: Looks like a record.
115134
var value = node[query.name]
116-
return typeof value === 'string' && value.includes(query.value)
135+
return query.value && typeof value === 'string' && value.includes(query.value)
117136
}
118137

119138
// Shouldn’t be invoked, Parser throws an error instead.

lib/nest.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ import {parent} from './util.js'
1414
var own = {}.hasOwnProperty
1515

1616
var handle = zwitch('nestingOperator', {
17+
// @ts-expect-error: hush.
1718
unknown: unknownNesting,
19+
// @ts-expect-error: hush.
1820
invalid: topScan, // `undefined` is the top query selector.
1921
handlers: {
22+
// @ts-expect-error: hush.
2023
null: descendant, // `null` is the descendant combinator.
24+
// @ts-expect-error: hush.
2125
'>': child,
26+
// @ts-expect-error: hush.
2227
'+': adjacentSibling,
28+
// @ts-expect-error: hush.
2329
'~': generalSibling
2430
}
2531
})
2632

2733
/** @type {Handler} */
28-
export function nest(query, node, index, parent, state) {
29-
return handle(query, node, index, parent, state)
30-
}
34+
export const nest = handle
3135

3236
// Shouldn’t be invoked, parser gives correct data.
3337
/* c8 ignore next 6 */
@@ -41,17 +45,33 @@ function unknownNesting(query) {
4145
/** @type {Handler} */
4246
function topScan(query, node, index, parent, state) {
4347
// Shouldn’t happen.
44-
/* c8 ignore next 3 */
48+
/* c8 ignore next 7 */
4549
if (parent) {
4650
throw new Error('topScan is supposed to be called from the root node')
4751
}
4852

53+
if (!state.iterator) {
54+
throw new Error('Expected `iterator` to be defined')
55+
}
56+
57+
// Shouldn’t happen.
58+
/* c8 ignore next 3 */
59+
if (typeof index !== 'number') {
60+
throw new TypeError('Expected `index` to be defined')
61+
}
62+
4963
state.iterator(query, node, index, parent, state)
5064
if (!state.shallow) descendant(query, node, index, parent, state)
5165
}
5266

5367
/** @type {Handler} */
5468
function descendant(query, node, index, parent, state) {
69+
// Shouldn’t happen.
70+
/* c8 ignore next 3 */
71+
if (!state.iterator) {
72+
throw new Error('Expected `iterator` to be defined')
73+
}
74+
5575
var previous = state.iterator
5676

5777
state.iterator = iterator
@@ -79,6 +99,12 @@ function child(query, node, _1, _2, state) {
7999

80100
/** @type {Handler} */
81101
function adjacentSibling(query, _, index, parent, state) {
102+
// Shouldn’t happen.
103+
/* c8 ignore next 3 */
104+
if (typeof index !== 'number') {
105+
throw new TypeError('Expected `index` to be defined')
106+
}
107+
82108
// Shouldn’t happen.
83109
/* c8 ignore next */
84110
if (!parent) return
@@ -92,6 +118,12 @@ function adjacentSibling(query, _, index, parent, state) {
92118

93119
/** @type {Handler} */
94120
function generalSibling(query, _, index, parent, state) {
121+
// Shouldn’t happen.
122+
/* c8 ignore next 3 */
123+
if (typeof index !== 'number') {
124+
throw new TypeError('Expected `index` to be defined')
125+
}
126+
95127
// Shouldn’t happen.
96128
/* c8 ignore next */
97129
if (!parent) return
@@ -161,6 +193,12 @@ class WalkIterator {
161193
index = this.typeIndex.index(child)
162194
this.delayed.push(delay)
163195
} else {
196+
// Shouldn’t happen.
197+
/* c8 ignore next 3 */
198+
if (!this.state.iterator) {
199+
throw new Error('Expected `iterator` to be defined')
200+
}
201+
164202
this.state.iterator(this.query, child, start, this.parent, this.state)
165203
}
166204

@@ -173,6 +211,18 @@ class WalkIterator {
173211
* @this {WalkIterator}
174212
*/
175213
function delay() {
214+
// Shouldn’t happen.
215+
/* c8 ignore next 3 */
216+
if (!this.typeIndex) {
217+
throw new TypeError('Expected `typeIndex` to be defined')
218+
}
219+
220+
// Shouldn’t happen.
221+
/* c8 ignore next 3 */
222+
if (!this.state.iterator) {
223+
throw new Error('Expected `iterator` to be defined')
224+
}
225+
176226
this.state.typeIndex = index
177227
this.state.nodeIndex = nodeIndex
178228
this.state.typeCount = this.typeIndex.count(child)

lib/parse.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import fauxEsmNthCheck from 'nth-check'
1212
import {zwitch} from 'zwitch'
1313

1414
/** @type {import('nth-check').default} */
15-
// @ts-ignore
15+
// @ts-expect-error
1616
var nthCheck = fauxEsmNthCheck.default
1717

1818
var nth = new Set([
@@ -28,6 +28,7 @@ parser.registerAttrEqualityMods('~', '^', '$', '*')
2828
parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
2929
parser.registerNestingOperators('>', '+', '~')
3030

31+
// @ts-expect-error: hush.
3132
var compile = zwitch('type', {handlers: {selectors, ruleSet, rule}})
3233

3334
/**
@@ -39,7 +40,7 @@ export function parse(selector) {
3940
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
4041
}
4142

42-
// @ts-ignore types are wrong.
43+
// @ts-expect-error types are wrong.
4344
return compile(parser.parse(selector))
4445
}
4546

@@ -77,9 +78,9 @@ function rule(query) {
7778
pseudo = pseudos[index]
7879

7980
if (nth.has(pseudo.name)) {
80-
// @ts-ignore Patch a non-primitive type.
81+
// @ts-expect-error Patch a non-primitive type.
8182
pseudo.value = nthCheck(pseudo.value)
82-
// @ts-ignore Patch a non-primitive type.
83+
// @ts-expect-error Patch a non-primitive type.
8384
pseudo.valueType = 'function'
8485
}
8586
}

0 commit comments

Comments
 (0)