Skip to content

Commit

Permalink
Unshift pragma comments to the estree directly
Browse files Browse the repository at this point in the history
This removes the need to keep an intermediate pragmas array which is
only used to map over directly after defining it.
  • Loading branch information
remcohaszing committed Feb 10, 2024
1 parent dbc5164 commit ddfdf61
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions packages/mdx/lib/plugin/recma-document.js
Expand Up @@ -73,8 +73,6 @@ export function recmaDocument(options) {
const exportedIdentifiers = []
/** @type {Array<Directive | ModuleDeclaration | Statement>} */
const replacement = []
/** @type {Array<string>} */
const pragmas = []
let exportAllCount = 0
/** @type {ExportDefaultDeclaration | ExportSpecifier | undefined} */
let layout
Expand All @@ -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) {
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit ddfdf61

Please sign in to comment.