12
12
* @typedef {import('unist').Point } Point
13
13
*
14
14
* @typedef {import('hast').Root } Root
15
- * @typedef {import('hast').DocType } Doctype
15
+ * @typedef {import('hast').Doctype } Doctype
16
16
* @typedef {import('hast').Element } Element
17
17
* @typedef {import('hast').Text } Text
18
18
* @typedef {import('hast').Comment } Comment
19
- * @typedef {import('hast').Content } Content
19
+ * @typedef {import('hast').RootContent } RootContent
20
+ * @typedef {import('hast').Nodes } Nodes
20
21
*
21
22
* @typedef {import('mdast-util-to-hast').Raw } Raw
22
23
*/
23
24
24
25
/**
25
- * @typedef {Root | Content } Node
26
- *
27
- * @typedef {{type: 'comment', value: {stitch: Node}} } Stitch
26
+ * @typedef {{type: 'comment', value: {stitch: Nodes}} } Stitch
28
27
*
29
28
* @typedef Options
30
29
* Configuration.
40
39
* Info passed around about the current state.
41
40
* @property {Parser<DefaultTreeAdapterMap> } parser
42
41
* Current parser.
43
- * @property {(node: Node ) => void } handle
42
+ * @property {(node: Nodes ) => void } handle
44
43
* Add a hast node to the parser.
45
44
* @property {boolean } stitches
46
45
* Whether there are stitches.
@@ -75,16 +74,16 @@ const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
75
74
* Pass a hast tree through an HTML parser, which will fix nesting, and turn
76
75
* raw nodes into actual nodes.
77
76
*
78
- * @param {Node } tree
77
+ * @param {Nodes } tree
79
78
* Original hast tree to transform.
80
79
* @param {Options | null | undefined } [options]
81
80
* Configuration.
82
- * @returns {Node }
81
+ * @returns {Nodes }
83
82
* Parsed again tree.
84
83
*/
85
84
export function raw ( tree , options ) {
86
85
const document = documentMode ( tree )
87
- /** @type {(node: Node , state: State) => void } */
86
+ /** @type {(node: Nodes , state: State) => void } */
88
87
const one = zwitch ( 'type' , {
89
88
handlers : { root, element, text, comment, doctype, raw : handleRaw } ,
90
89
unknown
@@ -137,7 +136,7 @@ export function raw(tree, options) {
137
136
/**
138
137
* Transform all nodes
139
138
*
140
- * @param {Array<Content > } nodes
139
+ * @param {Array<RootContent > } nodes
141
140
* hast content.
142
141
* @param {State } state
143
142
* Info passed around about the current state.
@@ -247,7 +246,7 @@ function doctype(node, state) {
247
246
/**
248
247
* Transform a stitch.
249
248
*
250
- * @param {Node } node
249
+ * @param {Nodes } node
251
250
* unknown node.
252
251
* @param {State } state
253
252
* Info passed around about the current state.
@@ -258,7 +257,7 @@ function stitch(node, state) {
258
257
// Mark that there are stitches, so we need to walk the tree and revert them.
259
258
state . stitches = true
260
259
261
- /** @type {Node } */
260
+ /** @type {Nodes } */
262
261
const clone = cloneWithoutChildren ( node )
263
262
264
263
// Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
@@ -382,7 +381,7 @@ function handleRaw(node, state) {
382
381
* Never.
383
382
*/
384
383
function unknown ( node_ , state ) {
385
- const node = /** @type {Node } */ ( node_ )
384
+ const node = /** @type {Nodes } */ ( node_ )
386
385
387
386
if (
388
387
state . options . passThrough &&
@@ -406,7 +405,7 @@ function unknown(node_, state) {
406
405
*
407
406
* @param {State } state
408
407
* Info passed around about the current state.
409
- * @param {Point } point
408
+ * @param {Point | undefined } point
410
409
* Point.
411
410
* @returns {void }
412
411
* Nothing.
@@ -479,18 +478,13 @@ function resetTokenizer(state, point) {
479
478
*
480
479
* @param {State } state
481
480
* Info passed around about the current state.
482
- * @param {Point } point
481
+ * @param {Point | undefined } point
483
482
* Point.
484
483
* @returns {void }
485
484
* Nothing.
486
485
*/
487
486
function setPoint ( state , point ) {
488
- if (
489
- point . line &&
490
- point . column &&
491
- point . offset !== null &&
492
- point . offset !== undefined
493
- ) {
487
+ if ( point && point . offset !== undefined ) {
494
488
/** @type {Location } */
495
489
const location = {
496
490
startLine : point . line ,
@@ -646,7 +640,7 @@ function endTag(node, state) {
646
640
/**
647
641
* Check if `node` represents a whole document or a fragment.
648
642
*
649
- * @param {Node } node
643
+ * @param {Nodes } node
650
644
* hast node.
651
645
* @returns {boolean }
652
646
* Whether this represents a whole document or a fragment.
@@ -663,7 +657,7 @@ function documentMode(node) {
663
657
/**
664
658
* Get a `parse5` location from a node.
665
659
*
666
- * @param {Node | Stitch } node
660
+ * @param {Nodes | Stitch } node
667
661
* hast node.
668
662
* @returns {Location }
669
663
* `parse5` location.
@@ -673,19 +667,23 @@ function createParse5Location(node) {
673
667
const end = pointEnd ( node )
674
668
675
669
return {
676
- startLine : start . line ,
677
- startCol : start . column ,
678
670
// @ts -expect-error: could be `undefined` in hast, which `parse5` types don’t want.
679
- startOffset : start . offset ,
680
- endLine : end . line ,
681
- endCol : end . column ,
671
+ startLine : start ?. line ,
672
+ // @ts -expect-error: could be `undefined` in hast, which `parse5` types don’t want.
673
+ startCol : start ?. column ,
674
+ // @ts -expect-error: could be `undefined` in hast, which `parse5` types don’t want.
675
+ startOffset : start ?. offset ,
676
+ // @ts -expect-error: could be `undefined` in hast, which `parse5` types don’t want.
677
+ endLine : end ?. line ,
678
+ // @ts -expect-error: could be `undefined` in hast, which `parse5` types don’t want.
679
+ endCol : end ?. column ,
682
680
// @ts -expect-error: could be `undefined` in hast, which `parse5` types don’t want.
683
- endOffset : end . offset
681
+ endOffset : end ? .offset
684
682
}
685
683
}
686
684
687
685
/**
688
- * @template {Node } NodeType
686
+ * @template {Nodes } NodeType
689
687
* Node type.
690
688
* @param {NodeType } node
691
689
* Node to clone.
0 commit comments