Skip to content

Commit

Permalink
Update @types/unist
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 7, 2023
1 parent befc0b3 commit 4dcff31
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
28 changes: 8 additions & 20 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import {expectAssignable, expectNotType, expectType} from 'tsd'
import type {
Blockquote,
Content,
Definition,
Delete,
Emphasis,
Footnote,
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 'unist-util-visit'
import {CONTINUE, EXIT, SKIP, visit} from './index.js'

// To do: use `mdast` when released.
type Nodes = Root | Content

// To do: use `mdast` when released.
type Parents = Extract<Nodes, Parent>

/* Setup */
// Setup.
const implicitTree = {
type: 'root',
children: [{type: 'heading', depth: 1, children: []}]
Expand Down Expand Up @@ -132,7 +127,7 @@ visit(sampleTree, isHeading2, function (node) {
// ## Combined tests
visit(sampleTree, ['heading', {depth: 1}, isHeading], function (node) {
// Unfortunately TS casts things in arrays too vague.
expectType<Root | Content>(node)
expectType<Root | RootContent>(node)
})

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

Expand Down Expand Up @@ -197,14 +192,7 @@ visit(sampleTree, 'tableCell', function (node) {
visit(node, function (node, _, parent) {
expectType<TableCell | PhrasingContent>(node)
expectType<
| Delete
| Emphasis
| Footnote
| Link
| LinkReference
| Strong
| TableCell
| undefined
Delete | Emphasis | Link | LinkReference | Strong | TableCell | undefined
>(parent)
})
})
Expand Down
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/**
* @typedef {import('unist').Node} UnistNode
* @typedef {import('unist').Parent} UnistParent
* @typedef {import('unist-util-is').Test} Test
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
*/

/**
* @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
* Test from `unist-util-is`.
*
* Note: we have remove and add `undefined`, because otherwise when generating
* automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
* which doesn’t work when publishing on npm.
*/

// To do: use types from `unist-util-visit-parents` when it’s released.

/**
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0",
"unist-util-is": "^5.0.0",
"unist-util-visit-parents": "^5.1.1"
"@types/unist": "^3.0.0",
"unist-util-is": "^6.0.0",
"unist-util-visit-parents": "^6.0.0"
},
"devDependencies": {
"@types/mdast": "^3.0.0",
"@types/mdast": "^4.0.0",
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"mdast-util-from-markdown": "^1.0.0",
Expand Down
9 changes: 5 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*/

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

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

const stopIndex = 5
const skipIndex = 7
Expand Down Expand Up @@ -145,10 +147,9 @@ test('visit', async function (t) {

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

// To do: when `unist-util-is` updates, we can drop `null`.
/**
* @param {Node} _
* @param {number | null | undefined} index
* @param {number | undefined} index
*/
function test(_, index) {
return typeof index === 'number' && index > 3
Expand Down

0 comments on commit 4dcff31

Please sign in to comment.