Skip to content

Commit 40313e0

Browse files
committed
Use @types/nlcst
1 parent 9325136 commit 40313e0

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

Diff for: index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
/**
2-
* @typedef {import('unist').Node} Node
2+
* @typedef {import('nlcst').Content} Content
3+
* @typedef {import('nlcst').Root} Root
34
*/
45

56
/**
67
* Stringify one nlcst node or list of nodes.
78
*
8-
* @param {unknown} node
9+
* @param {Root|Content|Content[]} node
910
* @param {string} [separator='']
1011
* @returns {string}
1112
*/
1213
export function toString(node, separator = '') {
1314
let index = -1
1415

15-
// @ts-expect-error Looks like an object.
1616
if (!node || (!Array.isArray(node) && !node.type)) {
1717
throw new Error('Expected node, not `' + node + '`')
1818
}
1919

2020
// @ts-expect-error Looks like a literal.
2121
if (typeof node.value === 'string') return node.value
2222

23-
/** @type {Array.<Node>} */
23+
/** @type {Array.<Content|Root>} */
2424
// @ts-expect-error Looks like a list of nodes or parent.
2525
const children = (Array.isArray(node) ? node : node.children) || []
2626

2727
// Shortcut: This is pretty common, and a small performance win.
2828
if (children.length === 1 && 'value' in children[0]) {
29-
// @ts-expect-error Looks like a literal.
3029
return children[0].value
3130
}
3231

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"index.js"
3434
],
3535
"dependencies": {
36-
"@types/unist": "^2.0.0"
36+
"@types/nlcst": "^1.0.0"
3737
},
3838
"devDependencies": {
3939
"@types/tape": "^4.0.0",

Diff for: test.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,62 @@ test('toString()', (t) => {
1414

1515
t.throws(
1616
() => {
17+
// @ts-expect-error: missing `type`.
1718
toString({value: 'foo'})
1819
},
1920
/\[object Object]/,
2021
'should throw when not given a node (#2)'
2122
)
2223

23-
t.equal(toString(u('foo', 'AT')), 'AT', 'should support texts')
24+
t.equal(toString(u('TextNode', 'AT')), 'AT', 'should support texts')
2425

2526
t.equal(
26-
toString(u('foo', [u('bar', 'AT'), u('bar', '&'), u('bar', 'T')])),
27+
toString(
28+
u('WordNode', [
29+
u('TextNode', 'AT'),
30+
u('SymbolNode', '&'),
31+
u('TextNode', 'T')
32+
])
33+
),
2734
'AT&T',
2835
'should support parents'
2936
)
3037

3138
t.equal(
32-
toString([u('bar', 'AT'), u('bar', '&'), u('bar', 'T')]),
39+
toString([u('TextNode', 'AT'), u('SymbolNode', '&'), u('TextNode', 'T')]),
3340
'AT&T',
3441
'should support nodes'
3542
)
3643

3744
t.equal(
3845
toString(
39-
u('foo', [u('bar', 'AT'), u('foo', [u('bar', '&')]), u('bar', 'T')])
46+
// @ts-expect-error: custom.
47+
u('WordNode', [
48+
u('TextNode', 'AT'),
49+
u('SomeNode', [u('TextNode', '&')]),
50+
u('TextNode', 'T')
51+
])
4052
),
4153
'AT&T',
4254
'should support parents with mixed children'
4355
)
4456

4557
t.equal(
4658
toString(
47-
u('foo', [u('bar', 'AT'), u('foo', [u('bar', '&')]), u('bar', 'T')]),
59+
// @ts-expect-error: custom.
60+
u('WordNode', [
61+
u('TextNode', 'AT'),
62+
u('WordNode', [u('TextNode', '&')]),
63+
u('TextNode', 'T')
64+
]),
4865
','
4966
),
5067
'AT,&,T',
5168
'should support separators'
5269
)
5370

54-
t.equal(toString(u('foo')), '', 'should support voids')
71+
// @ts-expect-error: custom node.
72+
t.equal(toString(u('VoidNode')), '', 'should support voids')
5573

5674
t.end()
5775
})

0 commit comments

Comments
 (0)