11/**
2- * @typedef {import('mdast').BlockContent } BlockContent
3- * @typedef {import('mdast').DefinitionContent } DefinitionContent
2+ * @typedef {import('mdast').Nodes } Nodes
43 * @typedef {import('mdast').Paragraph } Paragraph
54 *
65 * @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
1312 * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
1413 * @typedef {import('mdast-util-to-markdown').State } State
1514 *
15+ * @typedef {import('../index.js').Directive } Directive
1616 * @typedef {import('../index.js').LeafDirective } LeafDirective
1717 * @typedef {import('../index.js').TextDirective } TextDirective
18- * @typedef {import('../index.js').Directive } Directive
1918 */
2019
20+ import { ok as assert } from 'devlop'
2121import { parseEntities } from 'parse-entities'
2222import { stringifyEntitiesLight } from 'stringify-entities'
2323import { visitParents } from 'unist-util-visit-parents'
@@ -144,7 +144,12 @@ function enter(type, token) {
144144 * @param {Token } token
145145 */
146146function exitName ( token ) {
147- const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
147+ const node = this . stack [ this . stack . length - 1 ]
148+ assert (
149+ node . type === 'containerDirective' ||
150+ node . type === 'leafDirective' ||
151+ node . type === 'textDirective'
152+ )
148153 node . name = this . sliceSerialize ( token )
149154}
150155
@@ -181,9 +186,8 @@ function enterAttributes() {
181186 * @type {FromMarkdownHandle }
182187 */
183188function exitAttributeIdValue ( token ) {
184- const list = /** @type {Array<[string, string]> } */ (
185- this . data . directiveAttributes
186- )
189+ const list = this . data . directiveAttributes
190+ assert ( list , 'expected `directiveAttributes`' )
187191 list . push ( [
188192 'id' ,
189193 parseEntities ( this . sliceSerialize ( token ) , {
@@ -197,9 +201,8 @@ function exitAttributeIdValue(token) {
197201 * @type {FromMarkdownHandle }
198202 */
199203function exitAttributeClassValue ( token ) {
200- const list = /** @type {Array<[string, string]> } */ (
201- this . data . directiveAttributes
202- )
204+ const list = this . data . directiveAttributes
205+ assert ( list , 'expected `directiveAttributes`' )
203206 list . push ( [
204207 'class' ,
205208 parseEntities ( this . sliceSerialize ( token ) , {
@@ -213,9 +216,8 @@ function exitAttributeClassValue(token) {
213216 * @type {FromMarkdownHandle }
214217 */
215218function exitAttributeValue ( token ) {
216- const list = /** @type {Array<[string, string]> } */ (
217- this . data . directiveAttributes
218- )
219+ const list = this . data . directiveAttributes
220+ assert ( list , 'expected `directiveAttributes`' )
219221 list [ list . length - 1 ] [ 1 ] = parseEntities ( this . sliceSerialize ( token ) , {
220222 attribute : true
221223 } )
@@ -226,9 +228,8 @@ function exitAttributeValue(token) {
226228 * @type {FromMarkdownHandle }
227229 */
228230function exitAttributeName ( token ) {
229- const list = /** @type {Array<[string, string]> } */ (
230- this . data . directiveAttributes
231- )
231+ const list = this . data . directiveAttributes
232+ assert ( list , 'expected `directiveAttributes`' )
232233
233234 // Attribute names in CommonMark are significantly limited, so character
234235 // references can’t exist.
@@ -240,9 +241,8 @@ function exitAttributeName(token) {
240241 * @type {FromMarkdownHandle }
241242 */
242243function exitAttributes ( ) {
243- const list = /** @type {Array<[string, string]> } */ (
244- this . data . directiveAttributes
245- )
244+ const list = this . data . directiveAttributes
245+ assert ( list , 'expected `directiveAttributes`' )
246246 /** @type {Record<string, string> } */
247247 const cleaned = { }
248248 let index = - 1
@@ -259,7 +259,12 @@ function exitAttributes() {
259259
260260 this . data . directiveAttributes = undefined
261261 this . resume ( ) // Drop EOLs
262- const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
262+ const node = this . stack [ this . stack . length - 1 ]
263+ assert (
264+ node . type === 'containerDirective' ||
265+ node . type === 'leafDirective' ||
266+ node . type === 'textDirective'
267+ )
263268 node . attributes = cleaned
264269}
265270
@@ -275,12 +280,12 @@ function exit(token) {
275280 * @type {ToMarkdownHandle }
276281 * @param {Directive } node
277282 */
278- function handleDirective ( node , _ , state , safeOptions ) {
279- const tracker = state . createTracker ( safeOptions )
283+ function handleDirective ( node , _ , state , info ) {
284+ const tracker = state . createTracker ( info )
280285 const sequence = fence ( node )
281286 const exit = state . enter ( node . type )
282287 let value = tracker . move ( sequence + ( node . name || '' ) )
283- /** @type {Paragraph | LeafDirective | TextDirective | undefined } */
288+ /** @type {LeafDirective | Paragraph | TextDirective | undefined } */
284289 let label
285290
286291 if ( node . type === 'containerDirective' ) {
@@ -424,7 +429,7 @@ function attributes(node, state) {
424429}
425430
426431/**
427- * @param {BlockContent | DefinitionContent } node
432+ * @param {Nodes } node
428433 * @returns {node is Paragraph & {data: {directiveLabel: true}} }
429434 */
430435function inlineDirectiveLabel ( node ) {
0 commit comments