Skip to content

Commit 18f4bb0

Browse files
committed
Change to return undefined from enter, exit
If you make extensions to this, get the current node yourself.
1 parent 779364c commit 18f4bb0

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

dev/lib/index.js

+34-26
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@
117117
* Capture some of the output data.
118118
* @property {(this: CompileContext) => string} resume
119119
* 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
121121
* Enter a token.
122-
* @property {(this: CompileContext, token: Token, onError?: OnExitError) => Nodes} exit
122+
* @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined} exit
123123
* Exit a token.
124124
* @property {TokenizeContext['sliceSerialize']} sliceSerialize
125125
* Get the string value of a token.
@@ -135,7 +135,6 @@
135135
* Configuration.
136136
*/
137137

138-
// To do: next major: don’t return given `Nodes` from `enter`.
139138
// To do: next major: remove setter/getter.
140139

141140
import {ok as assert} from 'devlop'
@@ -517,15 +516,16 @@ function compiler(options) {
517516

518517
// Create a new list item.
519518
if (event[1].type === types.listItemPrefix) {
520-
listItem = {
519+
/** @type {Token} */
520+
const item = {
521521
type: 'listItem',
522522
_spread: false,
523523
start: Object.assign({}, event[1].start),
524524
// @ts-expect-error: we’ll add `end` in a second.
525525
end: undefined
526526
}
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]])
529529
index++
530530
length++
531531
firstBlankLineIndex = undefined
@@ -601,30 +601,31 @@ function compiler(options) {
601601
}
602602

603603
/**
604-
* @template {Nodes} Kind
605-
* Node type.
606604
* @this {CompileContext}
607605
* Context.
608-
* @param {Kind} node
606+
* @param {Nodes} node
609607
* Node to enter.
610608
* @param {Token} token
611609
* Corresponding token.
612610
* @param {OnEnterError | undefined} [errorHandler]
613611
* 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.
616614
*/
617615
function enter(node, token, errorHandler) {
618616
const parent = this.stack[this.stack.length - 1]
619617
assert(parent, 'expected `parent`')
620618
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)
623622
this.stack.push(node)
624623
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+
}
628629
}
629630

630631
/**
@@ -656,8 +657,8 @@ function compiler(options) {
656657
* Corresponding token.
657658
* @param {OnExitError | undefined} [onExitError]
658659
* Handle the case where another token is open.
659-
* @returns {Nodes}
660-
* The closed node.
660+
* @returns {undefined}
661+
* Nothing.
661662
*/
662663
function exit(token, onExitError) {
663664
const node = this.stack.pop()
@@ -684,7 +685,6 @@ function compiler(options) {
684685
assert(node.type !== 'fragment', 'unexpected fragment `exit`ed')
685686
assert(node.position, 'expected `position` to be defined')
686687
node.position.end = point(token.end)
687-
return node
688688
}
689689

690690
/**
@@ -892,16 +892,20 @@ function compiler(options) {
892892
const node = this.stack[this.stack.length - 1]
893893
assert(node, 'expected node on stack')
894894
assert('children' in node, 'expected parent on stack')
895+
/** @type {Array<Nodes>} */
896+
const siblings = node.children
895897

896-
let tail = node.children[node.children.length - 1]
898+
let tail = siblings[siblings.length - 1]
897899

898900
if (!tail || tail.type !== 'text') {
899901
// Add a new text node.
900902
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)
905909
}
906910

907911
this.stack.push(tail)
@@ -1301,8 +1305,12 @@ function compiler(options) {
13011305

13021306
/** @returns {Heading} */
13031307
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+
}
13061314
}
13071315

13081316
/** @returns {Break} */

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ mdast compiler context (TypeScript type).
177177
— capture some of the output data
178178
* `resume` (`() => string`)
179179
— stop capturing and access the output data
180-
* `enter` (`(node: Node, token: Token, onError?: OnEnterError) => Node`)
180+
* `enter` (`(node: Node, token: Token, onError?: OnEnterError) => undefined`)
181181
— enter a token
182-
* `exit` (`(token: Token, onError?: OnExitError) => Node`)
182+
* `exit` (`(token: Token, onError?: OnExitError) => undefined`)
183183
— exit a token
184184
* `sliceSerialize` (`(token: Token, expandTabs?: boolean) => string`)
185185
— get the string value of a token

0 commit comments

Comments
 (0)