Skip to content

Commit 3e4793b

Browse files
committed
fixed typed code coverage
1 parent c31c83f commit 3e4793b

File tree

1 file changed

+66
-28
lines changed

1 file changed

+66
-28
lines changed

lib/index.js

+66-28
Original file line numberDiff line numberDiff line change
@@ -289,61 +289,99 @@ function transform(state, node) {
289289
}
290290

291291
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+
293306
/** @type {Record<string, unknown>} */
294307
const attributesMap = {}
295308

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) {
298319
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
303324
) {
304-
attributesMap[attr.name] = attr.value
325+
attributesMap[attribute.name] = attribute.value
305326
}
306-
})
327+
}
307328
}
308329

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,
312343
properties: {
313-
...unsafe.properties,
314-
...attributesMap // Spread the mapped attributes directly into properties
344+
...typedUnsafe.properties,
345+
...attributesMap
315346
}
316347
}
317348

318-
return element(state, mdxProps)
349+
return element(state, mdxProperties)
319350
}
320351

321352
case 'mdxJsxTextElement': {
322353
/** @type {Record<string, unknown>} */
323354
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) {
326361
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
331366
) {
332-
attributesMap[attr.name] = attr.value
367+
attributesMap[attribute.name] = attribute.value
333368
}
334-
})
369+
}
335370
}
336371

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,
340377
properties: {
341-
...unsafe.properties,
378+
.../** @type {{ properties?: Record<string, unknown> }} */ (unsafe)
379+
.properties,
342380
...attributesMap
343381
}
344382
}
345383

346-
return element(state, mdxProps)
384+
return element(state, mdxProperties)
347385
}
348386

349387
default:

0 commit comments

Comments
 (0)