Skip to content

Commit 600b310

Browse files
committed
Add strict types
1 parent 6fc18ad commit 600b310

21 files changed

+115
-74
lines changed

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
import {hasProperty} from 'hast-util-has-property'
16+
// @ts-expect-error: next.
1617
import minifyWhitespace from 'rehype-minify-whitespace'
1718
import {convert} from 'unist-util-is'
1819
import {visit} from 'unist-util-visit'
@@ -50,7 +51,7 @@ export function toMdast(tree, options = {}) {
5051
* @param {string|Array.<Node>} [children]
5152
*/
5253
(node, type, props, children) => {
53-
/** @type {Properties} */
54+
/** @type {Properties|undefined} */
5455
let properties
5556

5657
if (typeof props === 'string' || Array.isArray(props)) {
@@ -61,7 +62,7 @@ export function toMdast(tree, options = {}) {
6162
}
6263

6364
/** @type {Node} */
64-
// @ts-ignore Assume valid `type` and `children`/`value`.
65+
// @ts-expect-error Assume valid `type` and `children`/`value`.
6566
const result = {type, ...properties}
6667

6768
if (typeof children === 'string') {
@@ -98,7 +99,7 @@ export function toMdast(tree, options = {}) {
9899

99100
minifyWhitespace({newlines: options.newlines === true})(tree)
100101

101-
const result = one(h, tree, null)
102+
const result = one(h, tree, undefined)
102103

103104
if (!result) {
104105
mdast = {type: 'root', children: []}
@@ -115,6 +116,7 @@ export function toMdast(tree, options = {}) {
115116
/** @type {import('unist-util-visit').Visitor<Element>} */
116117
function onelement(node) {
117118
const id =
119+
// @ts-expect-error: `hasProperty` just checked it.
118120
hasProperty(node, 'id') && String(node.properties.id).toUpperCase()
119121

120122
if (id && !own.call(byId, id)) {
@@ -132,11 +134,15 @@ export function toMdast(tree, options = {}) {
132134
* @type {import('unist-util-visit').Visitor<Text>}
133135
*/
134136
function ontext(node, index, parent) {
137+
/* c8 ignore next 3 */
138+
if (index === null || !parent) {
139+
return
140+
}
141+
135142
const previous = parent.children[index - 1]
136143

137-
if (previous && node.type === previous.type) {
144+
if (previous && previous.type === node.type) {
138145
previous.value += node.value
139-
140146
parent.children.splice(index, 1)
141147

142148
if (previous.position && node.position) {
@@ -151,7 +157,7 @@ export function toMdast(tree, options = {}) {
151157

152158
// We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
153159
// as there the whitespace matters.
154-
if (block(parent)) {
160+
if (parent && block(parent)) {
155161
if (!index) {
156162
node.value = node.value.replace(/^[\t ]+/, '')
157163
}

lib/all.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {one} from './one.js'
1515
*/
1616
export function all(h, parent) {
1717
/** @type {Array.<Node>} */
18-
// @ts-ignore Assume `parent` is a parent.
18+
// @ts-expect-error Assume `parent` is a parent.
1919
const nodes = parent.children || []
2020
/** @type {Array.<MdastNode>} */
2121
let values = []
@@ -24,7 +24,7 @@ export function all(h, parent) {
2424
let result
2525

2626
while (++index < nodes.length) {
27-
// @ts-ignore assume `parent` is a parent.
27+
// @ts-expect-error assume `parent` is a parent.
2828
result = one(h, nodes[index], parent)
2929

3030
if (result) {

lib/handlers/a.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('../types.js').Handle} Handle
33
* @typedef {import('../types.js').Element} Element
4+
* @typedef {import('../types.js').Properties} Properties
45
*/
56

67
import {all} from '../all.js'
@@ -11,12 +12,15 @@ import {resolve} from '../util/resolve.js'
1112
* @param {Element} node
1213
*/
1314
export function a(h, node) {
15+
/** @type {Properties} */
16+
// @ts-expect-error: `props` are defined.
17+
const props = node.properties
1418
return h(
1519
node,
1620
'link',
1721
{
18-
title: node.properties.title || null,
19-
url: resolve(h, String(node.properties.href || '') || null)
22+
title: props.title || null,
23+
url: resolve(h, String(props.href || '') || null)
2024
},
2125
all(h, node)
2226
)

lib/handlers/base.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010
export function base(h, node) {
1111
if (!h.baseFound) {
12-
h.frozenBaseUrl = String(node.properties.href || '') || null
12+
h.frozenBaseUrl =
13+
String((node.properties && node.properties.href) || '') || null
1314
h.baseFound = true
1415
}
1516
}

lib/handlers/code.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @typedef {import('../types.js').ElementChild} ElementChild
55
*/
66

7-
import {hasProperty} from 'hast-util-has-property'
87
import {convertElement} from 'hast-util-is-element'
98
import {toText} from 'hast-util-to-text'
109
import {trimTrailingLines} from 'trim-trailing-lines'
@@ -24,20 +23,19 @@ const isCode = convertElement('code')
2423
export function code(h, node) {
2524
const children = node.children
2625
let index = -1
27-
/** @type {Array.<string|number>} */
26+
/** @type {Array.<string|number>|undefined} */
2827
let classList
29-
/** @type {string} */
28+
/** @type {string|undefined} */
3029
let lang
31-
/** @type {ElementChild} */
32-
let child
3330

3431
if (pre(node)) {
3532
while (++index < children.length) {
36-
child = children[index]
33+
const child = children[index]
3734

3835
if (
3936
isCode(child) &&
40-
hasProperty(child, 'className') &&
37+
child.properties &&
38+
child.properties.className &&
4139
Array.isArray(child.properties.className)
4240
) {
4341
classList = child.properties.className
@@ -46,7 +44,7 @@ export function code(h, node) {
4644
}
4745
}
4846

49-
if (Array.isArray(classList)) {
47+
if (classList) {
5048
index = -1
5149

5250
while (++index < classList.length) {

lib/handlers/dl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export function dl(h, node) {
8282
content.push({
8383
type: 'listItem',
8484
spread: result.length > 1,
85+
// @ts-expect-error: `null` is fine.
8586
checked: null,
8687
children: result
8788
})
@@ -116,6 +117,7 @@ function handle(h, children) {
116117
}
117118

118119
return [
120+
// @ts-expect-error: `null` is fine.
119121
{
120122
type: 'list',
121123
ordered: false,

lib/handlers/iframe.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('../types.js').Handle} Handle
33
* @typedef {import('../types.js').Element} Element
4+
* @typedef {import('../types.js').Properties} Properties
45
*/
56

67
import {resolve} from '../util/resolve.js'
@@ -11,8 +12,11 @@ import {wrapText} from '../util/wrap-text.js'
1112
* @param {Element} node
1213
*/
1314
export function iframe(h, node) {
14-
const src = String(node.properties.src || '')
15-
const title = String(node.properties.title || '')
15+
/** @type {Properties} */
16+
// @ts-expect-error: `props` are defined.
17+
const props = node.properties
18+
const src = String(props.src || '')
19+
const title = String(props.title || '')
1620

1721
// Only create a link if there is a title.
1822
// We can’t use the content of the frame because conforming HTML parsers treat
@@ -21,6 +25,7 @@ export function iframe(h, node) {
2125
if (src && title) {
2226
return {
2327
type: 'link',
28+
// @ts-expect-error: `null` is fine.
2429
title: null,
2530
url: resolve(h, src),
2631
children: [{type: 'text', value: wrapText(h, title)}]

lib/handlers/img.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('../types.js').Handle} Handle
33
* @typedef {import('../types.js').Element} Element
4+
* @typedef {import('../types.js').Properties} Properties
45
*/
56

67
import {resolve} from '../util/resolve.js'
@@ -10,9 +11,12 @@ import {resolve} from '../util/resolve.js'
1011
* @param {Element} node
1112
*/
1213
export function img(h, node) {
14+
/** @type {Properties} */
15+
// @ts-expect-error: `props` are defined.
16+
const props = node.properties
1317
return h(node, 'image', {
14-
url: resolve(h, String(node.properties.src || '') || null),
15-
title: node.properties.title || null,
16-
alt: node.properties.alt || ''
18+
url: resolve(h, String(props.src || '') || null),
19+
title: props.title || null,
20+
alt: props.alt || ''
1721
})
1822
}

lib/handlers/input.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('../types.js').Handle} Handle
33
* @typedef {import('../types.js').Element} Element
4+
* @typedef {import('../types.js').Properties} Properties
45
* @typedef {import('../types.js').MdastNode} MdastNode
56
*/
67

@@ -20,6 +21,8 @@ const datalist = convertElement('datalist')
2021
*/
2122
// eslint-disable-next-line complexity
2223
export function input(h, node) {
24+
/** @type {Properties} */
25+
// @ts-expect-error: `props` are defined.
2326
const props = node.properties
2427
let value = String(props.value || props.placeholder || '')
2528
/** @type {Array.<MdastNode>} */

lib/handlers/li.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ const input = convertElement('input')
2020
*/
2121
export function li(h, node) {
2222
const head = node.children[0]
23-
/** @type {boolean} */
23+
/** @type {boolean|null} */
2424
let checked = null
2525
/** @type {ElementChild} */
2626
let checkbox
27-
/** @type {Element} */
27+
/** @type {Element|undefined} */
2828
let clone
2929

3030
// Check if this node starts with a checkbox.
@@ -33,6 +33,7 @@ export function li(h, node) {
3333

3434
if (
3535
input(checkbox) &&
36+
checkbox.properties &&
3637
(checkbox.properties.type === 'checkbox' ||
3738
checkbox.properties.type === 'radio')
3839
) {

0 commit comments

Comments
 (0)