-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.test-d.ts
217 lines (190 loc) · 5.16 KB
/
index.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {expectAssignable, expectNotType, expectType} from 'tsd'
import type {
Blockquote,
Definition,
Delete,
Emphasis,
FootnoteDefinition,
Heading,
Link,
LinkReference,
ListItem,
Nodes,
Parents,
PhrasingContent,
Root,
RootContent,
Strong,
TableCell,
TableRow
} from 'mdast'
import type {Node, Parent} from 'unist'
import {CONTINUE, EXIT, SKIP, visit} from './index.js'
// Setup.
const implicitTree = {
type: 'root',
children: [{type: 'heading', depth: 1, children: []}]
}
const sampleTree: Root = {
type: 'root',
children: [{type: 'heading', depth: 1, children: []}]
}
// ## Missing parameters
// @ts-expect-error: check that `node` is passed.
visit()
// @ts-expect-error: check that `visitor` is passed.
visit(sampleTree)
// ## No test
visit(sampleTree, function (node, index, parent) {
expectType<Nodes>(node)
expectType<number | undefined>(index)
expectType<Parents | undefined>(parent)
})
visit(implicitTree, function (node, index, parent) {
// Objects are too loose.
expectAssignable<Node>(node)
expectNotType<Node>(node)
expectType<number | undefined>(index)
expectAssignable<Parent | undefined>(parent)
})
// ## String test
// Knows it’s a heading and its parents.
visit(sampleTree, 'heading', function (node, index, parent) {
expectType<Heading>(node)
expectType<number | undefined>(index)
expectType<Blockquote | FootnoteDefinition | ListItem | Root | undefined>(
parent
)
})
// Not in tree.
visit(sampleTree, 'element', function (node, index, parent) {
expectType<never>(node)
expectType<never>(index)
expectType<never>(parent)
})
// Implicit nodes are too loose.
visit(implicitTree, 'heading', function (node, index, parent) {
expectType<never>(node)
expectType<never>(index)
expectType<never>(parent)
})
visit(sampleTree, 'tableCell', function (node, index, parent) {
expectType<TableCell>(node)
expectType<number | undefined>(index)
expectType<Root | TableRow | undefined>(parent)
})
// ## Props test
// Knows that headings have depth, but TS doesn’t infer the depth normally.
visit(sampleTree, {depth: 1}, function (node) {
expectType<Heading>(node)
expectType<1 | 2 | 3 | 4 | 5 | 6>(node.depth)
})
// This goes fine.
visit(sampleTree, {type: 'heading'} as const, function (node) {
expectType<Heading>(node)
expectType<1 | 2 | 3 | 4 | 5 | 6>(node.depth)
})
// For some reason the const goes wrong.
visit(sampleTree, {depth: 1} as const, function (node) {
// Note: something going wrong here, to do: investigate.
expectType<never>(node)
})
// For some reason the const goes wrong.
visit(sampleTree, {type: 'heading', depth: 1} as const, function (node) {
// Note: something going wrong here, to do: investigate.
expectType<never>(node)
})
// Function test (implicit assertion).
visit(sampleTree, isHeadingLoose, function (node) {
expectType<Nodes>(node)
})
// Function test (explicit assertion).
visit(sampleTree, isHeading, function (node) {
expectType<Heading>(node)
expectType<1 | 2 | 3 | 4 | 5 | 6>(node.depth)
})
// Function test (explicit assertion).
visit(sampleTree, isHeading2, function (node) {
expectType<Heading & {depth: 2}>(node)
})
// ## Combined tests
visit(sampleTree, ['heading', {depth: 1}, isHeading], function (node) {
// Unfortunately TS casts things in arrays too vague.
expectType<Root | RootContent>(node)
})
// To do: update to `unist-util-is` should make this work?
// visit(
// sampleTree,
// ['heading', {depth: 1}, isHeading] as const,
// function (node) {
// // Unfortunately TS casts things in arrays too vague.
// expectType<Root | RootContent>(node)
// }
// )
// ## Return type: incorrect.
// @ts-expect-error: not an action.
visit(sampleTree, function () {
return 'random'
})
// @ts-expect-error: not a tuple: missing action.
visit(sampleTree, function () {
return [1]
})
// @ts-expect-error: not a tuple: incorrect action.
visit(sampleTree, function () {
return ['random', 1]
})
// ## Return type: action.
visit(sampleTree, function () {
return CONTINUE
})
visit(sampleTree, function () {
return EXIT
})
visit(sampleTree, function () {
return SKIP
})
// ## Return type: index.
visit(sampleTree, function () {
return 0
})
visit(sampleTree, function () {
return 1
})
// ## Return type: tuple.
visit(sampleTree, function () {
return [CONTINUE, 1]
})
visit(sampleTree, function () {
return [EXIT, 1]
})
visit(sampleTree, function () {
return [SKIP, 1]
})
visit(sampleTree, function () {
return [SKIP]
})
// ## Infer on tree
visit(sampleTree, 'tableCell', function (node) {
visit(node, function (node, _, parent) {
expectType<TableCell | PhrasingContent>(node)
expectType<
Delete | Emphasis | Link | LinkReference | Strong | TableCell | undefined
>(parent)
})
})
visit(sampleTree, 'definition', function (node) {
visit(node, function (node, _, parent) {
expectType<Definition>(node)
expectType<never>(parent)
})
})
function isHeading(node: Node): node is Heading {
return node ? node.type === 'heading' : false
}
function isHeading2(node: Node): node is Heading & {depth: 2} {
return isHeading(node) && node.depth === 2
}
function isHeadingLoose(node: Node) {
return node ? node.type === 'heading' : false
}