Skip to content

Commit 4dcff31

Browse files
committed
Update @types/unist
1 parent befc0b3 commit 4dcff31

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

index.test-d.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
import {expectAssignable, expectNotType, expectType} from 'tsd'
22
import type {
33
Blockquote,
4-
Content,
54
Definition,
65
Delete,
76
Emphasis,
8-
Footnote,
97
FootnoteDefinition,
108
Heading,
119
Link,
1210
LinkReference,
1311
ListItem,
12+
Nodes,
13+
Parents,
1414
PhrasingContent,
1515
Root,
16+
RootContent,
1617
Strong,
1718
TableCell,
1819
TableRow
1920
} from 'mdast'
2021
import type {Node, Parent} from 'unist'
21-
import {CONTINUE, EXIT, SKIP, visit} from 'unist-util-visit'
22+
import {CONTINUE, EXIT, SKIP, visit} from './index.js'
2223

23-
// To do: use `mdast` when released.
24-
type Nodes = Root | Content
25-
26-
// To do: use `mdast` when released.
27-
type Parents = Extract<Nodes, Parent>
28-
29-
/* Setup */
24+
// Setup.
3025
const implicitTree = {
3126
type: 'root',
3227
children: [{type: 'heading', depth: 1, children: []}]
@@ -132,7 +127,7 @@ visit(sampleTree, isHeading2, function (node) {
132127
// ## Combined tests
133128
visit(sampleTree, ['heading', {depth: 1}, isHeading], function (node) {
134129
// Unfortunately TS casts things in arrays too vague.
135-
expectType<Root | Content>(node)
130+
expectType<Root | RootContent>(node)
136131
})
137132

138133
// To do: update to `unist-util-is` should make this work?
@@ -141,7 +136,7 @@ visit(sampleTree, ['heading', {depth: 1}, isHeading], function (node) {
141136
// ['heading', {depth: 1}, isHeading] as const,
142137
// function (node) {
143138
// // Unfortunately TS casts things in arrays too vague.
144-
// expectType<Root | Content>(node)
139+
// expectType<Root | RootContent>(node)
145140
// }
146141
// )
147142

@@ -197,14 +192,7 @@ visit(sampleTree, 'tableCell', function (node) {
197192
visit(node, function (node, _, parent) {
198193
expectType<TableCell | PhrasingContent>(node)
199194
expectType<
200-
| Delete
201-
| Emphasis
202-
| Footnote
203-
| Link
204-
| LinkReference
205-
| Strong
206-
| TableCell
207-
| undefined
195+
Delete | Emphasis | Link | LinkReference | Strong | TableCell | undefined
208196
>(parent)
209197
})
210198
})

lib/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
/**
22
* @typedef {import('unist').Node} UnistNode
33
* @typedef {import('unist').Parent} UnistParent
4-
* @typedef {import('unist-util-is').Test} Test
54
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
65
*/
76

7+
/**
8+
* @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
9+
* Test from `unist-util-is`.
10+
*
11+
* Note: we have remove and add `undefined`, because otherwise when generating
12+
* automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
13+
* which doesn’t work when publishing on npm.
14+
*/
15+
816
// To do: use types from `unist-util-visit-parents` when it’s released.
917

1018
/**

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
"index.js"
4848
],
4949
"dependencies": {
50-
"@types/unist": "^2.0.0",
51-
"unist-util-is": "^5.0.0",
52-
"unist-util-visit-parents": "^5.1.1"
50+
"@types/unist": "^3.0.0",
51+
"unist-util-is": "^6.0.0",
52+
"unist-util-visit-parents": "^6.0.0"
5353
},
5454
"devDependencies": {
55-
"@types/mdast": "^3.0.0",
55+
"@types/mdast": "^4.0.0",
5656
"@types/node": "^20.0.0",
5757
"c8": "^8.0.0",
5858
"mdast-util-from-markdown": "^1.0.0",

test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @typedef {import('mdast').Root} Root
33
* @typedef {import('unist').Node} Node
4-
* @typedef {import('unist').Parent} Parent
54
*/
65

76
import assert from 'node:assert/strict'
@@ -11,7 +10,10 @@ import {gfmFromMarkdown} from 'mdast-util-gfm'
1110
import {gfm} from 'micromark-extension-gfm'
1211
import {CONTINUE, EXIT, SKIP, visit} from 'unist-util-visit'
1312

14-
const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')
13+
// To do: remove cast after update.
14+
const tree = /** @type {Root} */ (
15+
fromMarkdown('Some _emphasis_, **importance**, and `code`.')
16+
)
1517

1618
const stopIndex = 5
1719
const skipIndex = 7
@@ -145,10 +147,9 @@ test('visit', async function (t) {
145147

146148
assert.equal(n, 3, 'should visit all passing nodes')
147149

148-
// To do: when `unist-util-is` updates, we can drop `null`.
149150
/**
150151
* @param {Node} _
151-
* @param {number | null | undefined} index
152+
* @param {number | undefined} index
152153
*/
153154
function test(_, index) {
154155
return typeof index === 'number' && index > 3

0 commit comments

Comments
 (0)