Skip to content

Commit ea7fd0b

Browse files
authored
Refactor internals to clarify some code
Closes GH-2253.
1 parent 764d3a2 commit ea7fd0b

4 files changed

Lines changed: 74 additions & 52 deletions

File tree

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mdx/lib/plugin/recma-document.js

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* @typedef {import('estree-jsx').Expression} Expression
88
* @typedef {import('estree-jsx').FunctionDeclaration} FunctionDeclaration
99
* @typedef {import('estree-jsx').ImportDeclaration} ImportDeclaration
10+
* @typedef {import('estree-jsx').ImportDefaultSpecifier} ImportDefaultSpecifier
11+
* @typedef {import('estree-jsx').ImportExpression} ImportExpression
12+
* @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier
13+
* @typedef {import('estree-jsx').Literal} Literal
1014
* @typedef {import('estree-jsx').JSXElement} JSXElement
1115
* @typedef {import('estree-jsx').ModuleDeclaration} ModuleDeclaration
1216
* @typedef {import('estree-jsx').Node} Node
@@ -186,25 +190,38 @@ export function recmaDocument(options) {
186190
layout = specifier
187191

188192
// Make it just an import: `import MDXLayout from '…'`.
189-
handleEsm(
190-
create(specifier, {
191-
type: 'ImportDeclaration',
192-
specifiers: [
193-
// Default as default / something else as default.
194-
specifier.local.name === 'default'
195-
? {
196-
type: 'ImportDefaultSpecifier',
197-
local: {type: 'Identifier', name: 'MDXLayout'}
198-
}
199-
: create(specifier.local, {
200-
type: 'ImportSpecifier',
201-
imported: specifier.local,
202-
local: {type: 'Identifier', name: 'MDXLayout'}
203-
})
204-
],
205-
source: create(source, {type: 'Literal', value: source.value})
193+
/** @type {Array<ImportDefaultSpecifier | ImportSpecifier>} */
194+
const specifiers = []
195+
196+
// Default as default / something else as default.
197+
if (specifier.local.name === 'default') {
198+
specifiers.push({
199+
type: 'ImportDefaultSpecifier',
200+
local: {type: 'Identifier', name: 'MDXLayout'}
206201
})
207-
)
202+
} else {
203+
/** @type {ImportSpecifier} */
204+
const importSpecifier = {
205+
type: 'ImportSpecifier',
206+
imported: specifier.local,
207+
local: {type: 'Identifier', name: 'MDXLayout'}
208+
}
209+
create(specifier.local, importSpecifier)
210+
specifiers.push(importSpecifier)
211+
}
212+
213+
/** @type {Literal} */
214+
const from = {type: 'Literal', value: source.value}
215+
create(source, from)
216+
217+
/** @type {ImportDeclaration} */
218+
const declaration = {
219+
type: 'ImportDeclaration',
220+
specifiers,
221+
source: from
222+
}
223+
create(specifier, declaration)
224+
handleEsm(declaration)
208225

209226
return false
210227
}
@@ -376,7 +393,10 @@ export function recmaDocument(options) {
376393
// with import maps (<https://github.com/WICG/import-maps>).
377394
}
378395

379-
node.source = create(node.source, {type: 'Literal', value})
396+
/** @type {Literal} */
397+
const literal = {type: 'Literal', value}
398+
create(node.source, literal)
399+
node.source = literal
380400
}
381401

382402
/** @type {ModuleDeclaration | Statement | undefined} */
@@ -416,13 +436,10 @@ export function recmaDocument(options) {
416436
// export * from 'a'
417437
// //=> const _exportAll0 = await import('a')
418438
// ```
419-
init = {
420-
type: 'AwaitExpression',
421-
argument: create(node, {
422-
type: 'ImportExpression',
423-
source: node.source
424-
})
425-
}
439+
/** @type {ImportExpression} */
440+
const argument = {type: 'ImportExpression', source: node.source}
441+
create(node, argument)
442+
init = {type: 'AwaitExpression', argument}
426443

427444
if (
428445
(node.type === 'ImportDeclaration' ||

packages/mdx/lib/util/estree-util-create.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
*/
44

55
/**
6-
* @template {Node} N
7-
* @param {Node} template
8-
* @param {N} node
9-
* @returns {N}
6+
* @param {Node} from
7+
* Node to take from.
8+
* @param {Node} to
9+
* Node to add to.
10+
* @returns {void}
11+
* Nothing.
1012
*/
11-
export function create(template, node) {
12-
/** @type {Array<keyof template>} */
13+
export function create(from, to) {
14+
/** @type {Array<keyof Node>} */
1315
// @ts-expect-error: `start`, `end`, `comments` are custom Acorn fields.
1416
const fields = ['start', 'end', 'loc', 'range', 'comments']
1517
let index = -1
1618

1719
while (++index < fields.length) {
1820
const field = fields[index]
1921

20-
if (field in template) {
22+
if (field in from) {
2123
// @ts-expect-error: assume they’re settable.
22-
node[field] = template[field]
24+
to[field] = from[field]
2325
}
2426
}
25-
26-
return node
2727
}

packages/mdx/lib/util/estree-util-specifiers-to-declarations.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/**
2+
* @typedef {import('estree-jsx').AssignmentProperty} AssignmentProperty
23
* @typedef {import('estree-jsx').ExportSpecifier} ExportSpecifier
34
* @typedef {import('estree-jsx').Expression} Expression
45
* @typedef {import('estree-jsx').Identifier} Identifier
5-
* @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier
66
* @typedef {import('estree-jsx').ImportDefaultSpecifier} ImportDefaultSpecifier
77
* @typedef {import('estree-jsx').ImportNamespaceSpecifier} ImportNamespaceSpecifier
8+
* @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier
89
* @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator
910
*/
1011

@@ -36,13 +37,14 @@ export function specifiersToDeclarations(specifiers, init) {
3637
}
3738

3839
if (importNamespaceSpecifier) {
39-
declarations.push(
40-
create(importNamespaceSpecifier, {
41-
type: 'VariableDeclarator',
42-
id: importNamespaceSpecifier.local,
43-
init
44-
})
45-
)
40+
/** @type {VariableDeclarator} */
41+
const declarator = {
42+
type: 'VariableDeclarator',
43+
id: importNamespaceSpecifier.local,
44+
init
45+
}
46+
create(importNamespaceSpecifier, declarator)
47+
declarations.push(declarator)
4648
}
4749

4850
declarations.push({
@@ -65,15 +67,18 @@ export function specifiersToDeclarations(specifiers, init) {
6567
key = specifier.local
6668
}
6769

68-
return create(specifier, {
70+
/** @type {AssignmentProperty} */
71+
const property = {
6972
type: 'Property',
7073
kind: 'init',
7174
shorthand: key.name === value.name,
7275
method: false,
7376
computed: false,
7477
key,
7578
value
76-
})
79+
}
80+
create(specifier, property)
81+
return property
7782
})
7883
},
7984
init: importNamespaceSpecifier

0 commit comments

Comments
 (0)