Skip to content

Commit 3e5d5d5

Browse files
committed
Add type tests
1 parent 8524c24 commit 3e5d5d5

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

index.test-d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {expectType} from 'tsd'
2+
import type {
3+
Delete,
4+
Emphasis,
5+
Link,
6+
LinkReference,
7+
Heading,
8+
PhrasingContent,
9+
Strong,
10+
Text
11+
} from 'mdast'
12+
import {map} from './index.js'
13+
14+
const text: Text = {type: 'text', value: 'alpha'}
15+
const heading: Heading = {type: 'heading', depth: 1, children: [text]}
16+
17+
// @ts-expect-error: tree needed.
18+
map()
19+
20+
// @ts-expect-error: map function needed.
21+
map(heading)
22+
23+
map(text, function (node) {
24+
expectType<Text>(node)
25+
return node
26+
})
27+
28+
map(heading, function (node, index, parent) {
29+
expectType<Heading | PhrasingContent>(node)
30+
expectType<number | undefined>(index)
31+
expectType<
32+
Delete | Emphasis | Link | Heading | LinkReference | Strong | undefined
33+
>(parent)
34+
return node
35+
})

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
* Node type.
5050
* @typedef {(
5151
* (
52-
* node: Readonly<InclusiveDescendant<Tree>>,
52+
* node: InclusiveDescendant<Tree>,
5353
* index: number | undefined,
54-
* parent: Readonly<Extract<InclusiveDescendant<Tree>, UnistParent>> | undefined
54+
* parent: Extract<InclusiveDescendant<Tree>, UnistParent> | undefined
5555
* ) => Tree | InclusiveDescendant<Tree>
5656
* )} MapFunction
5757
* Function called with a node, its index, and its parent to produce a new

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@
3838
"@types/unist": "^3.0.0"
3939
},
4040
"devDependencies": {
41+
"@types/mdast": "^4.0.0",
4142
"@types/node": "^20.0.0",
4243
"c8": "^8.0.0",
4344
"prettier": "^2.0.0",
4445
"remark-cli": "^11.0.0",
4546
"remark-preset-wooorm": "^9.0.0",
47+
"tsd": "^0.28.0",
4648
"type-coverage": "^2.0.0",
4749
"typescript": "^5.0.0",
4850
"unist-builder": "^4.0.0",
4951
"xo": "^0.54.0"
5052
},
5153
"scripts": {
5254
"prepack": "npm run build && npm run format",
53-
"build": "tsc --build --clean && tsc --build && type-coverage",
55+
"build": "tsc --build --clean && tsc --build && tsd && type-coverage",
5456
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5557
"test-api": "node --conditions development test.js",
5658
"test-coverage": "c8 --100 --reporter lcov npm run test-api",

0 commit comments

Comments
 (0)