Skip to content

Commit 961dbbc

Browse files
committed
Change types to use mdast types
1 parent f3f7db6 commit 961dbbc

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

index.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Literal<string>} Literal
4-
* @typedef {import('unist').Parent} Parent
52
* @typedef {import('unist').Point} Point
6-
* @typedef {import('mdast').Content} Content
3+
* @typedef {import('unist').Literal<string>} UnistLiteral
4+
* @typedef {import('unist').Parent} UnistParent
5+
* @typedef {import('unist').Node} UnistNode
6+
* @typedef {import('mdast').Root} MdastRoot
7+
* @typedef {import('mdast').Content} MdastContent
8+
* @typedef {MdastRoot|MdastContent} MdastNode
79
* @typedef {import('vfile').VFile} VFile
810
* @typedef {ReturnType<import('vfile-location').location>} Location
911
* @typedef {{
10-
* parse(nodes: Node[]): Node
11-
* tokenizeSource(value: string): Node
12-
* tokenizeWhiteSpace(value: string): Node
13-
* tokenize(value: string): Node[]
12+
* parse(nodes: UnistNode[]): UnistNode
13+
* tokenizeSource(value: string): UnistNode
14+
* tokenizeWhiteSpace(value: string): UnistNode
15+
* tokenize(value: string): UnistNode[]
1416
* }} ParserInstance
1517
* @typedef {new () => ParserInstance} ParserConstructor
1618
*
@@ -36,7 +38,7 @@ const defaultSource = ['inlineCode']
3638
/**
3739
* Transform a `tree` in mdast to nlcst.
3840
*
39-
* @param {Node} tree
41+
* @param {MdastNode} tree
4042
* @param {VFile} file
4143
* @param {ParserInstance|ParserConstructor} Parser
4244
* @param {Options} [options]
@@ -79,7 +81,6 @@ export function toNlcst(tree, file, Parser, options = {}) {
7981
? defaultSource.concat(options.source)
8082
: defaultSource
8183
},
82-
// @ts-expect-error assume mdast node.
8384
tree
8485
)
8586

@@ -91,8 +92,8 @@ export function toNlcst(tree, file, Parser, options = {}) {
9192
/**
9293
* Transform a single node.
9394
* @param {Context} config
94-
* @param {Content} node
95-
* @returns {Array.<Node>|undefined}
95+
* @param {MdastNode} node
96+
* @returns {Array.<UnistNode>|undefined}
9697
*/
9798
function one(config, node) {
9899
const start = node.position ? node.position.start.offset : undefined
@@ -135,18 +136,18 @@ function one(config, node) {
135136
/**
136137
* Transform all nodes in `parent`.
137138
* @param {Context} config
138-
* @param {Parent} parent
139-
* @returns {Array.<Node>}
139+
* @param {UnistParent} parent
140+
* @returns {Array.<UnistNode>}
140141
*/
141142
function all(config, parent) {
142143
let index = -1
143-
/** @type {Array.<Node>} */
144+
/** @type {Array.<UnistNode>} */
144145
const results = []
145146
/** @type {Point|undefined} */
146147
let end
147148

148149
while (++index < parent.children.length) {
149-
/** @type {Content} */
150+
/** @type {MdastContent} */
150151
// @ts-expect-error Assume `parent` is an mdast parent.
151152
const child = parent.children[index]
152153
const start = pointStart(child)
@@ -176,7 +177,7 @@ function all(config, parent) {
176177
* Patch a position on each node in `nodes`.
177178
* `offset` is the offset in `file` this run of content starts at.
178179
*
179-
* @template {Array.<Node>} T
180+
* @template {Array.<UnistNode>} T
180181
* @param {Context} config
181182
* @param {T} nodes
182183
* @param {number|undefined} offset
@@ -189,8 +190,7 @@ function patch(config, nodes, offset) {
189190
while (++index < nodes.length) {
190191
const node = nodes[index]
191192

192-
if ('children' in node) {
193-
// @ts-expect-error looks like a parent.
193+
if (parent(node)) {
194194
patch(config, node.children, start)
195195
}
196196

@@ -212,9 +212,17 @@ function patch(config, nodes, offset) {
212212
}
213213

214214
/**
215-
* @param {Node} node
216-
* @returns {node is Literal}
215+
* @param {UnistNode} node
216+
* @returns {node is UnistLiteral}
217217
*/
218218
function literal(node) {
219219
return 'value' in node
220220
}
221+
222+
/**
223+
* @param {UnistNode} node
224+
* @returns {node is UnistParent}
225+
*/
226+
function parent(node) {
227+
return 'children' in node
228+
}

test/index.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @typedef {import('unist').Node} Node
33
* @typedef {import('unist').Literal<string>} Literal
44
* @typedef {import('mdast').Root} Root
5+
* @typedef {import('mdast').Text} Text
56
* @typedef {import('mdast').InlineCode} InlineCode
67
* @typedef {import('vfile').VFile} VFile
78
*/
@@ -25,7 +26,7 @@ import {toNlcst} from '../index.js'
2526
test('mdast-util-to-nlcst', (t) => {
2627
t.throws(
2728
() => {
28-
// @ts-expect-error runtime.
29+
// @ts-expect-error runtime: too few arguments.
2930
toNlcst()
3031
},
3132
/mdast-util-to-nlcst expected node/,
@@ -34,7 +35,7 @@ test('mdast-util-to-nlcst', (t) => {
3435

3536
t.throws(
3637
() => {
37-
// @ts-expect-error runtime.
38+
// @ts-expect-error runtime: too few arguments.
3839
toNlcst({})
3940
},
4041
/mdast-util-to-nlcst expected node/,
@@ -43,7 +44,7 @@ test('mdast-util-to-nlcst', (t) => {
4344

4445
t.throws(
4546
() => {
46-
// @ts-expect-error runtime.
47+
// @ts-expect-error runtime: too few arguments.
4748
toNlcst({type: 'foo'})
4849
},
4950
/mdast-util-to-nlcst expected file/,
@@ -52,7 +53,7 @@ test('mdast-util-to-nlcst', (t) => {
5253

5354
t.throws(
5455
() => {
55-
// @ts-expect-error runtime.
56+
// @ts-expect-error runtime: too few arguments.
5657
toNlcst({type: 'foo'})
5758
},
5859
/mdast-util-to-nlcst expected file/,
@@ -61,7 +62,7 @@ test('mdast-util-to-nlcst', (t) => {
6162

6263
t.throws(
6364
() => {
64-
// @ts-expect-error runtime.
65+
// @ts-expect-error runtime: too few arguments.
6566
toNlcst({type: 'text', value: 'foo'}, {foo: 'bar'})
6667
},
6768
/mdast-util-to-nlcst expected file/,
@@ -70,9 +71,9 @@ test('mdast-util-to-nlcst', (t) => {
7071

7172
t.throws(
7273
() => {
73-
// @ts-expect-error runtime.
74+
// @ts-expect-error runtime: too few arguments.
7475
toNlcst(
75-
/** @type {Literal} */ ({type: 'text', value: 'foo'}),
76+
/** @type {Text} */ ({type: 'text', value: 'foo'}),
7677
vfile({contents: 'foo'})
7778
)
7879
},
@@ -83,7 +84,7 @@ test('mdast-util-to-nlcst', (t) => {
8384
t.throws(
8485
() => {
8586
toNlcst(
86-
/** @type {Literal} */ ({type: 'text', value: 'foo'}),
87+
/** @type {Text} */ ({type: 'text', value: 'foo'}),
8788
vfile(),
8889
ParseLatin
8990
)
@@ -94,7 +95,7 @@ test('mdast-util-to-nlcst', (t) => {
9495

9596
t.doesNotThrow(() => {
9697
toNlcst(
97-
/** @type {Literal} */ ({
98+
/** @type {Text} */ ({
9899
type: 'text',
99100
value: 'foo',
100101
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
@@ -106,7 +107,7 @@ test('mdast-util-to-nlcst', (t) => {
106107

107108
t.doesNotThrow(() => {
108109
toNlcst(
109-
/** @type {Literal} */ ({
110+
/** @type {Text} */ ({
110111
type: 'text',
111112
value: 'foo',
112113
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
@@ -122,7 +123,7 @@ test('mdast-util-to-nlcst', (t) => {
122123
{
123124
type: 'text',
124125
value: 'foo',
125-
// @ts-expect-error runtime.
126+
// @ts-expect-error runtime: incorrect positional info.
126127
position: {start: {}, end: {}}
127128
},
128129
vfile(),
@@ -225,40 +226,30 @@ test('Fixtures', (t) => {
225226
const base = path.join('test', 'fixtures')
226227
const files = fs.readdirSync(base)
227228
let index = -1
228-
/** @type {string} */
229-
let name
230-
/** @type {VFile} */
231-
let input
232-
/** @type {Node} */
233-
let expected
234-
/** @type {Node} */
235-
let mdast
236-
/** @type {Object.<string, unknown>|undefined} */
237-
let options
238229

239230
while (++index < files.length) {
240-
name = files[index]
231+
const name = files[index]
232+
/** @type {Object.<string, unknown>|undefined} */
233+
let options
241234

242235
if (isHidden(name)) continue
243236

244-
input = vfile.readSync(path.join(base, name, 'input.md'))
245-
expected = JSON.parse(
237+
const input = vfile.readSync(path.join(base, name, 'input.md'))
238+
/** @type {Node} */
239+
const expected = JSON.parse(
246240
String(vfile.readSync(path.join(base, name, 'output.json')))
247241
)
248242

249243
try {
250244
options = JSON.parse(
251245
String(vfile.readSync(path.join(base, name, 'options.json')))
252246
)
253-
} catch {
254-
options = undefined
255-
}
247+
} catch {}
256248

257249
const processor = remark()
258250
if (options && options.useRemarkGfm) processor.use(gfm)
259251
if (options && options.useRemarkFrontmatter) processor.use(frontmatter)
260-
261-
mdast = processor.parse(input)
252+
const mdast = /** @type {Root} */ (processor.parse(input))
262253

263254
t.deepEqual(
264255
toNlcst(mdast, input, ParseLatin, options),

0 commit comments

Comments
 (0)