117
117
* Capture some of the output data.
118
118
* @property {(this: CompileContext) => string } resume
119
119
* Stop capturing and access the output data.
120
- * @property {<Kind extends Nodes> (this: CompileContext, node: Kind , token: Token, onError?: OnEnterError) => Kind } enter
120
+ * @property {(this: CompileContext, node: Nodes , token: Token, onError?: OnEnterError) => undefined } enter
121
121
* Enter a token.
122
- * @property {(this: CompileContext, token: Token, onError?: OnExitError) => Nodes } exit
122
+ * @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined } exit
123
123
* Exit a token.
124
124
* @property {TokenizeContext['sliceSerialize'] } sliceSerialize
125
125
* Get the string value of a token.
135
135
* Configuration.
136
136
*/
137
137
138
- // To do: next major: don’t return given `Nodes` from `enter`.
139
138
// To do: next major: remove setter/getter.
140
139
141
140
import { ok as assert } from 'devlop'
@@ -517,15 +516,16 @@ function compiler(options) {
517
516
518
517
// Create a new list item.
519
518
if ( event [ 1 ] . type === types . listItemPrefix ) {
520
- listItem = {
519
+ /** @type {Token } */
520
+ const item = {
521
521
type : 'listItem' ,
522
522
_spread : false ,
523
523
start : Object . assign ( { } , event [ 1 ] . start ) ,
524
524
// @ts -expect-error: we’ll add `end` in a second.
525
525
end : undefined
526
526
}
527
- // @ts -expect-error: ` listItem` is most definitely defined, TS...
528
- events . splice ( index , 0 , [ 'enter' , listItem , event [ 2 ] ] )
527
+ listItem = item
528
+ events . splice ( index , 0 , [ 'enter' , item , event [ 2 ] ] )
529
529
index ++
530
530
length ++
531
531
firstBlankLineIndex = undefined
@@ -601,30 +601,31 @@ function compiler(options) {
601
601
}
602
602
603
603
/**
604
- * @template {Nodes} Kind
605
- * Node type.
606
604
* @this {CompileContext}
607
605
* Context.
608
- * @param {Kind } node
606
+ * @param {Nodes } node
609
607
* Node to enter.
610
608
* @param {Token } token
611
609
* Corresponding token.
612
610
* @param {OnEnterError | undefined } [errorHandler]
613
611
* Handle the case where this token is open, but it is closed by something else.
614
- * @returns {Kind }
615
- * The given node .
612
+ * @returns {undefined }
613
+ * Nothing .
616
614
*/
617
615
function enter ( node , token , errorHandler ) {
618
616
const parent = this . stack [ this . stack . length - 1 ]
619
617
assert ( parent , 'expected `parent`' )
620
618
assert ( 'children' in parent , 'expected `parent`' )
621
- // @ts -expect-error: Assume `Node` can exist as a child of `parent`.
622
- parent . children . push ( node )
619
+ /** @type {Array<Nodes> } */
620
+ const siblings = parent . children
621
+ siblings . push ( node )
623
622
this . stack . push ( node )
624
623
this . tokenStack . push ( [ token , errorHandler ] )
625
- // @ts -expect-error: `end` will be patched later.
626
- node . position = { start : point ( token . start ) }
627
- return node
624
+ node . position = {
625
+ start : point ( token . start ) ,
626
+ // @ts -expect-error: `end` will be patched later.
627
+ end : undefined
628
+ }
628
629
}
629
630
630
631
/**
@@ -656,8 +657,8 @@ function compiler(options) {
656
657
* Corresponding token.
657
658
* @param {OnExitError | undefined } [onExitError]
658
659
* Handle the case where another token is open.
659
- * @returns {Nodes }
660
- * The closed node .
660
+ * @returns {undefined }
661
+ * Nothing .
661
662
*/
662
663
function exit ( token , onExitError ) {
663
664
const node = this . stack . pop ( )
@@ -684,7 +685,6 @@ function compiler(options) {
684
685
assert ( node . type !== 'fragment' , 'unexpected fragment `exit`ed' )
685
686
assert ( node . position , 'expected `position` to be defined' )
686
687
node . position . end = point ( token . end )
687
- return node
688
688
}
689
689
690
690
/**
@@ -892,16 +892,20 @@ function compiler(options) {
892
892
const node = this . stack [ this . stack . length - 1 ]
893
893
assert ( node , 'expected node on stack' )
894
894
assert ( 'children' in node , 'expected parent on stack' )
895
+ /** @type {Array<Nodes> } */
896
+ const siblings = node . children
895
897
896
- let tail = node . children [ node . children . length - 1 ]
898
+ let tail = siblings [ siblings . length - 1 ]
897
899
898
900
if ( ! tail || tail . type !== 'text' ) {
899
901
// Add a new text node.
900
902
tail = text ( )
901
- // @ts -expect-error: we’ll add `end` later.
902
- tail . position = { start : point ( token . start ) }
903
- // @ts -expect-error: Assume `parent` accepts `text`.
904
- node . children . push ( tail )
903
+ tail . position = {
904
+ start : point ( token . start ) ,
905
+ // @ts -expect-error: we’ll add `end` later.
906
+ end : undefined
907
+ }
908
+ siblings . push ( tail )
905
909
}
906
910
907
911
this . stack . push ( tail )
@@ -1301,8 +1305,12 @@ function compiler(options) {
1301
1305
1302
1306
/** @returns {Heading } */
1303
1307
function heading ( ) {
1304
- // @ts -expect-error `depth` will be set later.
1305
- return { type : 'heading' , depth : undefined , children : [ ] }
1308
+ return {
1309
+ type : 'heading' ,
1310
+ // @ts -expect-error `depth` will be set later.
1311
+ depth : 0 ,
1312
+ children : [ ]
1313
+ }
1306
1314
}
1307
1315
1308
1316
/** @returns {Break } */
0 commit comments