This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,36 @@
/**
* Wrap the estree in `MDXContent`.
*
* @param {Readonly<ProcessorOptions>} options
* Configuration.
* @returns
* Transform.
*/
export function recmaDocument(options: Readonly<ProcessorOptions>): (tree: Program, file: VFile) => undefined;
export type CallExpression = import('estree-jsx').CallExpression;
export type Directive = import('estree-jsx').Directive;
export type ExportAllDeclaration = import('estree-jsx').ExportAllDeclaration;
export type ExportDefaultDeclaration = import('estree-jsx').ExportDefaultDeclaration;
export type ExportNamedDeclaration = import('estree-jsx').ExportNamedDeclaration;
export type ExportSpecifier = import('estree-jsx').ExportSpecifier;
export type Expression = import('estree-jsx').Expression;
export type FunctionDeclaration = import('estree-jsx').FunctionDeclaration;
export type Identifier = import('estree-jsx').Identifier;
export type ImportDeclaration = import('estree-jsx').ImportDeclaration;
export type ImportDefaultSpecifier = import('estree-jsx').ImportDefaultSpecifier;
export type ImportExpression = import('estree-jsx').ImportExpression;
export type ImportSpecifier = import('estree-jsx').ImportSpecifier;
export type JSXElement = import('estree-jsx').JSXElement;
export type JSXFragment = import('estree-jsx').JSXFragment;
export type Literal = import('estree-jsx').Literal;
export type ModuleDeclaration = import('estree-jsx').ModuleDeclaration;
export type Node = import('estree-jsx').Node;
export type Program = import('estree-jsx').Program;
export type Property = import('estree-jsx').Property;
export type SimpleLiteral = import('estree-jsx').SimpleLiteral;
export type SpreadElement = import('estree-jsx').SpreadElement;
export type Statement = import('estree-jsx').Statement;
export type VariableDeclarator = import('estree-jsx').VariableDeclarator;
export type VFile = import('vfile').VFile;
export type ProcessorOptions = import('../core.js').ProcessorOptions;
//# sourceMappingURL=recma-document.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"recma-document.d.ts","sourceRoot":"","sources":["recma-document.js"],"names":[],"mappings":"AA0CA;;;;;;;GAOG;AACH,uCALW,SAAS,gBAAgB,CAAC,UAkBxB,OAAO,QAEP,KAAK,KAEH,SAAS,CAknBvB;6BAprBY,OAAO,YAAY,EAAE,cAAc;wBACnC,OAAO,YAAY,EAAE,SAAS;mCAC9B,OAAO,YAAY,EAAE,oBAAoB;uCACzC,OAAO,YAAY,EAAE,wBAAwB;qCAC7C,OAAO,YAAY,EAAE,sBAAsB;8BAC3C,OAAO,YAAY,EAAE,eAAe;yBACpC,OAAO,YAAY,EAAE,UAAU;kCAC/B,OAAO,YAAY,EAAE,mBAAmB;yBACxC,OAAO,YAAY,EAAE,UAAU;gCAC/B,OAAO,YAAY,EAAE,iBAAiB;qCACtC,OAAO,YAAY,EAAE,sBAAsB;+BAC3C,OAAO,YAAY,EAAE,gBAAgB;8BACrC,OAAO,YAAY,EAAE,eAAe;yBACpC,OAAO,YAAY,EAAE,UAAU;0BAC/B,OAAO,YAAY,EAAE,WAAW;sBAChC,OAAO,YAAY,EAAE,OAAO;gCAC5B,OAAO,YAAY,EAAE,iBAAiB;mBACtC,OAAO,YAAY,EAAE,IAAI;sBACzB,OAAO,YAAY,EAAE,OAAO;uBAC5B,OAAO,YAAY,EAAE,QAAQ;4BAC7B,OAAO,YAAY,EAAE,aAAa;4BAClC,OAAO,YAAY,EAAE,aAAa;wBAClC,OAAO,YAAY,EAAE,SAAS;iCAC9B,OAAO,YAAY,EAAE,kBAAkB;oBAEvC,OAAO,OAAO,EAAE,KAAK;+BAErB,OAAO,YAAY,EAAE,gBAAgB"}

880
node_modules/@mdx-js/mdx/lib/plugin/recma-document.js generated vendored Normal file
View File

@@ -0,0 +1,880 @@
/**
* @typedef {import('estree-jsx').CallExpression} CallExpression
* @typedef {import('estree-jsx').Directive} Directive
* @typedef {import('estree-jsx').ExportAllDeclaration} ExportAllDeclaration
* @typedef {import('estree-jsx').ExportDefaultDeclaration} ExportDefaultDeclaration
* @typedef {import('estree-jsx').ExportNamedDeclaration} ExportNamedDeclaration
* @typedef {import('estree-jsx').ExportSpecifier} ExportSpecifier
* @typedef {import('estree-jsx').Expression} Expression
* @typedef {import('estree-jsx').FunctionDeclaration} FunctionDeclaration
* @typedef {import('estree-jsx').Identifier} Identifier
* @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').JSXElement} JSXElement
* @typedef {import('estree-jsx').JSXFragment} JSXFragment
* @typedef {import('estree-jsx').Literal} Literal
* @typedef {import('estree-jsx').ModuleDeclaration} ModuleDeclaration
* @typedef {import('estree-jsx').Node} Node
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('estree-jsx').Property} Property
* @typedef {import('estree-jsx').SimpleLiteral} SimpleLiteral
* @typedef {import('estree-jsx').SpreadElement} SpreadElement
* @typedef {import('estree-jsx').Statement} Statement
* @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator
*
* @typedef {import('vfile').VFile} VFile
*
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*/
import {ok as assert} from 'devlop'
import {walk} from 'estree-walker'
import {analyze} from 'periscopic'
import {positionFromEstree} from 'unist-util-position-from-estree'
import {stringifyPosition} from 'unist-util-stringify-position'
import {create} from '../util/estree-util-create.js'
import {declarationToExpression} from '../util/estree-util-declaration-to-expression.js'
import {isDeclaration} from '../util/estree-util-is-declaration.js'
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
import {toIdOrMemberExpression} from '../util/estree-util-to-id-or-member-expression.js'
/**
* Wrap the estree in `MDXContent`.
*
* @param {Readonly<ProcessorOptions>} options
* Configuration.
* @returns
* Transform.
*/
export function recmaDocument(options) {
const baseUrl = options.baseUrl || undefined
const baseHref = typeof baseUrl === 'object' ? baseUrl.href : baseUrl
const outputFormat = options.outputFormat || 'program'
const pragma =
options.pragma === undefined ? 'React.createElement' : options.pragma
const pragmaFrag =
options.pragmaFrag === undefined ? 'React.Fragment' : options.pragmaFrag
const pragmaImportSource = options.pragmaImportSource || 'react'
const jsxImportSource = options.jsxImportSource || 'react'
const jsxRuntime = options.jsxRuntime || 'automatic'
/**
* @param {Program} tree
* Tree.
* @param {VFile} file
* File.
* @returns {undefined}
* Nothing.
*/
return function (tree, file) {
/** @type {Array<[string, string] | string>} */
const exportedIdentifiers = []
/** @type {Array<Directive | ModuleDeclaration | Statement>} */
const replacement = []
let exportAllCount = 0
/** @type {ExportDefaultDeclaration | ExportSpecifier | undefined} */
let layout
/** @type {boolean | undefined} */
let content
/** @type {Node} */
let child
if (jsxRuntime === 'classic' && pragmaFrag) {
injectPragma(tree, '@jsxFrag', pragmaFrag)
}
if (jsxRuntime === 'classic' && pragma) {
injectPragma(tree, '@jsx', pragma)
}
if (jsxRuntime === 'automatic' && jsxImportSource) {
injectPragma(tree, '@jsxImportSource', jsxImportSource)
}
if (jsxRuntime) {
injectPragma(tree, '@jsxRuntime', jsxRuntime)
}
if (jsxRuntime === 'classic' && pragmaImportSource) {
if (!pragma) {
throw new Error(
'Missing `pragma` in classic runtime with `pragmaImportSource`'
)
}
handleEsm({
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportDefaultSpecifier',
local: {type: 'Identifier', name: pragma.split('.')[0]}
}
],
source: {type: 'Literal', value: pragmaImportSource}
})
}
// Find the `export default`, the JSX expression, and leave the rest
// (import/exports) as they are.
for (child of tree.body) {
// ```tsx
// export default properties => <>{properties.children}</>
// ```
//
// Treat it as an inline layout declaration.
if (child.type === 'ExportDefaultDeclaration') {
if (layout) {
file.fail(
'Unexpected duplicate layout, expected a single layout (previous: ' +
stringifyPosition(positionFromEstree(layout)) +
')',
{
ancestors: [tree, child],
place: positionFromEstree(child),
ruleId: 'duplicate-layout',
source: 'recma-document'
}
)
}
layout = child
replacement.push({
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
id: {type: 'Identifier', name: 'MDXLayout'},
init: isDeclaration(child.declaration)
? declarationToExpression(child.declaration)
: child.declaration
}
]
})
}
// ```tsx
// export {a, b as c} from 'd'
// ```
else if (child.type === 'ExportNamedDeclaration' && child.source) {
// Cast because always simple.
const source = /** @type {SimpleLiteral} */ (child.source)
// Remove `default` or `as default`, but not `default as`, specifier.
child.specifiers = child.specifiers.filter(function (specifier) {
if (specifier.exported.name === 'default') {
if (layout) {
file.fail(
'Unexpected duplicate layout, expected a single layout (previous: ' +
stringifyPosition(positionFromEstree(layout)) +
')',
{
ancestors: [tree, child, specifier],
place: positionFromEstree(child),
ruleId: 'duplicate-layout',
source: 'recma-document'
}
)
}
layout = specifier
// Make it just an import: `import MDXLayout from '…'`.
/** @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
}
return true
})
// If there are other things imported, keep it.
if (child.specifiers.length > 0) {
handleExport(child)
}
}
// ```tsx
// export {a, b as c}
// export * from 'a'
// ```
else if (
child.type === 'ExportNamedDeclaration' ||
child.type === 'ExportAllDeclaration'
) {
handleExport(child)
} else if (child.type === 'ImportDeclaration') {
handleEsm(child)
} else if (
child.type === 'ExpressionStatement' &&
(child.expression.type === 'JSXElement' ||
// @ts-expect-error: `estree-jsx` does not register `JSXFragment` as an expression.
child.expression.type === 'JSXFragment')
) {
content = true
replacement.push(
...createMdxContent(child.expression, outputFormat, Boolean(layout))
)
} else {
// This catch-all branch is because plugins might add other things.
// Normally, we only have import/export/jsx, but just add whatevers
// there.
replacement.push(child)
}
}
// If there was no JSX content at all, add an empty function.
if (!content) {
replacement.push(
...createMdxContent(undefined, outputFormat, Boolean(layout))
)
}
exportedIdentifiers.push(['MDXContent', 'default'])
if (outputFormat === 'function-body') {
replacement.push({
type: 'ReturnStatement',
argument: {
type: 'ObjectExpression',
properties: [
...Array.from({length: exportAllCount}).map(
/**
* @param {undefined} _
* Nothing.
* @param {number} index
* Index.
* @returns {SpreadElement}
* Node.
*/
function (_, index) {
return {
type: 'SpreadElement',
argument: {
type: 'Identifier',
name: '_exportAll' + (index + 1)
}
}
}
),
...exportedIdentifiers.map(function (d) {
/** @type {Property} */
const property = {
type: 'Property',
kind: 'init',
method: false,
computed: false,
shorthand: typeof d === 'string',
key: {
type: 'Identifier',
name: typeof d === 'string' ? d : d[1]
},
value: {
type: 'Identifier',
name: typeof d === 'string' ? d : d[0]
}
}
return property
})
]
}
})
}
tree.body = replacement
let usesImportMetaUrlVariable = false
let usesResolveDynamicHelper = false
if (baseHref || outputFormat === 'function-body') {
walk(tree, {
enter(node) {
if (
(node.type === 'ExportAllDeclaration' ||
node.type === 'ExportNamedDeclaration' ||
node.type === 'ImportDeclaration') &&
node.source
) {
// We never hit this branch when generating function bodies, as
// statements are already compiled away into import expressions.
assert(baseHref, 'unexpected missing `baseHref` in branch')
let value = node.source.value
// The literal source for statements can only be string.
assert(typeof value === 'string', 'expected string source')
// Resolve a specifier.
// This is the same as `_resolveDynamicMdxSpecifier`, which has to
// be injected to work with expressions at runtime, but as we have
// `baseHref` at compile time here and statements are static
// strings, we can do it now.
try {
// To do: use `URL.canParse` next major.
// eslint-disable-next-line no-new
new URL(value)
// Fine: a full URL.
} catch {
if (
value.startsWith('/') ||
value.startsWith('./') ||
value.startsWith('../')
) {
value = new URL(value, baseHref).href
} else {
// Fine: are bare specifier.
}
}
/** @type {SimpleLiteral} */
const replacement = {type: 'Literal', value}
create(node.source, replacement)
node.source = replacement
return
}
if (node.type === 'ImportExpression') {
usesResolveDynamicHelper = true
/** @type {CallExpression} */
const replacement = {
type: 'CallExpression',
callee: {type: 'Identifier', name: '_resolveDynamicMdxSpecifier'},
arguments: [node.source],
optional: false
}
node.source = replacement
return
}
// To do: add support for `import.meta.resolve`.
if (
node.type === 'MemberExpression' &&
'object' in node &&
node.object.type === 'MetaProperty' &&
node.property.type === 'Identifier' &&
node.object.meta.name === 'import' &&
node.object.property.name === 'meta' &&
node.property.name === 'url'
) {
usesImportMetaUrlVariable = true
/** @type {Identifier} */
const replacement = {type: 'Identifier', name: '_importMetaUrl'}
create(node, replacement)
this.replace(replacement)
}
}
})
}
if (usesResolveDynamicHelper) {
if (!baseHref) {
usesImportMetaUrlVariable = true
}
tree.body.push(
resolveDynamicMdxSpecifier(
baseHref
? {type: 'Literal', value: baseHref}
: {type: 'Identifier', name: '_importMetaUrl'}
)
)
}
if (usesImportMetaUrlVariable) {
assert(
outputFormat === 'function-body',
'expected `function-body` when using dynamic url injection'
)
tree.body.unshift(...createImportMetaUrlVariable())
}
/**
* @param {ExportAllDeclaration | ExportNamedDeclaration} node
* Export node.
* @returns {undefined}
* Nothing.
*/
function handleExport(node) {
if (node.type === 'ExportNamedDeclaration') {
// ```tsx
// export function a() {}
// export class A {}
// export var a = 1
// ```
if (node.declaration) {
exportedIdentifiers.push(
...analyze(node.declaration).scope.declarations.keys()
)
}
// ```tsx
// export {a, b as c}
// export {a, b as c} from 'd'
// ```
for (child of node.specifiers) {
exportedIdentifiers.push(child.exported.name)
}
}
handleEsm(node)
}
/**
* @param {ExportAllDeclaration | ExportNamedDeclaration | ImportDeclaration} node
* Export or import node.
* @returns {undefined}
* Nothing.
*/
function handleEsm(node) {
/** @type {ModuleDeclaration | Statement | undefined} */
let replace
/** @type {Expression} */
let init
if (outputFormat === 'function-body') {
if (
// Always have a source:
node.type === 'ImportDeclaration' ||
node.type === 'ExportAllDeclaration' ||
// Source optional:
(node.type === 'ExportNamedDeclaration' && node.source)
) {
// We always have a source, but types say they can be missing.
assert(node.source, 'expected `node.source` to be defined')
// ```
// import 'a'
// //=> await import('a')
// import a from 'b'
// //=> const {default: a} = await import('b')
// export {a, b as c} from 'd'
// //=> const {a, c: b} = await import('d')
// export * from 'a'
// //=> const _exportAll0 = await import('a')
// ```
/** @type {ImportExpression} */
const argument = {type: 'ImportExpression', source: node.source}
create(node, argument)
init = {type: 'AwaitExpression', argument}
if (
(node.type === 'ImportDeclaration' ||
node.type === 'ExportNamedDeclaration') &&
node.specifiers.length === 0
) {
replace = {type: 'ExpressionStatement', expression: init}
} else {
replace = {
type: 'VariableDeclaration',
kind: 'const',
declarations:
node.type === 'ExportAllDeclaration'
? [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: '_exportAll' + ++exportAllCount
},
init
}
]
: specifiersToDeclarations(node.specifiers, init)
}
}
} else if (node.declaration) {
replace = node.declaration
} else {
/** @type {Array<VariableDeclarator>} */
const declarators = node.specifiers
.filter(function (specifier) {
return specifier.local.name !== specifier.exported.name
})
.map(function (specifier) {
return {
type: 'VariableDeclarator',
id: specifier.exported,
init: specifier.local
}
})
if (declarators.length > 0) {
replace = {
type: 'VariableDeclaration',
kind: 'const',
declarations: declarators
}
}
}
} else {
replace = node
}
if (replace) {
replacement.push(replace)
}
}
}
/**
* @param {Readonly<Expression> | undefined} content
* Content.
* @param {'function-body' | 'program'} outputFormat
* Output format.
* @param {boolean | undefined} [hasInternalLayout=false]
* Whether theres an internal layout (default: `false`).
* @returns {Array<ExportDefaultDeclaration | FunctionDeclaration>}
* Functions.
*/
function createMdxContent(content, outputFormat, hasInternalLayout) {
/** @type {JSXElement} */
const element = {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: {type: 'JSXIdentifier', name: 'MDXLayout'},
attributes: [
{
type: 'JSXSpreadAttribute',
argument: {type: 'Identifier', name: 'props'}
}
],
selfClosing: false
},
closingElement: {
type: 'JSXClosingElement',
name: {type: 'JSXIdentifier', name: 'MDXLayout'}
},
children: [
{
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: {type: 'JSXIdentifier', name: '_createMdxContent'},
attributes: [
{
type: 'JSXSpreadAttribute',
argument: {type: 'Identifier', name: 'props'}
}
],
selfClosing: true
},
closingElement: null,
children: []
}
]
}
let result = /** @type {Expression} */ (element)
if (!hasInternalLayout) {
result = {
type: 'ConditionalExpression',
test: {type: 'Identifier', name: 'MDXLayout'},
consequent: result,
alternate: {
type: 'CallExpression',
callee: {type: 'Identifier', name: '_createMdxContent'},
arguments: [{type: 'Identifier', name: 'props'}],
optional: false
}
}
}
let argument =
// Cast because TS otherwise does not think `JSXFragment`s are expressions.
/** @type {Readonly<Expression> | Readonly<JSXFragment>} */ (
content || {type: 'Identifier', name: 'undefined'}
)
// Unwrap a fragment of a single element.
if (
argument.type === 'JSXFragment' &&
argument.children.length === 1 &&
argument.children[0].type === 'JSXElement'
) {
argument = argument.children[0]
}
let awaitExpression = false
walk(argument, {
enter(node) {
if (
node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression'
) {
return this.skip()
}
if (
node.type === 'AwaitExpression' ||
/* c8 ignore next 2 -- can only occur in a function (which then can
* only be async, so skipped it) */
(node.type === 'ForOfStatement' && node.await)
) {
awaitExpression = true
}
}
})
/** @type {FunctionDeclaration} */
const declaration = {
type: 'FunctionDeclaration',
id: {type: 'Identifier', name: 'MDXContent'},
params: [
{
type: 'AssignmentPattern',
left: {type: 'Identifier', name: 'props'},
right: {type: 'ObjectExpression', properties: []}
}
],
body: {
type: 'BlockStatement',
body: [{type: 'ReturnStatement', argument: result}]
}
}
return [
{
type: 'FunctionDeclaration',
async: awaitExpression,
id: {type: 'Identifier', name: '_createMdxContent'},
params: [{type: 'Identifier', name: 'props'}],
body: {
type: 'BlockStatement',
body: [
{
type: 'ReturnStatement',
// Cast because TS doesnt think `JSXFragment` is an expression.
// eslint-disable-next-line object-shorthand
argument: /** @type {Expression} */ (argument)
}
]
}
},
outputFormat === 'program'
? {type: 'ExportDefaultDeclaration', declaration}
: declaration
]
}
}
/**
* @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}
*/
function resolveDynamicMdxSpecifier(importMetaUrl) {
return {
type: 'FunctionDeclaration',
id: {type: 'Identifier', name: '_resolveDynamicMdxSpecifier'},
generator: false,
async: false,
params: [{type: 'Identifier', name: 'd'}],
body: {
type: 'BlockStatement',
body: [
{
type: 'IfStatement',
test: {
type: 'BinaryExpression',
left: {
type: 'UnaryExpression',
operator: 'typeof',
prefix: true,
argument: {type: 'Identifier', name: 'd'}
},
operator: '!==',
right: {type: 'Literal', value: 'string'}
},
consequent: {
type: 'ReturnStatement',
argument: {type: 'Identifier', name: 'd'}
},
alternate: null
},
// To do: use `URL.canParse` when widely supported (see commented
// out code below).
{
type: 'TryStatement',
block: {
type: 'BlockStatement',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'NewExpression',
callee: {type: 'Identifier', name: 'URL'},
arguments: [{type: 'Identifier', name: 'd'}]
}
},
{
type: 'ReturnStatement',
argument: {type: 'Identifier', name: 'd'}
}
]
},
handler: {
type: 'CatchClause',
param: null,
body: {type: 'BlockStatement', body: []}
},
finalizer: null
},
// To do: use `URL.canParse` when widely supported.
// {
// type: 'IfStatement',
// test: {
// type: 'CallExpression',
// callee: toIdOrMemberExpression(['URL', 'canParse']),
// arguments: [{type: 'Identifier', name: 'd'}],
// optional: false
// },
// consequent: {
// type: 'ReturnStatement',
// argument: {type: 'Identifier', name: 'd'}
// },
// alternate: null
// },
{
type: 'IfStatement',
test: {
type: 'LogicalExpression',
left: {
type: 'LogicalExpression',
left: {
type: 'CallExpression',
callee: toIdOrMemberExpression(['d', 'startsWith']),
arguments: [{type: 'Literal', value: '/'}],
optional: false
},
operator: '||',
right: {
type: 'CallExpression',
callee: toIdOrMemberExpression(['d', 'startsWith']),
arguments: [{type: 'Literal', value: './'}],
optional: false
}
},
operator: '||',
right: {
type: 'CallExpression',
callee: toIdOrMemberExpression(['d', 'startsWith']),
arguments: [{type: 'Literal', value: '../'}],
optional: false
}
},
consequent: {
type: 'ReturnStatement',
argument: {
type: 'MemberExpression',
object: {
type: 'NewExpression',
callee: {type: 'Identifier', name: 'URL'},
arguments: [{type: 'Identifier', name: 'd'}, importMetaUrl]
},
property: {type: 'Identifier', name: 'href'},
computed: false,
optional: false
}
},
alternate: null
},
{
type: 'ReturnStatement',
argument: {type: 'Identifier', name: 'd'}
}
]
}
}
}
/**
* @returns {Array<Statement>}
*/
function createImportMetaUrlVariable() {
return [
{
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {type: 'Identifier', name: '_importMetaUrl'},
init: toIdOrMemberExpression(['arguments', 0, 'baseUrl'])
}
],
kind: 'const'
},
{
type: 'IfStatement',
test: {
type: 'UnaryExpression',
operator: '!',
prefix: true,
argument: {type: 'Identifier', name: '_importMetaUrl'}
},
consequent: {
type: 'ThrowStatement',
argument: {
type: 'NewExpression',
callee: {type: 'Identifier', name: 'Error'},
arguments: [
{
type: 'Literal',
value:
'Unexpected missing `options.baseUrl` needed to support `export … from`, `import`, or `import.meta.url` when generating `function-body`'
}
]
}
},
alternate: null
}
]
}

View File

@@ -0,0 +1,28 @@
/**
* A plugin to build JSX into function calls.
* `estree-util-build-jsx` does all the work for us!
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Transform.
*/
export function recmaJsxBuild(options?: Readonly<Options> | null | undefined): (tree: Program, file: VFile) => undefined;
export type Program = import('estree-jsx').Program;
export type BuildJsxOptions = import('estree-util-build-jsx').Options;
export type VFile = import('vfile').VFile;
/**
* Configuration for internal plugin `recma-jsx-build`.
*/
export type ExtraOptions = {
/**
* Whether to keep the import of the automatic runtime or get it from
* `arguments[0]` instead (default: `'program'`).
*/
outputFormat?: 'function-body' | 'program' | null | undefined;
};
/**
* Options.
*/
export type Options = BuildJsxOptions & ExtraOptions;
//# sourceMappingURL=recma-jsx-build.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"recma-jsx-build.d.ts","sourceRoot":"","sources":["recma-jsx-build.js"],"names":[],"mappings":"AAqBA;;;;;;;;GAQG;AACH,wCALW,SAAS,OAAO,CAAC,GAAG,IAAI,GAAG,SAAS,UAUlC,OAAO,QAEP,KAAK,KAEH,SAAS,CAmDvB;sBAzFY,OAAO,YAAY,EAAE,OAAO;8BAC5B,OAAO,uBAAuB,EAAE,OAAO;oBACvC,OAAO,OAAO,EAAE,KAAK;;;;;;;;;mBAMpB,eAAe,GAAG,SAAS,GAAG,IAAI,GAAG,SAAS;;;;;sBAI/C,eAAe,GAAG,YAAY"}

91
node_modules/@mdx-js/mdx/lib/plugin/recma-jsx-build.js generated vendored Normal file
View File

@@ -0,0 +1,91 @@
/**
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('estree-util-build-jsx').Options} BuildJsxOptions
* @typedef {import('vfile').VFile} VFile
*/
/**
* @typedef ExtraOptions
* Configuration for internal plugin `recma-jsx-build`.
* @property {'function-body' | 'program' | null | undefined} [outputFormat='program']
* Whether to keep the import of the automatic runtime or get it from
* `arguments[0]` instead (default: `'program'`).
*
* @typedef {BuildJsxOptions & ExtraOptions} Options
* Options.
*/
import {buildJsx} from 'estree-util-build-jsx'
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
import {toIdOrMemberExpression} from '../util/estree-util-to-id-or-member-expression.js'
/**
* A plugin to build JSX into function calls.
* `estree-util-build-jsx` does all the work for us!
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Transform.
*/
export function recmaJsxBuild(options) {
/* c8 ignore next -- always given in `@mdx-js/mdx` */
const {development, outputFormat} = options || {}
/**
* @param {Program} tree
* Tree.
* @param {VFile} file
* File.
* @returns {undefined}
* Nothing.
*/
return function (tree, file) {
buildJsx(tree, {development, filePath: file.history[0]})
// Remove the pragma comment that we injected ourselves as it is no longer
// needed.
if (tree.comments) {
tree.comments = tree.comments.filter(function (d) {
return !d.data?._mdxIsPragmaComment
})
}
// When compiling to a function body, replace the import that was just
// generated, and get `jsx`, `jsxs`, and `Fragment` from `arguments[0]`
// instead.
if (outputFormat === 'function-body') {
let index = 0
// Skip directives: JS currently only has `use strict`, but Acorn allows
// arbitrary ones.
// Practically things like `use client` could be used?
while (index < tree.body.length) {
const child = tree.body[index]
if ('directive' in child && child.directive) {
index++
} else {
break
}
}
const declaration = tree.body[index]
if (
declaration &&
declaration.type === 'ImportDeclaration' &&
typeof declaration.source.value === 'string' &&
/\/jsx-(dev-)?runtime$/.test(declaration.source.value)
) {
tree.body[index] = {
type: 'VariableDeclaration',
kind: 'const',
declarations: specifiersToDeclarations(
declaration.specifiers,
toIdOrMemberExpression(['arguments', 0])
)
}
}
}
}
}

View File

@@ -0,0 +1,68 @@
/**
* A plugin that rewrites JSX in functions to accept components as
* `props.components` (when the function is called `_createMdxContent`), or from
* a provider (if there is one).
* It also makes sure that any undefined components are defined: either from
* received components or as a function that throws an error.
*
* @param {Readonly<ProcessorOptions>} options
* Configuration (optional).
* @returns
* Transform.
*/
export function recmaJsxRewrite(options: Readonly<ProcessorOptions>): (tree: Program, file: VFile) => undefined;
export type Expression = import('estree-jsx').Expression;
export type EstreeFunction = import('estree-jsx').Function;
export type Identifier = import('estree-jsx').Identifier;
export type ImportSpecifier = import('estree-jsx').ImportSpecifier;
export type JSXElement = import('estree-jsx').JSXElement;
export type ModuleDeclaration = import('estree-jsx').ModuleDeclaration;
export type Node = import('estree-jsx').Node;
export type ObjectPattern = import('estree-jsx').ObjectPattern;
export type Program = import('estree-jsx').Program;
export type Property = import('estree-jsx').Property;
export type SpreadElement = import('estree-jsx').SpreadElement;
export type Statement = import('estree-jsx').Statement;
export type VariableDeclarator = import('estree-jsx').VariableDeclarator;
export type PeriscopicScope = import('periscopic').Scope;
export type VFile = import('vfile').VFile;
export type ProcessorOptions = import('../core.js').ProcessorOptions;
/**
* Scope (with a `node`).
*/
export type Scope = PeriscopicScope & {
node: Node;
};
/**
* Entry.
*/
export type StackEntry = {
/**
* Used components.
*/
components: Array<string>;
/**
* Map of JSX identifiers which cannot be used as JS identifiers, to valid JS identifiers.
*/
idToInvalidComponentName: Map<string, string>;
/**
* Function.
*/
node: Readonly<EstreeFunction>;
/**
* Identifiers of used objects (such as `x` in `x.y`).
*/
objects: Array<string>;
/**
* Map of JSX identifiers for components and objects, to where they were first used.
*/
references: Record<string, {
node: Readonly<JSXElement>;
component: boolean;
}>;
/**
* Tag names.
*/
tags: Array<string>;
};
//# sourceMappingURL=recma-jsx-rewrite.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"recma-jsx-rewrite.d.ts","sourceRoot":"","sources":["recma-jsx-rewrite.js"],"names":[],"mappings":"AAsDA;;;;;;;;;;;GAWG;AACH,yCALW,SAAS,gBAAgB,CAAC,UASxB,OAAO,QAEP,KAAK,KAEH,SAAS,CAofvB;yBA7jBY,OAAO,YAAY,EAAE,UAAU;6BAC/B,OAAO,YAAY,EAAE,QAAQ;yBAC7B,OAAO,YAAY,EAAE,UAAU;8BAC/B,OAAO,YAAY,EAAE,eAAe;yBACpC,OAAO,YAAY,EAAE,UAAU;gCAC/B,OAAO,YAAY,EAAE,iBAAiB;mBACtC,OAAO,YAAY,EAAE,IAAI;4BACzB,OAAO,YAAY,EAAE,aAAa;sBAClC,OAAO,YAAY,EAAE,OAAO;uBAC5B,OAAO,YAAY,EAAE,QAAQ;4BAC7B,OAAO,YAAY,EAAE,aAAa;wBAClC,OAAO,YAAY,EAAE,SAAS;iCAC9B,OAAO,YAAY,EAAE,kBAAkB;8BAEvC,OAAO,YAAY,EAAE,KAAK;oBAE1B,OAAO,OAAO,EAAE,KAAK;+BAErB,OAAO,YAAY,EAAE,gBAAgB;;;;oBAIrC,eAAe,GAAG;IAAC,IAAI,EAAE,IAAI,CAAA;CAAC;;;;;;;;gBAK7B,MAAM,MAAM,CAAC;;;;8BAEb,IAAI,MAAM,EAAE,MAAM,CAAC;;;;UAEnB,SAAS,cAAc,CAAC;;;;aAExB,MAAM,MAAM,CAAC;;;;gBAEb,OAAO,MAAM,EAAE;QAAC,IAAI,EAAE,SAAS,UAAU,CAAC,CAAC;QAAC,SAAS,EAAE,OAAO,CAAA;KAAC,CAAC;;;;UAEhE,MAAM,MAAM,CAAC"}

View File

@@ -0,0 +1,647 @@
/**
* @typedef {import('estree-jsx').Expression} Expression
* @typedef {import('estree-jsx').Function} EstreeFunction
* @typedef {import('estree-jsx').Identifier} Identifier
* @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier
* @typedef {import('estree-jsx').JSXElement} JSXElement
* @typedef {import('estree-jsx').ModuleDeclaration} ModuleDeclaration
* @typedef {import('estree-jsx').Node} Node
* @typedef {import('estree-jsx').ObjectPattern} ObjectPattern
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('estree-jsx').Property} Property
* @typedef {import('estree-jsx').SpreadElement} SpreadElement
* @typedef {import('estree-jsx').Statement} Statement
* @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator
*
* @typedef {import('periscopic').Scope} PeriscopicScope
*
* @typedef {import('vfile').VFile} VFile
*
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*/
/**
* @typedef {PeriscopicScope & {node: Node}} Scope
* Scope (with a `node`).
*
* @typedef StackEntry
* Entry.
* @property {Array<string>} components
* Used components.
* @property {Map<string, string>} idToInvalidComponentName
* Map of JSX identifiers which cannot be used as JS identifiers, to valid JS identifiers.
* @property {Readonly<EstreeFunction>} node
* Function.
* @property {Array<string>} objects
* Identifiers of used objects (such as `x` in `x.y`).
* @property {Record<string, {node: Readonly<JSXElement>, component: boolean}>} references
* Map of JSX identifiers for components and objects, to where they were first used.
* @property {Array<string>} tags
* Tag names.
*/
import {name as isIdentifierName} from 'estree-util-is-identifier-name'
import {walk} from 'estree-walker'
import {analyze} from 'periscopic'
import {stringifyPosition} from 'unist-util-stringify-position'
import {positionFromEstree} from 'unist-util-position-from-estree'
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
import {toBinaryAddition} from '../util/estree-util-to-binary-addition.js'
import {
toIdOrMemberExpression,
toJsxIdOrMemberExpression
} from '../util/estree-util-to-id-or-member-expression.js'
/**
* A plugin that rewrites JSX in functions to accept components as
* `props.components` (when the function is called `_createMdxContent`), or from
* a provider (if there is one).
* It also makes sure that any undefined components are defined: either from
* received components or as a function that throws an error.
*
* @param {Readonly<ProcessorOptions>} options
* Configuration (optional).
* @returns
* Transform.
*/
export function recmaJsxRewrite(options) {
const {development, outputFormat, providerImportSource} = options
/**
* @param {Program} tree
* Tree.
* @param {VFile} file
* File.
* @returns {undefined}
* Nothing.
*/
return function (tree, file) {
// Find everything thats defined in the top-level scope.
const scopeInfo = analyze(tree)
/** @type {Array<StackEntry>} */
const functionStack = []
let importProvider = false
let createErrorHelper = false
/** @type {Scope | undefined} */
let currentScope
walk(tree, {
enter(node) {
// Cast because we match `node`.
const newScope = /** @type {Scope | undefined} */ (
scopeInfo.map.get(node)
)
if (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
) {
functionStack.push({
components: [],
idToInvalidComponentName: new Map(),
node,
objects: [],
references: {},
tags: []
})
// MDXContent only ever contains MDXLayout
if (
isNamedFunction(node, 'MDXContent') &&
newScope &&
!inScope(newScope, 'MDXLayout')
) {
functionStack[0].components.push('MDXLayout')
}
}
const functionScope = functionStack[0]
if (
!functionScope ||
(!isNamedFunction(functionScope.node, '_createMdxContent') &&
!providerImportSource)
) {
return
}
if (newScope) {
newScope.node = node
currentScope = newScope
}
if (currentScope && node.type === 'JSXElement') {
let name = node.openingElement.name
// `<x.y>`, `<Foo.Bar>`, `<x.y.z>`.
if (name.type === 'JSXMemberExpression') {
/** @type {Array<string>} */
const ids = []
// Find the left-most identifier.
while (name.type === 'JSXMemberExpression') {
ids.unshift(name.property.name)
name = name.object
}
ids.unshift(name.name)
const fullId = ids.join('.')
const id = name.name
const isInScope = inScope(currentScope, id)
if (!Object.hasOwn(functionScope.references, fullId)) {
// Cast because we match `node`.
const parentScope = /** @type {Scope | undefined} */ (
currentScope.parent
)
if (
!isInScope ||
// If the parent scope is `_createMdxContent`, then this
// references a component we can add a check statement for.
(parentScope &&
parentScope.node.type === 'FunctionDeclaration' &&
isNamedFunction(parentScope.node, '_createMdxContent'))
) {
functionScope.references[fullId] = {component: true, node}
}
}
if (!functionScope.objects.includes(id) && !isInScope) {
functionScope.objects.push(id)
}
}
// `<xml:thing>`.
else if (name.type === 'JSXNamespacedName') {
// Ignore namespaces.
}
// If the name is a valid ES identifier, and it doesnt start with a
// lowercase letter, its a component.
// For example, `$foo`, `_bar`, `Baz` are all component names.
// But `foo` and `b-ar` are tag names.
else if (isIdentifierName(name.name) && !/^[a-z]/.test(name.name)) {
const id = name.name
if (!inScope(currentScope, id)) {
// No need to add an error for an undefined layout — we use an
// `if` later.
if (
id !== 'MDXLayout' &&
!Object.hasOwn(functionScope.references, id)
) {
functionScope.references[id] = {component: true, node}
}
if (!functionScope.components.includes(id)) {
functionScope.components.push(id)
}
}
} else if (node.data && node.data._mdxExplicitJsx) {
// Do not turn explicit JSX into components from `_components`.
// As in, a given `h1` component is used for `# heading` (next case),
// but not for `<h1>heading</h1>`.
} else {
const id = name.name
if (!functionScope.tags.includes(id)) {
functionScope.tags.push(id)
}
/** @type {Array<number | string>} */
let jsxIdExpression = ['_components', id]
if (isIdentifierName(id) === false) {
let invalidComponentName =
functionScope.idToInvalidComponentName.get(id)
if (invalidComponentName === undefined) {
invalidComponentName = `_component${functionScope.idToInvalidComponentName.size}`
functionScope.idToInvalidComponentName.set(
id,
invalidComponentName
)
}
jsxIdExpression = [invalidComponentName]
}
node.openingElement.name =
toJsxIdOrMemberExpression(jsxIdExpression)
if (node.closingElement) {
node.closingElement.name =
toJsxIdOrMemberExpression(jsxIdExpression)
}
}
}
},
leave(node) {
/** @type {Array<Property | SpreadElement>} */
const defaults = []
/** @type {Array<string>} */
const actual = []
/** @type {Array<Expression>} */
const parameters = []
/** @type {Array<VariableDeclarator>} */
const declarations = []
if (currentScope && currentScope.node === node) {
// Cast to patch our `node`.
currentScope = /** @type {Scope} */ (currentScope.parent)
}
if (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
) {
const scopeNode = node
const scope = functionStack[functionStack.length - 1]
/** @type {string} */
let name
for (name of scope.tags.sort()) {
defaults.push({
type: 'Property',
kind: 'init',
key: isIdentifierName(name)
? {type: 'Identifier', name}
: {type: 'Literal', value: name},
value: {type: 'Literal', value: name},
method: false,
shorthand: false,
computed: false
})
}
actual.push(...scope.components)
for (name of scope.objects) {
// In some cases, a component is used directly (`<X>`) but its also
// used as an object (`<X.Y>`).
if (!actual.includes(name)) {
actual.push(name)
}
}
actual.sort()
/** @type {Array<Statement>} */
const statements = []
if (
defaults.length > 0 ||
actual.length > 0 ||
scope.idToInvalidComponentName.size > 0
) {
if (providerImportSource) {
importProvider = true
parameters.push({
type: 'CallExpression',
callee: {type: 'Identifier', name: '_provideComponents'},
arguments: [],
optional: false
})
}
// Accept `components` as a prop if this is the `MDXContent` or
// `_createMdxContent` function.
if (
isNamedFunction(scope.node, 'MDXContent') ||
isNamedFunction(scope.node, '_createMdxContent')
) {
parameters.push(toIdOrMemberExpression(['props', 'components']))
}
if (defaults.length > 0 || parameters.length > 1) {
for (const parameter of parameters) {
defaults.push({type: 'SpreadElement', argument: parameter})
}
}
// If were getting components from several sources, merge them.
/** @type {Expression} */
let componentsInit =
defaults.length > 0
? {type: 'ObjectExpression', properties: defaults}
: // If were only getting components from `props.components`,
// make sure its defined.
{
type: 'LogicalExpression',
operator: '||',
left: parameters[0],
right: {type: 'ObjectExpression', properties: []}
}
/** @type {ObjectPattern | undefined} */
let componentsPattern
// Add components to scope.
// For `['MyComponent', 'MDXLayout']` this generates:
// ```tsx
// const {MyComponent, wrapper: MDXLayout} = _components
// ```
// Note that MDXLayout is special as its taken from
// `_components.wrapper`.
if (actual.length > 0) {
componentsPattern = {
type: 'ObjectPattern',
properties: actual.map(function (name) {
return {
type: 'Property',
kind: 'init',
key: {
type: 'Identifier',
name: name === 'MDXLayout' ? 'wrapper' : name
},
value: {type: 'Identifier', name},
method: false,
shorthand: name !== 'MDXLayout',
computed: false
}
})
}
}
if (scope.tags.length > 0) {
declarations.push({
type: 'VariableDeclarator',
id: {type: 'Identifier', name: '_components'},
init: componentsInit
})
componentsInit = {type: 'Identifier', name: '_components'}
}
if (isNamedFunction(scope.node, '_createMdxContent')) {
for (const [id, componentName] of [
...scope.idToInvalidComponentName
].sort(function ([a], [b]) {
return a.localeCompare(b)
})) {
// For JSX IDs that cant be represented as JavaScript IDs (as in,
// those with dashes, such as `custom-element`), generate a
// separate variable that is a valid JS ID (such as `_component0`),
// and takes it from components:
// `const _component0 = _components['custom-element']`
declarations.push({
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: componentName
},
init: {
type: 'MemberExpression',
object: {type: 'Identifier', name: '_components'},
property: {type: 'Literal', value: id},
computed: true,
optional: false
}
})
}
}
if (componentsPattern) {
declarations.push({
type: 'VariableDeclarator',
id: componentsPattern,
init: componentsInit
})
}
if (declarations.length > 0) {
statements.push({
type: 'VariableDeclaration',
kind: 'const',
declarations
})
}
}
/** @type {string} */
let key
// Add partials (so for `x.y.z` itd generate `x` and `x.y` too).
for (key in scope.references) {
if (Object.hasOwn(scope.references, key)) {
const parts = key.split('.')
let index = 0
while (++index < parts.length) {
const partial = parts.slice(0, index).join('.')
if (!Object.hasOwn(scope.references, partial)) {
scope.references[partial] = {
component: false,
node: scope.references[key].node
}
}
}
}
}
const references = Object.keys(scope.references).sort()
let index = -1
while (++index < references.length) {
const id = references[index]
const info = scope.references[id]
const place = stringifyPosition(positionFromEstree(info.node))
/** @type {Array<Expression>} */
const parameters = [
{type: 'Literal', value: id},
{type: 'Literal', value: info.component}
]
createErrorHelper = true
if (development && place) {
parameters.push({type: 'Literal', value: place})
}
statements.push({
type: 'IfStatement',
test: {
type: 'UnaryExpression',
operator: '!',
prefix: true,
argument: toIdOrMemberExpression(id.split('.'))
},
consequent: {
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {type: 'Identifier', name: '_missingMdxReference'},
arguments: parameters,
optional: false
}
},
alternate: undefined
})
}
if (statements.length > 0) {
// Arrow functions with an implied return:
if (scopeNode.body.type !== 'BlockStatement') {
scopeNode.body = {
type: 'BlockStatement',
body: [{type: 'ReturnStatement', argument: scopeNode.body}]
}
}
scopeNode.body.body.unshift(...statements)
}
functionStack.pop()
}
}
})
// If a provider is used (and can be used), import it.
if (importProvider && providerImportSource) {
tree.body.unshift(
createImportProvider(providerImportSource, outputFormat)
)
}
// If potentially missing components are used.
if (createErrorHelper) {
/** @type {Array<Expression>} */
const message = [
{type: 'Literal', value: 'Expected '},
{
type: 'ConditionalExpression',
test: {type: 'Identifier', name: 'component'},
consequent: {type: 'Literal', value: 'component'},
alternate: {type: 'Literal', value: 'object'}
},
{type: 'Literal', value: ' `'},
{type: 'Identifier', name: 'id'},
{
type: 'Literal',
value:
'` to be defined: you likely forgot to import, pass, or provide it.'
}
]
/** @type {Array<Identifier>} */
const parameters = [
{type: 'Identifier', name: 'id'},
{type: 'Identifier', name: 'component'}
]
if (development) {
message.push({
type: 'ConditionalExpression',
test: {type: 'Identifier', name: 'place'},
consequent: toBinaryAddition([
{type: 'Literal', value: '\nIts referenced in your code at `'},
{type: 'Identifier', name: 'place'},
{
type: 'Literal',
value: (file.path ? '` in `' + file.path : '') + '`'
}
]),
alternate: {type: 'Literal', value: ''}
})
parameters.push({type: 'Identifier', name: 'place'})
}
tree.body.push({
type: 'FunctionDeclaration',
id: {type: 'Identifier', name: '_missingMdxReference'},
generator: false,
async: false,
params: parameters,
body: {
type: 'BlockStatement',
body: [
{
type: 'ThrowStatement',
argument: {
type: 'NewExpression',
callee: {type: 'Identifier', name: 'Error'},
arguments: [toBinaryAddition(message)]
}
}
]
}
})
}
if (outputFormat === 'function-body') {
tree.body.unshift({
type: 'ExpressionStatement',
expression: {type: 'Literal', value: 'use strict'},
directive: 'use strict'
})
}
}
}
/**
* @param {string} providerImportSource
* Provider source.
* @param {'function-body' | 'program' | null | undefined} outputFormat
* Format.
* @returns {ModuleDeclaration | Statement}
* Node.
*/
function createImportProvider(providerImportSource, outputFormat) {
/** @type {Array<ImportSpecifier>} */
const specifiers = [
{
type: 'ImportSpecifier',
imported: {type: 'Identifier', name: 'useMDXComponents'},
local: {type: 'Identifier', name: '_provideComponents'}
}
]
return outputFormat === 'function-body'
? {
type: 'VariableDeclaration',
kind: 'const',
declarations: specifiersToDeclarations(
specifiers,
toIdOrMemberExpression(['arguments', 0])
)
}
: {
type: 'ImportDeclaration',
specifiers,
source: {type: 'Literal', value: providerImportSource}
}
}
/**
* @param {Readonly<EstreeFunction>} node
* Node.
* @param {string} name
* Name.
* @returns {boolean}
* Whether `node` is a named function with `name`.
*/
function isNamedFunction(node, name) {
return Boolean(node && 'id' in node && node.id && node.id.name === name)
}
/**
* @param {Readonly<Scope>} scope
* Scope.
* @param {string} id
* Identifier.
* @returns {boolean}
* Whether `id` is in `scope`.
*/
function inScope(scope, id) {
/** @type {Scope | undefined} */
let currentScope = scope
while (currentScope) {
if (currentScope.declarations.has(id)) {
return true
}
// Cast to patch our `node`.
currentScope = /** @type {Scope | undefined} */ (
currentScope.parent || undefined
)
}
return false
}

View File

@@ -0,0 +1,6 @@
export function recmaStringify(this: import("unified").Processor<undefined, undefined, undefined, undefined, undefined>, parameters_0: Readonly<import("../core.js").ProcessorOptions>): void | undefined;
export type Program = import('estree-jsx').Program;
export type Processor = import('unified').Processor<undefined, undefined, undefined, Program, string>;
export type VFile = import('vfile').VFile;
export type ProcessorOptions = import('../core.js').ProcessorOptions;
//# sourceMappingURL=recma-stringify.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"recma-stringify.d.ts","sourceRoot":"","sources":["recma-stringify.js"],"names":[],"mappings":"AA6CwinC,0MAA6U;sBA5Cx2nC,OAAO,YAAY,EAAE,OAAO;wBAC5B,OAAO,SAAS,EAAE,SAAS,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC;oBAC7E,OAAO,OAAO,EAAE,KAAK;+BAErB,OAAO,YAAY,EAAE,gBAAgB"}

45
node_modules/@mdx-js/mdx/lib/plugin/recma-stringify.js generated vendored Normal file
View File

@@ -0,0 +1,45 @@
/**
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('unified').Processor<undefined, undefined, undefined, Program, string>} Processor
* @typedef {import('vfile').VFile} VFile
*
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*/
import {jsx, toJs} from 'estree-util-to-js'
/**
* Serialize an esast (estree) program to JavaScript.
*
* @type {import('unified').Plugin<[Readonly<ProcessorOptions>], Program, string>}
*/
export function recmaStringify(options) {
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
const {SourceMapGenerator} = options
self.compiler = compiler
/**
* @param {Program} tree
* Tree.
* @param {VFile} file
* File.
* @returns {string}
* JavaScript.
*/
function compiler(tree, file) {
const result = SourceMapGenerator
? toJs(tree, {
SourceMapGenerator,
filePath: file.path || 'unknown.mdx',
handlers: jsx
})
: toJs(tree, {handlers: jsx})
file.map = result.map
return result.value
}
}

14
node_modules/@mdx-js/mdx/lib/plugin/rehype-recma.d.ts generated vendored Normal file
View File

@@ -0,0 +1,14 @@
/**
* A plugin to transform an HTML (hast) tree to a JS (estree).
* `hast-util-to-estree` does all the work for us!
*
* @param {Readonly<ProcessorOptions>} options
* Configuration (optional).
* @returns
* Transform.
*/
export function rehypeRecma(options: Readonly<ProcessorOptions>): (tree: Root) => Program;
export type Program = import('estree-jsx').Program;
export type Root = import('hast').Root;
export type ProcessorOptions = import('../core.js').ProcessorOptions;
//# sourceMappingURL=rehype-recma.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"rehype-recma.d.ts","sourceRoot":"","sources":["rehype-recma.js"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AACH,qCALW,SAAS,gBAAgB,CAAC,UAOxB,IAAI,KAEF,OAAO,CAMrB;sBA5BY,OAAO,YAAY,EAAE,OAAO;mBAE5B,OAAO,MAAM,EAAE,IAAI;+BAEnB,OAAO,YAAY,EAAE,gBAAgB"}

30
node_modules/@mdx-js/mdx/lib/plugin/rehype-recma.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
/**
* @typedef {import('estree-jsx').Program} Program
*
* @typedef {import('hast').Root} Root
*
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*/
import {toEstree} from 'hast-util-to-estree'
/**
* A plugin to transform an HTML (hast) tree to a JS (estree).
* `hast-util-to-estree` does all the work for us!
*
* @param {Readonly<ProcessorOptions>} options
* Configuration (optional).
* @returns
* Transform.
*/
export function rehypeRecma(options) {
/**
* @param {Root} tree
* Tree (hast).
* @returns {Program}
* Program (esast).
*/
return function (tree) {
return toEstree(tree, options)
}
}

View File

@@ -0,0 +1,12 @@
/**
* A tiny plugin that removes raw HTML.
*
* This is needed if the format is `md` and `rehype-raw` was not used to parse
* dangerous HTML into nodes.
*
* @returns
* Transform.
*/
export function rehypeRemoveRaw(): (tree: Root) => undefined;
export type Root = import('hast').Root;
//# sourceMappingURL=rehype-remove-raw.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"rehype-remove-raw.d.ts","sourceRoot":"","sources":["rehype-remove-raw.js"],"names":[],"mappings":"AAMA;;;;;;;;GAQG;AACH,0CAEa,IAAI,KAEF,SAAS,CAWvB;mBA7BY,OAAO,MAAM,EAAE,IAAI"}

View File

@@ -0,0 +1,31 @@
/**
* @typedef {import('hast').Root} Root
*/
import {visit} from 'unist-util-visit'
/**
* A tiny plugin that removes raw HTML.
*
* This is needed if the format is `md` and `rehype-raw` was not used to parse
* dangerous HTML into nodes.
*
* @returns
* Transform.
*/
export function rehypeRemoveRaw() {
/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, 'raw', function (_, index, parent) {
if (parent && typeof index === 'number') {
parent.children.splice(index, 1)
return index
}
})
}
}

View File

@@ -0,0 +1,14 @@
/**
* A tiny plugin that unravels `<p><h1>x</h1></p>` but also
* `<p><Component /></p>` (so it has no knowledge of “HTML”).
*
* It also marks JSX as being explicitly JSX, so when a user passes a `h1`
* component, it is used for `# heading` but not for `<h1>heading</h1>`.
*
* @returns
* Transform.
*/
export function remarkMarkAndUnravel(): (tree: Root) => undefined;
export type Root = import('mdast').Root;
export type RootContent = import('mdast').RootContent;
//# sourceMappingURL=remark-mark-and-unravel.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"remark-mark-and-unravel.d.ts","sourceRoot":"","sources":["remark-mark-and-unravel.js"],"names":[],"mappings":"AAQA;;;;;;;;;GASG;AACH,+CAEa,IAAI,KAEF,SAAS,CA0EvB;mBA/FY,OAAO,OAAO,EAAE,IAAI;0BACpB,OAAO,OAAO,EAAE,WAAW"}

View File

@@ -0,0 +1,97 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').RootContent} RootContent
*/
import {collapseWhiteSpace} from 'collapse-white-space'
import {visit} from 'unist-util-visit'
/**
* A tiny plugin that unravels `<p><h1>x</h1></p>` but also
* `<p><Component /></p>` (so it has no knowledge of “HTML”).
*
* It also marks JSX as being explicitly JSX, so when a user passes a `h1`
* component, it is used for `# heading` but not for `<h1>heading</h1>`.
*
* @returns
* Transform.
*/
export function remarkMarkAndUnravel() {
/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, function (node, index, parent) {
let offset = -1
let all = true
let oneOrMore = false
if (parent && typeof index === 'number' && node.type === 'paragraph') {
const children = node.children
while (++offset < children.length) {
const child = children[offset]
if (
child.type === 'mdxJsxTextElement' ||
child.type === 'mdxTextExpression'
) {
oneOrMore = true
} else if (
child.type === 'text' &&
collapseWhiteSpace(child.value, {style: 'html', trim: true}) === ''
) {
// Empty.
} else {
all = false
break
}
}
if (all && oneOrMore) {
offset = -1
/** @type {Array<RootContent>} */
const newChildren = []
while (++offset < children.length) {
const child = children[offset]
if (child.type === 'mdxJsxTextElement') {
// @ts-expect-error: mutate because it is faster; content model is fine.
child.type = 'mdxJsxFlowElement'
}
if (child.type === 'mdxTextExpression') {
// @ts-expect-error: mutate because it is faster; content model is fine.
child.type = 'mdxFlowExpression'
}
if (
child.type === 'text' &&
/^[\t\r\n ]+$/.test(String(child.value))
) {
// Empty.
} else {
newChildren.push(child)
}
}
parent.children.splice(index, 1, ...newChildren)
return index
}
}
if (
node.type === 'mdxJsxFlowElement' ||
node.type === 'mdxJsxTextElement'
) {
const data = node.data || (node.data = {})
data._mdxExplicitJsx = true
}
})
}
}