diff --git a/packages/mdx/lib/plugin/recma-document.js b/packages/mdx/lib/plugin/recma-document.js index 75a91ce06..65ba326d6 100644 --- a/packages/mdx/lib/plugin/recma-document.js +++ b/packages/mdx/lib/plugin/recma-document.js @@ -73,8 +73,6 @@ export function recmaDocument(options) { const exportedIdentifiers = [] /** @type {Array} */ const replacement = [] - /** @type {Array} */ - const pragmas = [] let exportAllCount = 0 /** @type {ExportDefaultDeclaration | ExportSpecifier | undefined} */ let layout @@ -83,31 +81,20 @@ export function recmaDocument(options) { /** @type {Node} */ let child - if (jsxRuntime) { - pragmas.push('@jsxRuntime ' + jsxRuntime) - } - - if (jsxRuntime === 'automatic' && jsxImportSource) { - pragmas.push('@jsxImportSource ' + jsxImportSource) + if (jsxRuntime === 'classic' && pragmaFrag) { + injectPragma(tree, '@jsxFrag', pragmaFrag) } if (jsxRuntime === 'classic' && pragma) { - pragmas.push('@jsx ' + pragma) + injectPragma(tree, '@jsx', pragma) } - if (jsxRuntime === 'classic' && pragmaFrag) { - pragmas.push('@jsxFrag ' + pragmaFrag) + if (jsxRuntime === 'automatic' && jsxImportSource) { + injectPragma(tree, '@jsxImportSource', jsxImportSource) } - /* c8 ignore next -- comments can be missing in the types, we always have it. */ - if (!tree.comments) tree.comments = [] - - for (let index = pragmas.length; index--; index >= 0) { - tree.comments.unshift({ - type: 'Block', - value: pragmas[index], - data: {_mdxIsPragmaComment: true} - }) + if (jsxRuntime) { + injectPragma(tree, '@jsxRuntime', jsxRuntime) } if (jsxRuntime === 'classic' && pragmaImportSource) { @@ -706,6 +693,20 @@ export function recmaDocument(options) { } } +/** + * @param {Program} tree + * @param {string} name + * @param {string} value + * @returns {undefined} + */ +function injectPragma(tree, name, value) { + tree.comments?.unshift({ + type: 'Block', + value: name + ' ' + value, + data: {_mdxIsPragmaComment: true} + }) +} + /** * @param {Expression} importMetaUrl * @returns {FunctionDeclaration}