Skip to content

Commit bd62f7d

Browse files
committed
Add strict to tsconfig.json
1 parent 45a7ae8 commit bd62f7d

File tree

14 files changed

+202
-88
lines changed

14 files changed

+202
-88
lines changed

lib/any.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ import {pseudo} from './pseudo.js'
1616
import {test} from './test.js'
1717

1818
const type = zwitch('type', {
19+
// @ts-expect-error: hush.
1920
unknown: unknownType,
2021
invalid: invalidType,
22+
// @ts-expect-error: hush.
2123
handlers: {selectors, ruleSet, rule}
2224
})
2325

2426
/**
2527
* @param {Selectors|RuleSet|Rule} query
26-
* @param {HastNode} node
28+
* @param {HastNode|undefined} node
2729
* @param {SelectState} state
2830
* @returns {Array.<Element>}
2931
*/
3032
export function any(query, node, state) {
31-
// @ts-ignore zwitch types are off.
33+
// @ts-expect-error zwitch types are off.
3234
return query && node ? type(query, node, state) : []
3335
}
3436

@@ -79,10 +81,10 @@ function rule(query, tree, state) {
7981
null,
8082
configure(query, {
8183
schema: state.space === 'svg' ? svg : html,
82-
language: null,
84+
language: undefined,
8385
direction: 'ltr',
8486
editableOrEditingHost: false,
85-
// @ts-ignore assume elements.
87+
// @ts-expect-error assume elements.
8688
scopeElements: tree.type === 'root' ? tree.children : [tree],
8789
iterator,
8890
one: state.one,
@@ -100,7 +102,7 @@ function rule(query, tree, state) {
100102
if (query.rule) {
101103
nest(query.rule, node, index, parent, configure(query.rule, state))
102104
} else {
103-
// @ts-ignore `test` also asserts `node is Element`
105+
// @ts-expect-error `test` also asserts `node is Element`
104106
collector.collect(node)
105107
state.found = true
106108
}
@@ -147,12 +149,12 @@ function invalidType() {
147149

148150
class Collector {
149151
/**
150-
* @param {boolean} one
152+
* @param {boolean|undefined} [one]
151153
*/
152154
constructor(one) {
153155
/** @type {Array.<Element>} */
154156
this.result = []
155-
/** @type {boolean} */
157+
/** @type {boolean|undefined} */
156158
this.one = one
157159
/** @type {boolean} */
158160
this.found = false

lib/attribute.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ import {stringify as spaces} from 'space-separated-tokens'
1414
import {zwitch} from 'zwitch'
1515

1616
const handle = zwitch('operator', {
17+
// @ts-expect-error: hush.
1718
unknown: unknownOperator,
19+
// @ts-expect-error: hush.
1820
invalid: exists,
1921
handlers: {
22+
// @ts-expect-error: hush.
2023
'=': exact,
24+
// @ts-expect-error: hush.
2125
'~=': spaceSeparatedList,
26+
// @ts-expect-error: hush.
2227
'|=': exactOrPrefix,
28+
// @ts-expect-error: hush.
2329
'^=': begins,
30+
// @ts-expect-error: hush.
2431
'$=': ends,
32+
// @ts-expect-error: hush.
2533
'*=': contains
2634
}
2735
})
@@ -37,7 +45,9 @@ export function attribute(query, element, schema) {
3745
let index = -1
3846

3947
while (++index < attrs.length) {
40-
if (!handle(attrs[index], element, find(schema, attrs[index].name))) return
48+
if (!handle(attrs[index], element, find(schema, attrs[index].name))) {
49+
return false
50+
}
4151
}
4252

4353
return true
@@ -64,9 +74,10 @@ function exists(_, element, info) {
6474
* @returns {boolean}
6575
*/
6676
function exact(query, element, info) {
67-
return (
77+
return Boolean(
6878
hasProperty(element, info.property) &&
69-
normalizeValue(element.properties[info.property], info) === query.value
79+
element.properties &&
80+
normalizeValue(element.properties[info.property], info) === query.value
7081
)
7182
}
7283

@@ -79,14 +90,15 @@ function exact(query, element, info) {
7990
* @returns {boolean}
8091
*/
8192
function spaceSeparatedList(query, element, info) {
82-
const value = element.properties[info.property]
93+
const value = element.properties && element.properties[info.property]
8394

8495
return (
8596
// If this is a comma-separated list, and the query is contained in it, return
8697
// true.
8798
(!info.commaSeparated &&
8899
value &&
89100
typeof value === 'object' &&
101+
query.value &&
90102
value.includes(query.value)) ||
91103
// For all other values (including comma-separated lists), return whether this
92104
// is an exact match.
@@ -104,13 +116,17 @@ function spaceSeparatedList(query, element, info) {
104116
* @returns {boolean}
105117
*/
106118
function exactOrPrefix(query, element, info) {
107-
const value = normalizeValue(element.properties[info.property], info)
119+
const value = normalizeValue(
120+
element.properties && element.properties[info.property],
121+
info
122+
)
108123

109-
return (
124+
return Boolean(
110125
hasProperty(element, info.property) &&
111-
(value === query.value ||
112-
(value.slice(0, query.value.length) === query.value &&
113-
value.charAt(query.value.length) === '-'))
126+
query.value &&
127+
(value === query.value ||
128+
(value.slice(0, query.value.length) === query.value &&
129+
value.charAt(query.value.length) === '-'))
114130
)
115131
}
116132

@@ -123,12 +139,14 @@ function exactOrPrefix(query, element, info) {
123139
* @returns {boolean}
124140
*/
125141
function begins(query, element, info) {
126-
return (
142+
return Boolean(
127143
hasProperty(element, info.property) &&
128-
normalizeValue(element.properties[info.property], info).slice(
129-
0,
130-
query.value.length
131-
) === query.value
144+
element.properties &&
145+
query.value &&
146+
normalizeValue(element.properties[info.property], info).slice(
147+
0,
148+
query.value.length
149+
) === query.value
132150
)
133151
}
134152

@@ -141,11 +159,13 @@ function begins(query, element, info) {
141159
* @returns {boolean}
142160
*/
143161
function ends(query, element, info) {
144-
return (
162+
return Boolean(
145163
hasProperty(element, info.property) &&
146-
normalizeValue(element.properties[info.property], info).slice(
147-
-query.value.length
148-
) === query.value
164+
element.properties &&
165+
query.value &&
166+
normalizeValue(element.properties[info.property], info).slice(
167+
-query.value.length
168+
) === query.value
149169
)
150170
}
151171

@@ -158,11 +178,13 @@ function ends(query, element, info) {
158178
* @returns {boolean}
159179
*/
160180
function contains(query, element, info) {
161-
return (
181+
return Boolean(
162182
hasProperty(element, info.property) &&
163-
normalizeValue(element.properties[info.property], info).includes(
164-
query.value
165-
)
183+
element.properties &&
184+
query.value &&
185+
normalizeValue(element.properties[info.property], info).includes(
186+
query.value
187+
)
166188
)
167189
}
168190

lib/class-name.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
*/
1111
export function className(query, element) {
1212
/** @type {Array.<string>} */
13-
// @ts-ignore Assume array.
13+
// @ts-expect-error Assume array.
1414
const value = element.properties.className || []
1515
let index = -1
1616

17-
while (++index < query.classNames.length) {
18-
if (!value.includes(query.classNames[index])) return
17+
if (query.classNames) {
18+
while (++index < query.classNames.length) {
19+
if (!value.includes(query.classNames[index])) return false
20+
}
1921
}
2022

2123
return true

lib/enter-state.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {direction} from 'direction'
1010
import {isElement} from 'hast-util-is-element'
11+
// @ts-expect-error: to do type.
1112
import toString from 'hast-util-to-string'
1213
import {svg} from 'property-information'
1314
import {visit, EXIT, SKIP} from 'unist-util-visit'
@@ -24,15 +25,13 @@ export function enterState(state, node) {
2425
const language = state.language
2526
const currentDirection = state.direction
2627
const editableOrEditingHost = state.editableOrEditingHost
27-
/** @type {Direction|null} */
28+
/** @type {Direction|undefined} */
2829
let dirInferred
29-
/** @type {boolean} */
30+
/** @type {boolean|undefined} */
3031
let found
3132

32-
if (element(node)) {
33-
// @ts-ignore Assume string.
33+
if (element(node) && node.properties) {
3434
const lang = node.properties.xmlLang || node.properties.lang
35-
// @ts-ignore Assume string.
3635
const type = node.properties.type || 'text'
3736
const dir = dirProperty(node)
3837

@@ -41,7 +40,7 @@ export function enterState(state, node) {
4140
found = true
4241
}
4342

44-
if (schema.space === 'html') {
43+
if (schema && schema.space === 'html') {
4544
if (node.properties.contentEditable === 'true') {
4645
state.editableOrEditingHost = true
4746
found = true
@@ -78,13 +77,14 @@ export function enterState(state, node) {
7877
type === 'text')
7978
) {
8079
// Check value of `<input>`.
81-
// @ts-ignore something is `never` in types but this is needed.
80+
// @ts-expect-error something is `never` in types but this is needed.
8281
dirInferred = node.properties.value
83-
? // @ts-ignore Assume string
82+
? // @ts-expect-error Assume string
8483
dirBidi(node.properties.value)
8584
: 'ltr'
8685
} else {
8786
// Check text nodes in `node`.
87+
// @ts-expect-error: fine.
8888
visit(node, inferDirectionality)
8989
}
9090
}
@@ -129,23 +129,26 @@ export function enterState(state, node) {
129129

130130
/**
131131
* @param {string} value
132-
* @returns {Direction}
132+
* @returns {Direction|undefined}
133133
*/
134134
function dirBidi(value) {
135135
const result = direction(value)
136-
return result === 'neutral' ? null : result
136+
return result === 'neutral' ? undefined : result
137137
}
138138

139139
/**
140140
* @param {ElementChild} node
141-
* @returns {Direction}
141+
* @returns {Direction|undefined}
142142
*/
143143
function dirProperty(node) {
144144
const value =
145-
element(node) && typeof node.properties.dir === 'string'
145+
element(node) && node.properties && typeof node.properties.dir === 'string'
146146
? node.properties.dir.toLowerCase()
147-
: null
148-
return value === 'auto' || value === 'ltr' || value === 'rtl' ? value : null
147+
: undefined
148+
149+
return value === 'auto' || value === 'ltr' || value === 'rtl'
150+
? value
151+
: undefined
149152
}
150153

151154
function noop() {}

lib/id.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
* @returns {boolean}
1010
*/
1111
export function id(query, element) {
12-
return element.properties.id === query.id
12+
return Boolean(element.properties && element.properties.id === query.id)
1313
}

0 commit comments

Comments
 (0)