Skip to content

Commit

Permalink
Refactor internals to clarify some code
Browse files Browse the repository at this point in the history
Closes GH-2253.
  • Loading branch information
wooorm committed Feb 9, 2023
1 parent 764d3a2 commit ea7fd0b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 52 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 43 additions & 26 deletions packages/mdx/lib/plugin/recma-document.js
Expand Up @@ -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
Expand Down Expand Up @@ -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<ImportDefaultSpecifier | ImportSpecifier>} */
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
}
Expand Down Expand Up @@ -376,7 +393,10 @@ export function recmaDocument(options) {
// with import maps (<https://github.com/WICG/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} */
Expand Down Expand Up @@ -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' ||
Expand Down
20 changes: 10 additions & 10 deletions packages/mdx/lib/util/estree-util-create.js
Expand Up @@ -3,25 +3,25 @@
*/

/**
* @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<keyof template>} */
export function create(from, to) {
/** @type {Array<keyof Node>} */
// @ts-expect-error: `start`, `end`, `comments` are custom Acorn fields.
const fields = ['start', 'end', 'loc', 'range', 'comments']
let index = -1

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
}
25 changes: 15 additions & 10 deletions 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
*/

Expand Down Expand Up @@ -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({
Expand All @@ -65,15 +67,18 @@ export function specifiersToDeclarations(specifiers, init) {
key = specifier.local
}

return create(specifier, {
/** @type {AssignmentProperty} */
const property = {
type: 'Property',
kind: 'init',
shorthand: key.name === value.name,
method: false,
computed: false,
key,
value
})
}
create(specifier, property)
return property
})
},
init: importNamespaceSpecifier
Expand Down

1 comment on commit ea7fd0b

@vercel
Copy link

@vercel vercel bot commented on ea7fd0b Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mdx – ./

mdx-mdx.vercel.app
mdx-git-main-mdx.vercel.app
mdxjs.com
v2.mdxjs.com

Please sign in to comment.