@@ -289,61 +289,99 @@ function transform(state, node) {
289
289
}
290
290
291
291
case 'mdxJsxFlowElement' : {
292
- // Map the attributes array into name/value pairs
292
+ /**
293
+ * A typedef for the MDX node shape.
294
+ * (Adjust property types as needed for your use case.)
295
+ *
296
+ * @typedef {Object } MdxJsxFlowElement
297
+ * @property {string } name
298
+ * @property {Record<string, unknown> } [properties]
299
+ * @property {Array<{
300
+ * type: 'mdxJsxAttribute',
301
+ * name: string,
302
+ * value: unknown
303
+ * }>} [attributes]
304
+ */
305
+
293
306
/** @type {Record<string, unknown> } */
294
307
const attributesMap = { }
295
308
296
- if ( Array . isArray ( unsafe . attributes ) ) {
297
- unsafe . attributes . forEach ( ( attr ) => {
309
+ /**
310
+ * Cast `unsafe` to our `MdxJsxFlowElement`.
311
+ * If `unsafe` truly may be something else, you may want extra runtime checks.
312
+ *
313
+ * @type {MdxJsxFlowElement }
314
+ */
315
+ const typedUnsafe = /** @type {MdxJsxFlowElement } */ ( unsafe )
316
+
317
+ if ( Array . isArray ( typedUnsafe . attributes ) ) {
318
+ for ( const attribute of typedUnsafe . attributes ) {
298
319
if (
299
- attr &&
300
- typeof attr === 'object' &&
301
- 'name' in attr &&
302
- 'value' in attr
320
+ attribute &&
321
+ typeof attribute === 'object' &&
322
+ 'name' in attribute &&
323
+ 'value' in attribute
303
324
) {
304
- attributesMap [ attr . name ] = attr . value
325
+ attributesMap [ attribute . name ] = attribute . value
305
326
}
306
- } )
327
+ }
307
328
}
308
329
309
- const mdxProps = {
310
- ...unsafe ,
311
- tagName : unsafe . name ,
330
+ /**
331
+ * Here we define `mdxProps` to include everything from `typedUnsafe`
332
+ * plus a new `tagName` field.
333
+ *
334
+ * @typedef {MdxJsxFlowElement & {
335
+ * tagName: string,
336
+ * }} MdxProps
337
+ */
338
+
339
+ /** @type {MdxProps } */
340
+ const mdxProperties = {
341
+ ...typedUnsafe ,
342
+ tagName : typedUnsafe . name ,
312
343
properties : {
313
- ...unsafe . properties ,
314
- ...attributesMap // Spread the mapped attributes directly into properties
344
+ ...typedUnsafe . properties ,
345
+ ...attributesMap
315
346
}
316
347
}
317
348
318
- return element ( state , mdxProps )
349
+ return element ( state , mdxProperties )
319
350
}
320
351
321
352
case 'mdxJsxTextElement' : {
322
353
/** @type {Record<string, unknown> } */
323
354
const attributesMap = { }
324
- if ( Array . isArray ( unsafe . attributes ) ) {
325
- unsafe . attributes . forEach ( ( attr ) => {
355
+
356
+ /** @type {{ attributes?: Array<{ type: 'mdxJsxAttribute', name: string, value: unknown }> } } */
357
+ const typedUnsafe = unsafe
358
+
359
+ if ( Array . isArray ( typedUnsafe . attributes ) ) {
360
+ for ( const attribute of typedUnsafe . attributes ) {
326
361
if (
327
- attr &&
328
- typeof attr === 'object' &&
329
- 'name' in attr &&
330
- 'value' in attr
362
+ attribute &&
363
+ typeof attribute === 'object' &&
364
+ 'name' in attribute &&
365
+ 'value' in attribute
331
366
) {
332
- attributesMap [ attr . name ] = attr . value
367
+ attributesMap [ attribute . name ] = attribute . value
333
368
}
334
- } )
369
+ }
335
370
}
336
371
337
- const mdxProps = {
338
- ...unsafe ,
339
- tagName : unsafe . name ,
372
+ const mdxProperties = {
373
+ .../** @type {{ name: string, properties?: Record<string, unknown> } } */ (
374
+ unsafe
375
+ ) ,
376
+ tagName : /** @type {{ name: string } } */ ( unsafe ) . name ,
340
377
properties : {
341
- ...unsafe . properties ,
378
+ .../** @type {{ properties?: Record<string, unknown> } } */ ( unsafe )
379
+ . properties ,
342
380
...attributesMap
343
381
}
344
382
}
345
383
346
- return element ( state , mdxProps )
384
+ return element ( state , mdxProperties )
347
385
}
348
386
349
387
default :
0 commit comments