1
1
/**
2
2
* @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
+ *
6
11
* @typedef {import('mdast').Root } MdastRoot
7
12
* @typedef {import('mdast').Content } MdastContent
8
13
* @typedef {MdastRoot|MdastContent } MdastNode
14
+ * @typedef {Extract<MdastNode, import('unist').Parent> } MdastParent
15
+ *
16
+ *
9
17
* @typedef {import('vfile').VFile } VFile
10
18
* @typedef {ReturnType<import('vfile-location').location> } Location
11
19
* @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 []
16
24
* }} ParserInstance
17
25
* @typedef {new () => ParserInstance } ParserConstructor
18
26
*
19
27
* @typedef Options
20
- * @property {Array.< string> } [ignore]
21
- * @property {Array.< string> } [source]
28
+ * @property {string[] } [ignore]
29
+ * @property {string[] } [source]
22
30
*
23
31
* @typedef Context
24
32
* @property {string } doc
25
33
* @property {Location } place
26
34
* @property {ParserInstance } parser
27
- * @property {Array.< string> } ignore
28
- * @property {Array.< string> } source
35
+ * @property {string[] } ignore
36
+ * @property {string[] } source
29
37
*/
30
38
31
39
import { toString } from 'nlcst-to-string'
@@ -93,7 +101,7 @@ export function toNlcst(tree, file, Parser, options = {}) {
93
101
* Transform a single node.
94
102
* @param {Context } config
95
103
* @param {MdastNode } node
96
- * @returns {Array.<UnistNode> |undefined }
104
+ * @returns {NlcstContent[] |undefined }
97
105
*/
98
106
function one ( config , node ) {
99
107
const start = node . position ? node . position . start . offset : undefined
@@ -136,19 +144,17 @@ function one(config, node) {
136
144
/**
137
145
* Transform all nodes in `parent`.
138
146
* @param {Context } config
139
- * @param {UnistParent } parent
140
- * @returns {Array.<UnistNode> }
147
+ * @param {MdastParent } parent
148
+ * @returns {NlcstContent[] }
141
149
*/
142
150
function all ( config , parent ) {
143
151
let index = - 1
144
- /** @type {Array.<UnistNode> } */
152
+ /** @type {NlcstContent[] } */
145
153
const results = [ ]
146
154
/** @type {Point|undefined } */
147
155
let end
148
156
149
157
while ( ++ index < parent . children . length ) {
150
- /** @type {MdastContent } */
151
- // @ts -expect-error Assume `parent` is an mdast parent.
152
158
const child = parent . children [ index ]
153
159
const start = pointStart ( child )
154
160
@@ -158,7 +164,7 @@ function all(config, parent) {
158
164
)
159
165
patch ( config , [ lineEnding ] , end . offset )
160
166
161
- if ( literal ( lineEnding ) && lineEnding . value . length < 2 ) {
167
+ if ( lineEnding . value . length < 2 ) {
162
168
lineEnding . value = '\n\n'
163
169
}
164
170
@@ -177,7 +183,7 @@ function all(config, parent) {
177
183
* Patch a position on each node in `nodes`.
178
184
* `offset` is the offset in `file` this run of content starts at.
179
185
*
180
- * @template {Array.<UnistNode> } T
186
+ * @template {NlcstContent[] } T
181
187
* @param {Context } config
182
188
* @param {T } nodes
183
189
* @param {number|undefined } offset
@@ -190,7 +196,7 @@ function patch(config, nodes, offset) {
190
196
while ( ++ index < nodes . length ) {
191
197
const node = nodes [ index ]
192
198
193
- if ( parent ( node ) ) {
199
+ if ( 'children' in node ) {
194
200
patch ( config , node . children , start )
195
201
}
196
202
@@ -210,19 +216,3 @@ function patch(config, nodes, offset) {
210
216
211
217
return nodes
212
218
}
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
- }
0 commit comments