Skip to content

Commit c1462b2

Browse files
committed
Use @types/nlcst
1 parent f69ae5c commit c1462b2

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

index.js

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
/**
22
* @typedef {import('unist').Point} Point
3-
* @typedef {import('unist').Literal<string>} UnistLiteral
4-
* @typedef {import('unist').Parent} UnistParent
5-
* @typedef {import('unist').Node} UnistNode
3+
*
4+
* @typedef {import('nlcst').Root} NlcstRoot
5+
* @typedef {import('nlcst').Content} NlcstContent
6+
* @typedef {import('nlcst').SentenceContent} NlcstSentenceContent
7+
* @typedef {import('nlcst').WhiteSpace} NlcstWhiteSpace
8+
* @typedef {import('nlcst').Source} NlcstSource
9+
* @typedef {NlcstRoot|NlcstContent} NlcstNode
10+
*
611
* @typedef {import('mdast').Root} MdastRoot
712
* @typedef {import('mdast').Content} MdastContent
813
* @typedef {MdastRoot|MdastContent} MdastNode
14+
* @typedef {Extract<MdastNode, import('unist').Parent>} MdastParent
15+
*
16+
*
917
* @typedef {import('vfile').VFile} VFile
1018
* @typedef {ReturnType<import('vfile-location').location>} Location
1119
* @typedef {{
12-
* parse(nodes: UnistNode[]): UnistNode
13-
* tokenizeSource(value: string): UnistNode
14-
* tokenizeWhiteSpace(value: string): UnistNode
15-
* tokenize(value: string): UnistNode[]
20+
* parse(nodes: NlcstContent[]): NlcstRoot
21+
* tokenizeSource(value: string): NlcstSource
22+
* tokenizeWhiteSpace(value: string): NlcstWhiteSpace
23+
* tokenize(value: string): NlcstSentenceContent[]
1624
* }} ParserInstance
1725
* @typedef {new () => ParserInstance} ParserConstructor
1826
*
1927
* @typedef Options
20-
* @property {Array.<string>} [ignore]
21-
* @property {Array.<string>} [source]
28+
* @property {string[]} [ignore]
29+
* @property {string[]} [source]
2230
*
2331
* @typedef Context
2432
* @property {string} doc
2533
* @property {Location} place
2634
* @property {ParserInstance} parser
27-
* @property {Array.<string>} ignore
28-
* @property {Array.<string>} source
35+
* @property {string[]} ignore
36+
* @property {string[]} source
2937
*/
3038

3139
import {toString} from 'nlcst-to-string'
@@ -93,7 +101,7 @@ export function toNlcst(tree, file, Parser, options = {}) {
93101
* Transform a single node.
94102
* @param {Context} config
95103
* @param {MdastNode} node
96-
* @returns {Array.<UnistNode>|undefined}
104+
* @returns {NlcstContent[]|undefined}
97105
*/
98106
function one(config, node) {
99107
const start = node.position ? node.position.start.offset : undefined
@@ -136,19 +144,17 @@ function one(config, node) {
136144
/**
137145
* Transform all nodes in `parent`.
138146
* @param {Context} config
139-
* @param {UnistParent} parent
140-
* @returns {Array.<UnistNode>}
147+
* @param {MdastParent} parent
148+
* @returns {NlcstContent[]}
141149
*/
142150
function all(config, parent) {
143151
let index = -1
144-
/** @type {Array.<UnistNode>} */
152+
/** @type {NlcstContent[]} */
145153
const results = []
146154
/** @type {Point|undefined} */
147155
let end
148156

149157
while (++index < parent.children.length) {
150-
/** @type {MdastContent} */
151-
// @ts-expect-error Assume `parent` is an mdast parent.
152158
const child = parent.children[index]
153159
const start = pointStart(child)
154160

@@ -158,7 +164,7 @@ function all(config, parent) {
158164
)
159165
patch(config, [lineEnding], end.offset)
160166

161-
if (literal(lineEnding) && lineEnding.value.length < 2) {
167+
if (lineEnding.value.length < 2) {
162168
lineEnding.value = '\n\n'
163169
}
164170

@@ -177,7 +183,7 @@ function all(config, parent) {
177183
* Patch a position on each node in `nodes`.
178184
* `offset` is the offset in `file` this run of content starts at.
179185
*
180-
* @template {Array.<UnistNode>} T
186+
* @template {NlcstContent[]} T
181187
* @param {Context} config
182188
* @param {T} nodes
183189
* @param {number|undefined} offset
@@ -190,7 +196,7 @@ function patch(config, nodes, offset) {
190196
while (++index < nodes.length) {
191197
const node = nodes[index]
192198

193-
if (parent(node)) {
199+
if ('children' in node) {
194200
patch(config, node.children, start)
195201
}
196202

@@ -210,19 +216,3 @@ function patch(config, nodes, offset) {
210216

211217
return nodes
212218
}
213-
214-
/**
215-
* @param {UnistNode} node
216-
* @returns {node is UnistLiteral}
217-
*/
218-
function literal(node) {
219-
return 'value' in node
220-
}
221-
222-
/**
223-
* @param {UnistNode} node
224-
* @returns {node is UnistParent}
225-
*/
226-
function parent(node) {
227-
return 'children' in node
228-
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
],
3636
"dependencies": {
3737
"@types/mdast": "^3.0.0",
38+
"@types/nlcst": "^1.0.0",
3839
"@types/unist": "^2.0.0",
3940
"nlcst-to-string": "^3.0.0",
4041
"unist-util-position": "^4.0.0",

0 commit comments

Comments
 (0)