From e525db9b06686a7a79483180d9220f242086f2a5 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 21 Oct 2023 18:28:23 +0200 Subject: [PATCH] Refactor some errors --- packages/mdx/lib/core.js | 66 ++++++++++++------------------------ packages/mdx/test/compile.js | 4 +-- 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/packages/mdx/lib/core.js b/packages/mdx/lib/core.js index 6b231feac..eaec0dec1 100644 --- a/packages/mdx/lib/core.js +++ b/packages/mdx/lib/core.js @@ -125,6 +125,7 @@ * you should probably set `baseUrl` too. */ +import {unreachable} from 'devlop' import remarkMdx from 'remark-mdx' import remarkParse from 'remark-parse' import remarkRehype from 'remark-rehype' @@ -158,85 +159,60 @@ const removedOptions = [ * Processor. */ export function createProcessor(options) { - const { - SourceMapGenerator, - development, - elementAttributeNameCase, - jsx, - format, - outputFormat, - providerImportSource, - recmaPlugins, - rehypePlugins, - remarkPlugins, - remarkRehypeOptions, - stylePropertyNameCase, - tableCellAlignToStyle, - ...rest - } = options || {} + const settings = options || {} let index = -1 while (++index < removedOptions.length) { const key = removedOptions[index] - if (options && key in options) { - throw new Error( - '`options.' + + if (key in settings) { + unreachable( + 'Unexpected removed option `' + key + - '` is no longer supported. Please see for more information' + '`; see on how to migrate' ) } } // @ts-expect-error: throw an error for a runtime value which is not allowed // by the types. - if (format === 'detect') { - throw new Error( - "Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format" + if (settings.format === 'detect') { + unreachable( + "Unexpected `format: 'detect'`, which is not supported by `createProcessor`, expected `'mdx'` or `'md'`" ) } const pipeline = unified().use(remarkParse) - if (format !== 'md') { + if (settings.format !== 'md') { pipeline.use(remarkMdx) } - const extraNodeTypes = remarkRehypeOptions - ? remarkRehypeOptions.passThrough || [] - : [] + const remarkRehypeOptions = settings.remarkRehypeOptions || {} pipeline .use(remarkMarkAndUnravel) - .use(remarkPlugins || []) + .use(settings.remarkPlugins || []) .use(remarkRehype, { ...remarkRehypeOptions, allowDangerousHtml: true, - passThrough: [...extraNodeTypes, ...nodeTypes] + passThrough: [...(remarkRehypeOptions.passThrough || []), ...nodeTypes] }) - .use(rehypePlugins || []) + .use(settings.rehypePlugins || []) - if (format === 'md') { + if (settings.format === 'md') { pipeline.use(rehypeRemoveRaw) } pipeline - .use(rehypeRecma, { - elementAttributeNameCase, - stylePropertyNameCase, - tableCellAlignToStyle - }) - .use(recmaDocument, {...rest, outputFormat}) - .use(recmaJsxRewrite, { - development, - providerImportSource, - outputFormat - }) + .use(rehypeRecma, settings) + .use(recmaDocument, settings) + .use(recmaJsxRewrite, settings) - if (!jsx) { - pipeline.use(recmaJsxBuild, {development, outputFormat}) + if (!settings.jsx) { + pipeline.use(recmaJsxBuild, settings) } - pipeline.use(recmaStringify, {SourceMapGenerator}).use(recmaPlugins || []) + pipeline.use(recmaStringify, settings).use(settings.recmaPlugins || []) // @ts-expect-error: we added plugins with if-checks, which TS doesn’t get. return pipeline diff --git a/packages/mdx/test/compile.js b/packages/mdx/test/compile.js index bf2bb606c..e87a7185f 100644 --- a/packages/mdx/test/compile.js +++ b/packages/mdx/test/compile.js @@ -25,7 +25,7 @@ test('@mdx-js/mdx: compile', async function (t) { assert.throws(function () { // @ts-expect-error: check how the runtime handles a removed option. compile('# hi!', {filepath: 'example.mdx'}) - }, /`options.filepath` is no longer supported/) + }, /Unexpected removed option `filepath`/) }) await t.test('should compile', async function () { @@ -1396,6 +1396,6 @@ test('@mdx-js/mdx: createProcessor', async function (t) { assert.throws(function () { // @ts-expect-error: check how runtime handles an incorrect `format: 'detect'`. createProcessor({format: 'detect'}) - }, new Error("Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format")) + }, /Unexpected `format: 'detect'`/) }) })