🎉 initiate project *astro_rewrite*

This commit is contained in:
sindrekjelsrud 2023-07-19 21:31:30 +02:00
parent ffd4d5e86c
commit 2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions

View file

@ -0,0 +1,17 @@
/**
* Create smart processors to handle different formats.
*
* @param {CompileOptions | null | undefined} [compileOptions]
* configuration.
* @return {{extnames: Array<string>, process: process, processSync: processSync}}
* Smart processor.
*/
export function createFormatAwareProcessors(compileOptions?: CompileOptions | null | undefined): {
extnames: Array<string>;
process: (vfileCompatible: VFileCompatible) => Promise<VFile>;
processSync: (vfileCompatible: VFileCompatible) => VFile;
};
export type Processor = import('unified').Processor;
export type VFile = import('vfile').VFile;
export type VFileCompatible = import('vfile').VFileCompatible;
export type CompileOptions = import('../compile.js').CompileOptions;

View file

@ -0,0 +1,91 @@
/**
* @typedef {import('unified').Processor} Processor
* @typedef {import('vfile').VFile} VFile
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('../compile.js').CompileOptions} CompileOptions
*/
import {createProcessor} from '../core.js'
import {md, mdx} from './extnames.js'
import {resolveFileAndOptions} from './resolve-file-and-options.js'
/**
* Create smart processors to handle different formats.
*
* @param {CompileOptions | null | undefined} [compileOptions]
* configuration.
* @return {{extnames: Array<string>, process: process, processSync: processSync}}
* Smart processor.
*/
export function createFormatAwareProcessors(compileOptions) {
const compileOptions_ = compileOptions || {}
const mdExtensions = compileOptions_.mdExtensions || md
const mdxExtensions = compileOptions_.mdxExtensions || mdx
/** @type {Processor} */
let cachedMarkdown
/** @type {Processor} */
let cachedMdx
return {
extnames:
compileOptions_.format === 'md'
? mdExtensions
: compileOptions_.format === 'mdx'
? mdxExtensions
: mdExtensions.concat(mdxExtensions),
process,
processSync
}
/**
* Smart processor.
*
* @param {VFileCompatible} vfileCompatible
* MDX or markdown document.
* @return {Promise<VFile>}
* File.
*/
function process(vfileCompatible) {
const {file, processor} = split(vfileCompatible)
return processor.process(file)
}
/**
* Sync smart processor.
*
* @param {VFileCompatible} vfileCompatible
* MDX or markdown document.
* @return {VFile}
* File.
*/
// C8 does not cover `.cjs` files (this is only used for the require hook,
// which has to be CJS).
/* c8 ignore next 4 */
function processSync(vfileCompatible) {
const {file, processor} = split(vfileCompatible)
return processor.processSync(file)
}
/**
* Make a full vfile from whats given, and figure out which processor
* should be used for it.
* This caches processors (one for markdown and one for MDX) so that they do
* not have to be reconstructed for each file.
*
* @param {VFileCompatible} vfileCompatible
* MDX or markdown document.
* @return {{file: VFile, processor: Processor}}
* File and corresponding processor.
*/
function split(vfileCompatible) {
const {file, options} = resolveFileAndOptions(
vfileCompatible,
compileOptions_
)
const processor =
options.format === 'md'
? cachedMarkdown || (cachedMarkdown = createProcessor(options))
: cachedMdx || (cachedMdx = createProcessor(options))
return {file, processor}
}
}

View file

@ -0,0 +1,13 @@
/**
* @typedef {import('estree-jsx').Node} Node
*/
/**
* @param {Node} from
* Node to take from.
* @param {Node} to
* Node to add to.
* @returns {void}
* Nothing.
*/
export function create(from: Node, to: Node): void;
export type Node = import('estree-jsx').Node;

View file

@ -0,0 +1,27 @@
/**
* @typedef {import('estree-jsx').Node} Node
*/
/**
* @param {Node} from
* Node to take from.
* @param {Node} to
* Node to add to.
* @returns {void}
* Nothing.
*/
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 from) {
// @ts-expect-error: assume theyre settable.
to[field] = from[field]
}
}
}

View file

@ -0,0 +1,19 @@
/**
* @typedef {import('estree-jsx').Declaration} Declaration
* @typedef {import('estree-jsx').Expression} Expression
*/
/**
* Turn a declaration into an expression.
*
* Doesnt work for variable declarations, but thats fine for our use case
* because currently were using this utility for export default declarations,
* which cant contain variable declarations.
*
* @param {Declaration} declaration
* Declaration.
* @returns {Expression}
* Expression.
*/
export function declarationToExpression(declaration: Declaration): Expression;
export type Declaration = import('estree-jsx').Declaration;
export type Expression = import('estree-jsx').Expression;

View file

@ -0,0 +1,32 @@
/**
* @typedef {import('estree-jsx').Declaration} Declaration
* @typedef {import('estree-jsx').Expression} Expression
*/
/**
* Turn a declaration into an expression.
*
* Doesnt work for variable declarations, but thats fine for our use case
* because currently were using this utility for export default declarations,
* which cant contain variable declarations.
*
* @param {Declaration} declaration
* Declaration.
* @returns {Expression}
* Expression.
*/
export function declarationToExpression(declaration) {
if (declaration.type === 'FunctionDeclaration') {
return {...declaration, type: 'FunctionExpression'}
}
if (declaration.type === 'ClassDeclaration') {
return {...declaration, type: 'ClassExpression'}
/* Internal utility so the next shouldnt happen or a maintainer is making a
* mistake. */
/* c8 ignore next 4 */
}
// Probably `VariableDeclaration`.
throw new Error('Cannot turn `' + declaration.type + '` into an expression')
}

View file

@ -0,0 +1,15 @@
/**
* @typedef {import('estree-jsx').Node} Node
* @typedef {import('estree-jsx').Declaration} Declaration
*/
/**
* Check if `node` is a declaration.
*
* @param {Node} node
* Node to check.
* @returns {node is Declaration}
* Whether `node` is a declaration.
*/
export function isDeclaration(node: Node): node is import("estree").Declaration;
export type Node = import('estree-jsx').Node;
export type Declaration = import('estree-jsx').Declaration;

View file

@ -0,0 +1,20 @@
/**
* @typedef {import('estree-jsx').Node} Node
* @typedef {import('estree-jsx').Declaration} Declaration
*/
/**
* Check if `node` is a declaration.
*
* @param {Node} node
* Node to check.
* @returns {node is Declaration}
* Whether `node` is a declaration.
*/
export function isDeclaration(node) {
return Boolean(
node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration' ||
node.type === 'VariableDeclaration'
)
}

View file

@ -0,0 +1,14 @@
/**
* @param {Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier>} specifiers
* @param {Expression} init
* @returns {Array<VariableDeclarator>}
*/
export function specifiersToDeclarations(specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier>, init: Expression): Array<VariableDeclarator>;
export type AssignmentProperty = import('estree-jsx').AssignmentProperty;
export type ExportSpecifier = import('estree-jsx').ExportSpecifier;
export type Expression = import('estree-jsx').Expression;
export type Identifier = import('estree-jsx').Identifier;
export type ImportDefaultSpecifier = import('estree-jsx').ImportDefaultSpecifier;
export type ImportNamespaceSpecifier = import('estree-jsx').ImportNamespaceSpecifier;
export type ImportSpecifier = import('estree-jsx').ImportSpecifier;
export type VariableDeclarator = import('estree-jsx').VariableDeclarator;

View file

@ -0,0 +1,90 @@
/**
* @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').ImportDefaultSpecifier} ImportDefaultSpecifier
* @typedef {import('estree-jsx').ImportNamespaceSpecifier} ImportNamespaceSpecifier
* @typedef {import('estree-jsx').ImportSpecifier} ImportSpecifier
* @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator
*/
import {create} from './estree-util-create.js'
/**
* @param {Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier>} specifiers
* @param {Expression} init
* @returns {Array<VariableDeclarator>}
*/
export function specifiersToDeclarations(specifiers, init) {
let index = -1
/** @type {Array<VariableDeclarator>} */
const declarations = []
/** @type {Array<ImportSpecifier | ImportDefaultSpecifier | ExportSpecifier>} */
const otherSpecifiers = []
// Can only be one according to JS syntax.
/** @type {ImportNamespaceSpecifier | undefined} */
let importNamespaceSpecifier
while (++index < specifiers.length) {
const specifier = specifiers[index]
if (specifier.type === 'ImportNamespaceSpecifier') {
importNamespaceSpecifier = specifier
} else {
otherSpecifiers.push(specifier)
}
}
if (importNamespaceSpecifier) {
/** @type {VariableDeclarator} */
const declarator = {
type: 'VariableDeclarator',
id: importNamespaceSpecifier.local,
init
}
create(importNamespaceSpecifier, declarator)
declarations.push(declarator)
}
declarations.push({
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: otherSpecifiers.map((specifier) => {
/** @type {Identifier} */
let key =
specifier.type === 'ImportSpecifier'
? specifier.imported
: specifier.type === 'ExportSpecifier'
? specifier.exported
: {type: 'Identifier', name: 'default'}
let value = specifier.local
// Switch them around if were exporting.
if (specifier.type === 'ExportSpecifier') {
value = key
key = specifier.local
}
/** @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
? {type: 'Identifier', name: importNamespaceSpecifier.local.name}
: init
})
return declarations
}

View file

@ -0,0 +1,8 @@
/**
* @typedef {import('estree-jsx').Expression} Expression
*/
/**
* @param {Array<Expression>} expressions
*/
export function toBinaryAddition(expressions: Array<Expression>): import("estree").Expression;
export type Expression = import('estree-jsx').Expression;

View file

@ -0,0 +1,23 @@
/**
* @typedef {import('estree-jsx').Expression} Expression
*/
/**
* @param {Array<Expression>} expressions
*/
export function toBinaryAddition(expressions) {
let index = -1
/** @type {Expression | undefined} */
let left
while (++index < expressions.length) {
const right = expressions[index]
left = left ? {type: 'BinaryExpression', left, operator: '+', right} : right
}
// Just for types.
/* c8 ignore next */
if (!left) throw new Error('Expected non-empty `expressions` to be passed')
return left
}

View file

@ -0,0 +1,11 @@
/**
* @param {Array<string | number>} ids
* @returns {Identifier | MemberExpression}
*/
export function toIdOrMemberExpression(ids: Array<string | number>): Identifier | MemberExpression;
export function toJsxIdOrMemberExpression(ids: Array<string | number>): JSXIdentifier | JSXMemberExpression;
export type Identifier = import('estree-jsx').Identifier;
export type JSXIdentifier = import('estree-jsx').JSXIdentifier;
export type JSXMemberExpression = import('estree-jsx').JSXMemberExpression;
export type Literal = import('estree-jsx').Literal;
export type MemberExpression = import('estree-jsx').MemberExpression;

View file

@ -0,0 +1,108 @@
/**
* @typedef {import('estree-jsx').Identifier} Identifier
* @typedef {import('estree-jsx').JSXIdentifier} JSXIdentifier
* @typedef {import('estree-jsx').JSXMemberExpression} JSXMemberExpression
* @typedef {import('estree-jsx').Literal} Literal
* @typedef {import('estree-jsx').MemberExpression} MemberExpression
*/
import {
start as esStart,
cont as esCont,
name as isIdentifierName
} from 'estree-util-is-identifier-name'
export const toIdOrMemberExpression = toIdOrMemberExpressionFactory(
'Identifier',
'MemberExpression',
isIdentifierName
)
export const toJsxIdOrMemberExpression =
// @ts-expect-error: fine
/** @type {(ids: Array<string | number>) => JSXIdentifier | JSXMemberExpression)} */
(
toIdOrMemberExpressionFactory(
'JSXIdentifier',
'JSXMemberExpression',
isJsxIdentifierName
)
)
/**
* @param {string} idType
* @param {string} memberType
* @param {(value: string) => boolean} isIdentifier
*/
function toIdOrMemberExpressionFactory(idType, memberType, isIdentifier) {
return toIdOrMemberExpression
/**
* @param {Array<string | number>} ids
* @returns {Identifier | MemberExpression}
*/
function toIdOrMemberExpression(ids) {
let index = -1
/** @type {Identifier | Literal | MemberExpression | undefined} */
let object
while (++index < ids.length) {
const name = ids[index]
const valid = typeof name === 'string' && isIdentifier(name)
// A value of `asd.123` could be turned into `asd['123']` in the JS form,
// but JSX does not have a form for it, so throw.
/* c8 ignore next 3 */
if (idType === 'JSXIdentifier' && !valid) {
throw new Error('Cannot turn `' + name + '` into a JSX identifier')
}
/** @type {Identifier | Literal} */
// @ts-expect-error: JSX is fine.
const id = valid ? {type: idType, name} : {type: 'Literal', value: name}
// @ts-expect-error: JSX is fine.
object = object
? {
type: memberType,
object,
property: id,
computed: id.type === 'Literal',
optional: false
}
: id
}
// Just for types.
/* c8 ignore next 3 */
if (!object) throw new Error('Expected non-empty `ids` to be passed')
if (object.type === 'Literal')
throw new Error('Expected identifier as left-most value')
return object
}
}
/**
* Checks if the given string is a valid JSX identifier name.
* @param {string} name
*/
function isJsxIdentifierName(name) {
let index = -1
while (++index < name.length) {
// We currently receive valid input, but this catches bugs and is needed
// when externalized.
/* c8 ignore next */
if (!(index ? jsxCont : esStart)(name.charCodeAt(index))) return false
}
// `false` if `name` is empty.
return index > 0
}
/**
* Checks if the given character code can continue a JSX identifier.
* @param {number} code
*/
function jsxCont(code) {
return code === 45 /* `-` */ || esCont(code)
}

View file

@ -0,0 +1,9 @@
/**
* Utility to turn a list of extnames (*with* dots) into an expression.
*
* @param {Array<string>} extnames
* List of extnames.
* @returns {RegExp}
* Regex matching them.
*/
export function extnamesToRegex(extnames: Array<string>): RegExp;

14
node_modules/@mdx-js/mdx/lib/util/extnames-to-regex.js generated vendored Normal file
View file

@ -0,0 +1,14 @@
/**
* Utility to turn a list of extnames (*with* dots) into an expression.
*
* @param {Array<string>} extnames
* List of extnames.
* @returns {RegExp}
* Regex matching them.
*/
export function extnamesToRegex(extnames) {
// eslint-disable-next-line security/detect-non-literal-regexp
return new RegExp(
'\\.(' + extnames.map((d) => d.slice(1)).join('|') + ')([?#]|$)'
)
}

3
node_modules/@mdx-js/mdx/lib/util/extnames.d.ts generated vendored Normal file
View file

@ -0,0 +1,3 @@
export const mdx: string[];
/** @type {Array<string>} */
export const md: Array<string>;

6
node_modules/@mdx-js/mdx/lib/util/extnames.js generated vendored Normal file
View file

@ -0,0 +1,6 @@
// @ts-expect-error: untyped.
import markdownExtensions from 'markdown-extensions'
export const mdx = ['.mdx']
/** @type {Array<string>} */
export const md = markdownExtensions.map((/** @type {string} */ d) => '.' + d)

View file

@ -0,0 +1,66 @@
/**
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*
* @typedef RunnerOptions
* Configuration with JSX runtime.
* @property {any} Fragment
* Symbol to use for fragments.
* @property {any} [jsx]
* Function to generate an element with static children in production mode.
* @property {any} [jsxs]
* Function to generate an element with dynamic children in production mode.
* @property {any} [jsxDEV]
* Function to generate an element in development mode.
* @property {any} [useMDXComponents]
* Function to get `MDXComponents` from context.
*
* @typedef {Omit<ProcessorOptions, 'jsx' | 'jsxImportSource' | 'jsxRuntime' | 'pragma' | 'pragmaFrag' | 'pragmaImportSource' | 'providerImportSource' | 'outputFormat'> } EvaluateProcessorOptions
* Compile configuration without JSX options for evaluation.
*
* @typedef {EvaluateProcessorOptions & RunnerOptions} EvaluateOptions
* Configuration for evaluation.
*/
/**
* Split compiletime options from runtime options.
*
* @param {EvaluateOptions | null | undefined} options
* @returns {{compiletime: ProcessorOptions, runtime: RunnerOptions}}
*/
export function resolveEvaluateOptions(options: EvaluateOptions | null | undefined): {
compiletime: ProcessorOptions;
runtime: RunnerOptions;
};
export type ProcessorOptions = import('../core.js').ProcessorOptions;
/**
* Configuration with JSX runtime.
*/
export type RunnerOptions = {
/**
* Symbol to use for fragments.
*/
Fragment: any;
/**
* Function to generate an element with static children in production mode.
*/
jsx?: any;
/**
* Function to generate an element with dynamic children in production mode.
*/
jsxs?: any;
/**
* Function to generate an element in development mode.
*/
jsxDEV?: any;
/**
* Function to get `MDXComponents` from context.
*/
useMDXComponents?: any;
};
/**
* Compile configuration without JSX options for evaluation.
*/
export type EvaluateProcessorOptions = Omit<ProcessorOptions, 'jsx' | 'jsxImportSource' | 'jsxRuntime' | 'pragma' | 'pragmaFrag' | 'pragmaImportSource' | 'providerImportSource' | 'outputFormat'>;
/**
* Configuration for evaluation.
*/
export type EvaluateOptions = EvaluateProcessorOptions & RunnerOptions;

View file

@ -0,0 +1,51 @@
/**
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*
* @typedef RunnerOptions
* Configuration with JSX runtime.
* @property {any} Fragment
* Symbol to use for fragments.
* @property {any} [jsx]
* Function to generate an element with static children in production mode.
* @property {any} [jsxs]
* Function to generate an element with dynamic children in production mode.
* @property {any} [jsxDEV]
* Function to generate an element in development mode.
* @property {any} [useMDXComponents]
* Function to get `MDXComponents` from context.
*
* @typedef {Omit<ProcessorOptions, 'jsx' | 'jsxImportSource' | 'jsxRuntime' | 'pragma' | 'pragmaFrag' | 'pragmaImportSource' | 'providerImportSource' | 'outputFormat'> } EvaluateProcessorOptions
* Compile configuration without JSX options for evaluation.
*
* @typedef {EvaluateProcessorOptions & RunnerOptions} EvaluateOptions
* Configuration for evaluation.
*/
/**
* Split compiletime options from runtime options.
*
* @param {EvaluateOptions | null | undefined} options
* @returns {{compiletime: ProcessorOptions, runtime: RunnerOptions}}
*/
export function resolveEvaluateOptions(options) {
const {development, Fragment, jsx, jsxs, jsxDEV, useMDXComponents, ...rest} =
options || {}
if (!Fragment) throw new Error('Expected `Fragment` given to `evaluate`')
if (development) {
if (!jsxDEV) throw new Error('Expected `jsxDEV` given to `evaluate`')
} else {
if (!jsx) throw new Error('Expected `jsx` given to `evaluate`')
if (!jsxs) throw new Error('Expected `jsxs` given to `evaluate`')
}
return {
compiletime: {
...rest,
development,
outputFormat: 'function-body',
providerImportSource: useMDXComponents ? '#' : undefined
},
runtime: {Fragment, jsx, jsxs, jsxDEV, useMDXComponents}
}
}

View file

@ -0,0 +1,16 @@
/**
* Create a file and options from a given `vfileCompatible` and options that
* might contain `format: 'detect'`.
*
* @param {VFileCompatible} vfileCompatible
* @param {CompileOptions | null | undefined} [options]
* @returns {{file: VFile, options: ProcessorOptions}}
*/
export function resolveFileAndOptions(vfileCompatible: VFileCompatible, options?: CompileOptions | null | undefined): {
file: VFile;
options: ProcessorOptions;
};
export type VFileCompatible = import('vfile').VFileCompatible;
export type ProcessorOptions = import('../core.js').ProcessorOptions;
export type CompileOptions = import('../compile.js').CompileOptions;
import { VFile } from "vfile";

View file

@ -0,0 +1,48 @@
/**
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
* @typedef {import('../compile.js').CompileOptions} CompileOptions
*/
import {VFile} from 'vfile'
import {md} from './extnames.js'
/**
* Create a file and options from a given `vfileCompatible` and options that
* might contain `format: 'detect'`.
*
* @param {VFileCompatible} vfileCompatible
* @param {CompileOptions | null | undefined} [options]
* @returns {{file: VFile, options: ProcessorOptions}}
*/
export function resolveFileAndOptions(vfileCompatible, options) {
const file = looksLikeAVFile(vfileCompatible)
? vfileCompatible
: new VFile(vfileCompatible)
const {format, ...rest} = options || {}
return {
file,
options: {
format:
format === 'md' || format === 'mdx'
? format
: file.extname && (rest.mdExtensions || md).includes(file.extname)
? 'md'
: 'mdx',
...rest
}
}
}
/**
* @param {VFileCompatible | null | undefined} [value]
* @returns {value is VFile}
*/
function looksLikeAVFile(value) {
return Boolean(
value &&
typeof value === 'object' &&
'message' in value &&
'messages' in value
)
}