Skip to content

Commit cf430ef

Browse files
committed
Use @types/nlcst
1 parent beb6998 commit cf430ef

File tree

3 files changed

+78
-55
lines changed

3 files changed

+78
-55
lines changed

lib/index.js

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
/**
2-
* @typedef {import('unist').Node} UnistNode
3-
* @typedef {import('hast').Parent} Parent
4-
* @typedef {import('hast').Root} Root
5-
* @typedef {import('hast').Element} Element
6-
* @typedef {Parent['children'][number]} Child
7-
* @typedef {Element['children'][number]} ElementChild
8-
* @typedef {Child|Root} Node
2+
* @typedef {import('unist').Point} Point
3+
*
4+
* @typedef {import('nlcst').Root} NlcstRoot
5+
* @typedef {import('nlcst').Paragraph} NlcstParagraph
6+
* @typedef {import('nlcst').WhiteSpace} NlcstWhiteSpace
7+
* @typedef {import('nlcst').Source} NlcstSource
8+
* @typedef {import('nlcst').Content} NlcstContent
9+
* @typedef {import('nlcst').SentenceContent} NlcstSentenceContent
10+
* @typedef {NlcstRoot|NlcstContent} NlcstNode
11+
*
12+
* @typedef {import('hast').Root} HastRoot
13+
* @typedef {import('hast').Element} HastElement
14+
* @typedef {import('hast').Content} HastContent
15+
* @typedef {import('hast').ElementContent} HastElementContent
16+
* @typedef {HastRoot|HastContent} HastNode
17+
* @typedef {Extract<HastNode, import('unist').Parent>} HastParent
18+
*
919
* @typedef {import('vfile').VFile} VFile
1020
*
1121
* @typedef {{
12-
* parse(nodes: UnistNode[]): UnistNode
13-
* tokenizeSource(value: string): UnistNode
14-
* tokenizeWhiteSpace(value: string): UnistNode
15-
* tokenizeParagraph(nodes: UnistNode[]): UnistNode
16-
* tokenize(value: string): UnistNode[]
22+
* parse(nodes: NlcstContent[]): NlcstRoot
23+
* tokenizeSource(value: string): NlcstSource
24+
* tokenizeWhiteSpace(value: string): NlcstWhiteSpace
25+
* tokenizeParagraph(nodes: NlcstSentenceContent[]): NlcstParagraph
26+
* tokenize(value: string): NlcstSentenceContent[]
1727
* }} ParserInstance
1828
* @typedef {new () => ParserInstance} ParserConstructor
1929
*/
@@ -64,15 +74,13 @@ const flowAccepting = convertElement([
6474
'dialog'
6575
])
6676

67-
// See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
68-
const unravelInParagraph = convertElement(['a', 'ins', 'del', 'map'])
69-
7077
/**
7178
* Transform `tree` to nlcst.
7279
*
73-
* @param {Node} tree
80+
* @param {HastNode} tree
7481
* @param {VFile} file
7582
* @param {ParserInstance|ParserConstructor} Parser
83+
* @returns {NlcstRoot}
7684
*/
7785
export function toNlcst(tree, file, Parser) {
7886
// Warn for invalid parameters.
@@ -96,7 +104,7 @@ export function toNlcst(tree, file, Parser) {
96104
const doc = String(file)
97105
const loc = location(doc)
98106
const parser = 'parse' in Parser ? Parser : new Parser()
99-
/** @type {Array.<UnistNode>} */
107+
/** @type {NlcstContent[]} */
100108
const results = []
101109

102110
find(tree)
@@ -108,7 +116,7 @@ export function toNlcst(tree, file, Parser) {
108116
}
109117

110118
/**
111-
* @param {Node} node
119+
* @param {HastNode} node
112120
*/
113121
function find(node) {
114122
if (node.type === 'root') {
@@ -128,7 +136,7 @@ export function toNlcst(tree, file, Parser) {
128136
}
129137

130138
/**
131-
* @param {Array.<Child>} children
139+
* @param {HastContent[]} children
132140
*/
133141
function findAll(children) {
134142
let index = -1
@@ -139,41 +147,48 @@ export function toNlcst(tree, file, Parser) {
139147
}
140148

141149
/**
142-
* @param {Array.<ElementChild>} children
143-
* @returns {Array.<ElementChild>}
150+
* @param {HastElementContent[]} children
151+
* @returns {HastElementContent[]}
144152
*/
145153
function flattenAll(children) {
146-
/** @type {Array.<ElementChild>} */
154+
/** @type {HastElementContent[]} */
147155
const results = []
148156
let index = -1
149157

150158
while (++index < children.length) {
151-
if (unravelInParagraph(children[index])) {
152-
// @ts-ignore Is element.
153-
results.push(...flattenAll(children[index].children))
159+
const child = children[index]
160+
161+
// See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
162+
if (
163+
child.type === 'element' &&
164+
(child.tagName === 'a' ||
165+
child.tagName === 'ins' ||
166+
child.tagName === 'del' ||
167+
child.tagName === 'map')
168+
) {
169+
results.push(...flattenAll(child.children))
154170
} else {
155-
results.push(children[index])
171+
results.push(child)
156172
}
157173
}
158174

159175
return results
160176
}
161177

162178
/**
163-
* @param {Array.<Node>|Node} node
179+
* @param {HastElementContent|HastElementContent[]} node
164180
*/
165181
function add(node) {
166-
/** @type {Array.<UnistNode>} */
167-
// @ts-ignore Assume child.
182+
/** @type {NlcstSentenceContent[]|undefined} */
168183
const result = Array.isArray(node) ? all(node) : one(node)
169184

170-
if (result.length > 0) {
185+
if (result && result.length > 0) {
171186
results.push(parser.tokenizeParagraph(result))
172187
}
173188
}
174189

175190
/**
176-
* @param {Array.<ElementChild>} children
191+
* @param {HastElementContent[]} children
177192
*/
178193
function implicit(children) {
179194
let index = -1
@@ -209,11 +224,11 @@ export function toNlcst(tree, file, Parser) {
209224
/**
210225
* Convert `node` (hast) to nlcst.
211226
*
212-
* @param {Node} node
213-
* @returns {Array.<UnistNode>|undefined}
227+
* @param {HastContent} node
228+
* @returns {NlcstSentenceContent[]|undefined}
214229
*/
215230
function one(node) {
216-
/** @type {Array.<UnistNode>|undefined} */
231+
/** @type {NlcstSentenceContent[]|undefined} */
217232
let replacement
218233
/** @type {boolean|undefined} */
219234
let change
@@ -244,11 +259,11 @@ export function toNlcst(tree, file, Parser) {
244259
/**
245260
* Convert all `children` (hast) to nlcst.
246261
*
247-
* @param {Array.<Child>} children
248-
* @returns {Array.<UnistNode>}
262+
* @param {HastContent[]} children
263+
* @returns {NlcstSentenceContent[]}
249264
*/
250265
function all(children) {
251-
/** @type {Array.<UnistNode>} */
266+
/** @type {NlcstSentenceContent[]} */
252267
const results = []
253268
let index = -1
254269

@@ -266,7 +281,7 @@ export function toNlcst(tree, file, Parser) {
266281
* Note that nlcst nodes are concrete, meaning that their starting and ending
267282
* positions can be inferred from their content.
268283
*
269-
* @template {Array.<UnistNode>} T
284+
* @template {NlcstContent[]} T
270285
* @param {T} nodes
271286
* @param {ReturnType<location>} location
272287
* @param {number} offset
@@ -280,7 +295,6 @@ export function toNlcst(tree, file, Parser) {
280295
const node = nodes[index]
281296

282297
if ('children' in node) {
283-
// @ts-ignore Looks like a parent.
284298
patch(node.children, location, start)
285299
}
286300

@@ -299,15 +313,15 @@ export function toNlcst(tree, file, Parser) {
299313
}
300314

301315
/**
302-
* @param {Element} node
316+
* @param {HastElement} node
303317
* @returns {boolean}
304318
*/
305319
function dataNlcstSourced(node) {
306320
return Boolean(node.properties && node.properties.dataNlcst === 'source')
307321
}
308322

309323
/**
310-
* @param {Element} node
324+
* @param {HastElement} node
311325
* @returns {boolean}
312326
*/
313327
function dataNlcstIgnore(node) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
],
3838
"dependencies": {
3939
"@types/hast": "^2.0.0",
40+
"@types/nlcst": "^1.0.0",
4041
"@types/unist": "^2.0.0",
4142
"hast-util-embedded": "^2.0.0",
4243
"hast-util-is-element": "^2.0.0",

test/index.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {toNlcst} from '../index.js'
1515
test('hast-util-to-nlcst', (t) => {
1616
t.throws(
1717
() => {
18-
// @ts-ignore runtime.
18+
// @ts-expect-error runtime.
1919
toNlcst()
2020
},
2121
/hast-util-to-nlcst expected node/,
@@ -24,7 +24,7 @@ test('hast-util-to-nlcst', (t) => {
2424

2525
t.throws(
2626
() => {
27-
// @ts-ignore runtime.
27+
// @ts-expect-error runtime.
2828
toNlcst({})
2929
},
3030
/hast-util-to-nlcst expected node/,
@@ -33,7 +33,7 @@ test('hast-util-to-nlcst', (t) => {
3333

3434
t.throws(
3535
() => {
36-
// @ts-ignore runtime.
36+
// @ts-expect-error runtime.
3737
toNlcst({type: 'foo'})
3838
},
3939
/hast-util-to-nlcst expected file/,
@@ -42,7 +42,7 @@ test('hast-util-to-nlcst', (t) => {
4242

4343
t.throws(
4444
() => {
45-
// @ts-ignore runtime.
45+
// @ts-expect-error runtime.
4646
toNlcst({type: 'foo'})
4747
},
4848
/hast-util-to-nlcst expected file/,
@@ -51,7 +51,7 @@ test('hast-util-to-nlcst', (t) => {
5151

5252
t.throws(
5353
() => {
54-
// @ts-ignore runtime.
54+
// @ts-expect-error runtime.
5555
toNlcst({type: 'text', value: 'foo'}, {foo: 'bar'})
5656
},
5757
/hast-util-to-nlcst expected file/,
@@ -60,7 +60,7 @@ test('hast-util-to-nlcst', (t) => {
6060

6161
t.throws(
6262
() => {
63-
// @ts-ignore runtime.
63+
// @ts-expect-error runtime.
6464
toNlcst({type: 'text', value: 'foo'}, new VFile('foo'))
6565
},
6666
/hast-util-to-nlcst expected parser/,
@@ -111,7 +111,7 @@ test('hast-util-to-nlcst', (t) => {
111111
{
112112
type: 'text',
113113
value: 'foo',
114-
// @ts-ignore runtime.
114+
// @ts-expect-error runtime.
115115
position: {start: {}, end: {}}
116116
},
117117
new VFile(),
@@ -122,7 +122,7 @@ test('hast-util-to-nlcst', (t) => {
122122
'should fail when not given positional information (#2)'
123123
)
124124

125-
t.test('should accept nodes without offsets', (st) => {
125+
t.test('should accept nodes without offsets', (t) => {
126126
const node = toNlcst(
127127
{
128128
type: 'text',
@@ -136,13 +136,21 @@ test('hast-util-to-nlcst', (t) => {
136136
ParseLatin
137137
)
138138

139-
st.equal(node.position.start.offset, 0, 'should set starting offset')
140-
st.equal(node.position.end.offset, 3, 'should set ending offset')
139+
t.equal(
140+
node.position && node.position.start.offset,
141+
0,
142+
'should set starting offset'
143+
)
144+
t.equal(
145+
node.position && node.position.end.offset,
146+
3,
147+
'should set ending offset'
148+
)
141149

142-
st.end()
150+
t.end()
143151
})
144152

145-
t.test('should accept comments', (st) => {
153+
t.test('should accept comments', (t) => {
146154
const node = toNlcst(
147155
{
148156
type: 'comment',
@@ -153,7 +161,7 @@ test('hast-util-to-nlcst', (t) => {
153161
ParseLatin
154162
)
155163

156-
st.deepEqual(
164+
t.deepEqual(
157165
node,
158166
{
159167
type: 'RootNode',
@@ -166,7 +174,7 @@ test('hast-util-to-nlcst', (t) => {
166174
'should support comments'
167175
)
168176

169-
st.end()
177+
t.end()
170178
})
171179

172180
t.end()

0 commit comments

Comments
 (0)