Skip to content

Commit

Permalink
Add more types, fix small bugs
Browse files Browse the repository at this point in the history
* Update `unist-util-visit`
* Add a bunch of types
* Refactor all types
* Add some tests for class components, more exports
* Fix bug when exporting object/array/assignments patterns and rest
  elements
  • Loading branch information
wooorm committed Apr 22, 2021
1 parent 4713bd5 commit 49f3a92
Show file tree
Hide file tree
Showing 36 changed files with 926 additions and 489 deletions.
12 changes: 6 additions & 6 deletions lib/compile.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {createProcessor} from './core.js'
import {resolveFileAndOptions} from './util/resolve-file-and-options.js'

/**
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('vfile').VFile} VFile
* @typedef {import('./core').PluginOptions} PluginOptions
* @typedef {import('./core').BaseProcessorOptions} BaseProcessorOptions
* @typedef {import('./core.js').PluginOptions} PluginOptions
* @typedef {import('./core.js').BaseProcessorOptions} BaseProcessorOptions
* @typedef {Omit<BaseProcessorOptions, 'format'>} CoreProcessorOptions
*
* @typedef ExtraOptions
* @property {'detect' | 'mdx' | 'md'} [format='detect'] Format of `file`
* @property {'detect'|'mdx'|'md'} [format='detect'] Format of `file`
*
* @typedef {CoreProcessorOptions & PluginOptions & ExtraOptions} CompileOptions
*/

import {createProcessor} from './core.js'
import {resolveFileAndOptions} from './util/resolve-file-and-options.js'

/**
* Compile MDX to JS.
*
Expand Down
41 changes: 20 additions & 21 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import unified from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {recmaJsxBuild} from './plugin/recma-jsx-build.js'
import {recmaDocument} from './plugin/recma-document.js'
import {recmaJsxRewrite} from './plugin/recma-jsx-rewrite.js'
import {recmaStringify} from './plugin/recma-stringify.js'
import {rehypeRecma} from './plugin/rehype-recma.js'
import {rehypeRemoveRaw} from './plugin/rehype-remove-raw.js'
import {remarkMarkAndUnravel} from './plugin/remark-mark-and-unravel.js'
import {remarkMdx} from './plugin/remark-mdx.js'
import {nodeTypes} from './node-types.js'

/**
* @typedef {import('unified').Processor} Processor
* @typedef {import('unified').PluggableList} PluggableList
* @typedef {import('./plugin/recma-document').RecmaDocumentOptions} RecmaDocumentOptions
* @typedef {import('./plugin/recma-stringify').RecmaStringifyOptions} RecmaStringifyOptions
* @typedef {import('./plugin/recma-jsx-rewrite').RecmaJsxRewriteOptions} RecmaJsxRewriteOptions
* @typedef {import('./plugin/recma-document.js').RecmaDocumentOptions} RecmaDocumentOptions
* @typedef {import('./plugin/recma-stringify.js').RecmaStringifyOptions} RecmaStringifyOptions
* @typedef {import('./plugin/recma-jsx-rewrite.js').RecmaJsxRewriteOptions} RecmaJsxRewriteOptions
*
* @typedef BaseProcessorOptions
* @property {boolean} [jsx=false] Whether to keep JSX
* @property {'mdx' | 'md'} [format='mdx'] Format of the files to be processed
* @property {'program' | 'function-body'} [outputFormat='program'] Whether to compile to a whole program or a function body.
* @property {'mdx'|'md'} [format='mdx'] Format of the files to be processed
* @property {'program'|'function-body'} [outputFormat='program'] Whether to compile to a whole program or a function body.
* @property {string[]} [mdExtensions] Extensions (with `.`) for markdown
* @property {string[]} [mdxExtensions] Extensions (with `.`) for MDX
* @property {PluggableList} [recmaPlugins] List of recma (esast, JavaScript) plugins
Expand All @@ -32,6 +19,19 @@ import {nodeTypes} from './node-types.js'
* @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions
*/

import unified from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {recmaJsxBuild} from './plugin/recma-jsx-build.js'
import {recmaDocument} from './plugin/recma-document.js'
import {recmaJsxRewrite} from './plugin/recma-jsx-rewrite.js'
import {recmaStringify} from './plugin/recma-stringify.js'
import {rehypeRecma} from './plugin/rehype-recma.js'
import {rehypeRemoveRaw} from './plugin/rehype-remove-raw.js'
import {remarkMarkAndUnravel} from './plugin/remark-mark-and-unravel.js'
import {remarkMdx} from './plugin/remark-mdx.js'
import {nodeTypes} from './node-types.js'

/**
* Pipeline to:
*
Expand All @@ -55,7 +55,7 @@ export function createProcessor(options = {}) {
...rest
} = options

// @ts-ignore Sure the types prohibit it but what if someone does it anyway?
// @ts-ignore runtime.
if (format === 'detect') {
throw new Error(
"Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format"
Expand All @@ -71,11 +71,10 @@ export function createProcessor(options = {}) {
.use(remarkRehype, {allowDangerousHtml: true, passThrough: nodeTypes})
.use(rehypePlugins)
.use(format === 'md' ? rehypeRemoveRaw : undefined)
// @ts-ignore this switches from rehype to recma
.use(rehypeRecma)
.use(recmaDocument, {...rest, outputFormat})
// @ts-ignore recma transformer uses an esast node rather than a unist node
.use(recmaJsxRewrite, {providerImportSource, outputFormat})
// @ts-ignore recma transformer uses an esast node rather than a unist node
.use(jsx ? undefined : recmaJsxBuild, {outputFormat})
// @ts-ignore recma compiler is seen as a transformer
.use(recmaStringify, {SourceMapGenerator})
Expand Down
8 changes: 4 additions & 4 deletions lib/evaluate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {compile, compileSync} from './compile.js'
import {run, runSync} from './run.js'
import {resolveEvaluateOptions} from './util/resolve-evaluate-options.js'

/**
* @typedef {import('vfile').VFileCompatible} VFileCompatible
* @typedef {import('./util/resolve-evaluate-options.js').EvaluateOptions} EvaluateOptions
Expand All @@ -12,6 +8,10 @@ import {resolveEvaluateOptions} from './util/resolve-evaluate-options.js'
* @typedef {{[exports: string]: unknown, default: MDXContent}} ExportMap
*/

import {compile, compileSync} from './compile.js'
import {run, runSync} from './run.js'
import {resolveEvaluateOptions} from './util/resolve-evaluate-options.js'

/**
* Evaluate MDX.
*
Expand Down
33 changes: 22 additions & 11 deletions lib/integration/esbuild.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* @typedef {import('esbuild').Plugin} Plugin
* @typedef {import('esbuild').PluginBuild} PluginBuild
* @typedef {import('esbuild').OnLoadArgs} OnLoadArgs
* @typedef {import('esbuild').Message} Message
* @typedef {import('vfile').VFileContents} VFileContents
* @typedef {import('vfile-message').VFileMessage} VFileMessage
* @typedef {import('unist').Point} Point
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*/

import vfile from 'vfile'
import {promises as fs} from 'fs'
import {createFormatAwareProcessors} from '../util/create-format-aware-processors.js'
Expand All @@ -8,40 +19,40 @@ var eol = /\r\n|\r|\n|\u2028|\u2029/g
/**
* Compile MDX w/ esbuild.
*
* @param {import('../core.js').ProcessorOptions} [options]
* @return {import('esbuild').Plugin}
* @param {ProcessorOptions} [options]
* @return {Plugin}
*/
export function esbuild(options) {
var {extnames, process} = createFormatAwareProcessors(options)

return {name: 'esbuild-xdm', setup}

/**
* @param {import('esbuild').PluginBuild} build
* @param {PluginBuild} build
*/
function setup(build) {
build.onLoad({filter: extnamesToRegex(extnames)}, onload)
}

/**
* @param {import('esbuild').OnLoadArgs} data
* @param {OnLoadArgs} data
*/
async function onload(data) {
var doc = String(await fs.readFile(data.path))
var file = vfile({contents: doc, path: data.path})
/** @type {import('vfile-message').VFileMessage[]} */
/** @type {VFileMessage[]} */
var messages = []
/** @type {import('esbuild').Message[]} */
/** @type {Message[]} */
var errors = []
/** @type {import('esbuild').Message[]} */
/** @type {Message[]} */
var warnings = []
/** @type {import('vfile').VFileContents} */
/** @type {VFileContents} */
var contents
/** @type {import('vfile-message').VFileMessage} */
/** @type {VFileMessage} */
var message
/** @type {import('unist').Point} */
/** @type {Point} */
var start
/** @type {import('unist').Point} */
/** @type {Point} */
var end
/** @type {number} */
var length
Expand Down
9 changes: 7 additions & 2 deletions lib/integration/node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('../compile.js').CompileOptions} CompileOptions
*/

import path from 'path'
import vfile from 'vfile'
import {createFormatAwareProcessors} from '../util/create-format-aware-processors.js'

/**
* Create smart processors to handle different formats.
*
* @param {import('../compile').CompileOptions} [options]
* @param {CompileOptions} [options]
*/
export function createLoader(options) {
var {extnames, process} = createFormatAwareProcessors(options)
Expand All @@ -29,7 +34,7 @@ export function createLoader(options) {
* @param {Function} defaultTransformSource
*/
async function transformSource(contents, context, defaultTransformSource) {
/** @type {import('vfile').VFile} */
/** @type {VFile} */
var file

if (!extnames.includes(path.extname(context.url))) {
Expand Down
20 changes: 12 additions & 8 deletions lib/integration/rollup.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import vfile from 'vfile'
import {createFilter} from '@rollup/pluginutils'
import {createFormatAwareProcessors} from '../util/create-format-aware-processors.js'

/**
* @typedef {import('@rollup/pluginutils').FilterPattern} FilterPattern
* @typedef {import('rollup').Plugin} Plugin
* @typedef {import('../compile.js').CompileOptions} CompileOptions
*
* @typedef RollupPluginOptions
* @property {import('@rollup/pluginutils').FilterPattern} [include] List of picomatch patterns to include
* @property {import('@rollup/pluginutils').FilterPattern} [exclude] List of picomatch patterns to exclude
* @property {FilterPattern} [include] List of picomatch patterns to include
* @property {FilterPattern} [exclude] List of picomatch patterns to exclude
*
* @typedef {import('../compile').CompileOptions & RollupPluginOptions} ProcessorAndRollupOptions
* @typedef {CompileOptions & RollupPluginOptions} ProcessorAndRollupOptions
*/

import vfile from 'vfile'
import {createFilter} from '@rollup/pluginutils'
import {createFormatAwareProcessors} from '../util/create-format-aware-processors.js'

/**
* Compile MDX w/ rollup.
*
* @param {ProcessorAndRollupOptions} [options]
* @return {import('rollup').Plugin}
* @return {Plugin}
*/
export function rollup(options = {}) {
var {include, exclude, ...rest} = options
Expand Down
8 changes: 6 additions & 2 deletions lib/integration/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import {compile} from '../compile.js'
* `file.map` is defined when a `SourceMapGenerator` is passed in options.
*
* @param {string} value
* @param {function(Error): void | function(null|undefined, string|Buffer, Object?): void} callback
* @param {(error: Error|null|undefined, content?: string|Buffer, map?: Object) => void} callback
*/
export function loader(value, callback) {
compile(
// TS through JSDoc can’t handle `this`
// type-coverage:ignore-next-line
{contents: value, path: this.resourcePath},
// TS through JSDoc can’t handle `this`
// type-coverage:ignore-next-line
{...getOptions(this)}
).then((file) => {
// @ts-ignore conflict between UInt8Array and Buffer is expected, and a tradeoff made in vFile typings to avoid @types/node being required
// @ts-ignore conflict between UInt8Array and Buffer is expected, and a tradeoff made in vfile typings to avoid `@types/node` being required
callback(null, file.contents, file.map)
return file
}, callback)
Expand Down
Loading

0 comments on commit 49f3a92

Please sign in to comment.