diff --git a/package-lock.json b/package-lock.json index 19f0b29b6..c9190d11e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6873,9 +6873,9 @@ } }, "node_modules/hast-util-to-estree": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/hast-util-to-estree/-/hast-util-to-estree-2.3.0.tgz", - "integrity": "sha512-FCFA4GhTQqRO9n8dDDj9IlSWiVxDEY7jEOmTWocMpF5IiQ2BADVYf72AWB1hsZmJJPDsf5VTvYZnXHS/52HA4w==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/hast-util-to-estree/-/hast-util-to-estree-2.3.1.tgz", + "integrity": "sha512-32ZCqq3wz1AAJVhLKtAyIRJ/Fcpn7ZPDDJ10ICFNP6xWWSymdPJ0kEn9dRbrgUMLYQmV+Uc6+SGSGqXCLZgnLw==", "dependencies": { "@types/estree": "^1.0.0", "@types/estree-jsx": "^1.0.0", @@ -9664,9 +9664,9 @@ } }, "node_modules/meow/node_modules/type-fest": { - "version": "3.5.6", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-3.5.6.tgz", - "integrity": "sha512-6bd2bflx8ed7c99tc6zSTIzHr1/QG29bQoK4Qh8MYGnlPbODUzGxklLShjwc/xWQQFHgIci+y5Arv7Rbb0LjXw==", + "version": "3.5.7", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-3.5.7.tgz", + "integrity": "sha512-6J4bYzb4sdkcLBty4XW7F18VPI66M4boXNE+CY40532oq2OJe6AVMB5NmjOp6skt/jw5mRjz/hLRpuglz0U+FA==", "dev": true, "engines": { "node": ">=14.16" diff --git a/packages/mdx/lib/plugin/recma-document.js b/packages/mdx/lib/plugin/recma-document.js index 156b57153..28a3c2967 100644 --- a/packages/mdx/lib/plugin/recma-document.js +++ b/packages/mdx/lib/plugin/recma-document.js @@ -7,6 +7,10 @@ * @typedef {import('estree-jsx').Expression} Expression * @typedef {import('estree-jsx').FunctionDeclaration} FunctionDeclaration * @typedef {import('estree-jsx').ImportDeclaration} ImportDeclaration + * @typedef {import('estree-jsx').ImportDefaultSpecifier} ImportDefaultSpecifier + * @typedef {import('estree-jsx').ImportExpression} ImportExpression + * @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier + * @typedef {import('estree-jsx').Literal} Literal * @typedef {import('estree-jsx').JSXElement} JSXElement * @typedef {import('estree-jsx').ModuleDeclaration} ModuleDeclaration * @typedef {import('estree-jsx').Node} Node @@ -186,25 +190,38 @@ export function recmaDocument(options) { layout = specifier // Make it just an import: `import MDXLayout from '…'`. - handleEsm( - create(specifier, { - type: 'ImportDeclaration', - specifiers: [ - // Default as default / something else as default. - specifier.local.name === 'default' - ? { - type: 'ImportDefaultSpecifier', - local: {type: 'Identifier', name: 'MDXLayout'} - } - : create(specifier.local, { - type: 'ImportSpecifier', - imported: specifier.local, - local: {type: 'Identifier', name: 'MDXLayout'} - }) - ], - source: create(source, {type: 'Literal', value: source.value}) + /** @type {Array} */ + const specifiers = [] + + // Default as default / something else as default. + if (specifier.local.name === 'default') { + specifiers.push({ + type: 'ImportDefaultSpecifier', + local: {type: 'Identifier', name: 'MDXLayout'} }) - ) + } else { + /** @type {ImportSpecifier} */ + const importSpecifier = { + type: 'ImportSpecifier', + imported: specifier.local, + local: {type: 'Identifier', name: 'MDXLayout'} + } + create(specifier.local, importSpecifier) + specifiers.push(importSpecifier) + } + + /** @type {Literal} */ + const from = {type: 'Literal', value: source.value} + create(source, from) + + /** @type {ImportDeclaration} */ + const declaration = { + type: 'ImportDeclaration', + specifiers, + source: from + } + create(specifier, declaration) + handleEsm(declaration) return false } @@ -376,7 +393,10 @@ export function recmaDocument(options) { // with import maps (). } - node.source = create(node.source, {type: 'Literal', value}) + /** @type {Literal} */ + const literal = {type: 'Literal', value} + create(node.source, literal) + node.source = literal } /** @type {ModuleDeclaration | Statement | undefined} */ @@ -416,13 +436,10 @@ export function recmaDocument(options) { // export * from 'a' // //=> const _exportAll0 = await import('a') // ``` - init = { - type: 'AwaitExpression', - argument: create(node, { - type: 'ImportExpression', - source: node.source - }) - } + /** @type {ImportExpression} */ + const argument = {type: 'ImportExpression', source: node.source} + create(node, argument) + init = {type: 'AwaitExpression', argument} if ( (node.type === 'ImportDeclaration' || diff --git a/packages/mdx/lib/util/estree-util-create.js b/packages/mdx/lib/util/estree-util-create.js index 7a9f9c7a4..ce559de19 100644 --- a/packages/mdx/lib/util/estree-util-create.js +++ b/packages/mdx/lib/util/estree-util-create.js @@ -3,13 +3,15 @@ */ /** - * @template {Node} N - * @param {Node} template - * @param {N} node - * @returns {N} + * @param {Node} from + * Node to take from. + * @param {Node} to + * Node to add to. + * @returns {void} + * Nothing. */ -export function create(template, node) { - /** @type {Array} */ +export function create(from, to) { + /** @type {Array} */ // @ts-expect-error: `start`, `end`, `comments` are custom Acorn fields. const fields = ['start', 'end', 'loc', 'range', 'comments'] let index = -1 @@ -17,11 +19,9 @@ export function create(template, node) { while (++index < fields.length) { const field = fields[index] - if (field in template) { + if (field in from) { // @ts-expect-error: assume they’re settable. - node[field] = template[field] + to[field] = from[field] } } - - return node } diff --git a/packages/mdx/lib/util/estree-util-specifiers-to-declarations.js b/packages/mdx/lib/util/estree-util-specifiers-to-declarations.js index 25fac38f1..48195f316 100644 --- a/packages/mdx/lib/util/estree-util-specifiers-to-declarations.js +++ b/packages/mdx/lib/util/estree-util-specifiers-to-declarations.js @@ -1,10 +1,11 @@ /** + * @typedef {import('estree-jsx').AssignmentProperty} AssignmentProperty * @typedef {import('estree-jsx').ExportSpecifier} ExportSpecifier * @typedef {import('estree-jsx').Expression} Expression * @typedef {import('estree-jsx').Identifier} Identifier - * @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier * @typedef {import('estree-jsx').ImportDefaultSpecifier} ImportDefaultSpecifier * @typedef {import('estree-jsx').ImportNamespaceSpecifier} ImportNamespaceSpecifier + * @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier * @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator */ @@ -36,13 +37,14 @@ export function specifiersToDeclarations(specifiers, init) { } if (importNamespaceSpecifier) { - declarations.push( - create(importNamespaceSpecifier, { - type: 'VariableDeclarator', - id: importNamespaceSpecifier.local, - init - }) - ) + /** @type {VariableDeclarator} */ + const declarator = { + type: 'VariableDeclarator', + id: importNamespaceSpecifier.local, + init + } + create(importNamespaceSpecifier, declarator) + declarations.push(declarator) } declarations.push({ @@ -65,7 +67,8 @@ export function specifiersToDeclarations(specifiers, init) { key = specifier.local } - return create(specifier, { + /** @type {AssignmentProperty} */ + const property = { type: 'Property', kind: 'init', shorthand: key.name === value.name, @@ -73,7 +76,9 @@ export function specifiersToDeclarations(specifiers, init) { computed: false, key, value - }) + } + create(specifier, property) + return property }) }, init: importNamespaceSpecifier