Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor internals to clarify some code #2253

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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