From 6ccc0031c85605ec4e247540ddfa58b44dab0017 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 15 Oct 2024 15:02:40 +0200 Subject: [PATCH] Remove some unneeded `ts-expect-error`s --- docs/_asset/editor.jsx | 2 - docs/docs/getting-started.mdx | 4 +- packages/mdx/lib/core.js | 7 +++- packages/mdx/lib/plugin/recma-stringify.js | 15 +++---- packages/mdx/lib/types.d.ts | 7 ++++ packages/mdx/lib/util/estree-util-create.js | 3 +- packages/preact/test/index.jsx | 42 ++++++++++---------- packages/remark-mdx/lib/index.js | 6 +-- packages/remark-mdx/package.json | 3 +- website/types.d.ts | 43 +++++++++++++++------ 10 files changed, 79 insertions(+), 53 deletions(-) diff --git a/docs/_asset/editor.jsx b/docs/_asset/editor.jsx index 5b5fe47ea..99bd8d0ad 100644 --- a/docs/_asset/editor.jsx +++ b/docs/_asset/editor.jsx @@ -662,9 +662,7 @@ function cleanUnistNode(node) { */ function removeFromEstree(node) { delete node.loc - // @ts-expect-error: this field is added by acorn. delete node.start - // @ts-expect-error: this field is added by acorn. delete node.end delete node.range } diff --git a/docs/docs/getting-started.mdx b/docs/docs/getting-started.mdx index 3db338c66..e7bb7903e 100644 --- a/docs/docs/getting-started.mdx +++ b/docs/docs/getting-started.mdx @@ -454,7 +454,7 @@ for more info. * @returns {unknown} */ function compiler(tree) { - // @ts-expect-error TS2349: This expression *is* callable, `estreeToBabel` types are wrong. + // @ts-expect-error: TS2349: This expression *is* callable, `estreeToBabel` types are wrong. return estreeToBabel(tree) } } @@ -526,7 +526,7 @@ for more info. * @returns {unknown} */ function compiler(tree) { - // @ts-expect-error TS2349: This expression *is* callable, `estreeToBabel` types are wrong. + // @ts-expect-error: TS2349: This expression *is* callable, `estreeToBabel` types are wrong. return estreeToBabel(tree) } } diff --git a/packages/mdx/lib/core.js b/packages/mdx/lib/core.js index c30abbc03..9af260d5b 100644 --- a/packages/mdx/lib/core.js +++ b/packages/mdx/lib/core.js @@ -5,6 +5,7 @@ * @import {Options as RemarkRehypeOptions} from 'remark-rehype' * @import {SourceMapGenerator} from 'source-map' * @import {PluggableList, Processor} from 'unified' + * @import {Node} from 'unist' */ /** @@ -217,6 +218,8 @@ export function createProcessor(options) { } pipeline + // @ts-expect-error: `Program` is close enough to a `Node`, + // but type inference has trouble with it and bridges. .use(rehypeRecma, settings) .use(recmaDocument, settings) .use(recmaJsxRewrite, settings) @@ -225,8 +228,10 @@ export function createProcessor(options) { pipeline.use(recmaJsxBuild, settings) } + // @ts-expect-error: `Program` is close enough to a `Node`, + // but type inference has trouble with it and bridges. pipeline.use(recmaStringify, settings).use(settings.recmaPlugins || []) - // @ts-expect-error: we added plugins with if-checks, which TS doesn’t get. + // @ts-expect-error: TS doesn’t get the plugins we added with if-statements. return pipeline } diff --git a/packages/mdx/lib/plugin/recma-stringify.js b/packages/mdx/lib/plugin/recma-stringify.js index 0c65f4c95..d520d66f2 100644 --- a/packages/mdx/lib/plugin/recma-stringify.js +++ b/packages/mdx/lib/plugin/recma-stringify.js @@ -1,6 +1,6 @@ /** * @import {Program} from 'estree-jsx' - * @import {Plugin, Processor} from 'unified' + * @import {Processor} from 'unified' * @import {VFile} from 'vfile' * @import {ProcessorOptions} from '../core.js' */ @@ -10,18 +10,15 @@ import {jsx, toJs} from 'estree-util-to-js' /** * Serialize an esast (estree) program to JavaScript. * - * @type {Plugin<[Readonly], Program, string>} + * @this {Processor} + * Processor. + * @param {Readonly} options + * Configuration. */ export function recmaStringify(options) { - // eslint-disable-next-line unicorn/no-this-assignment - const self = - // @ts-expect-error: TS is wrong about `this`. - /** @type {Processor} */ ( - this - ) const {SourceMapGenerator} = options - self.compiler = compiler + this.compiler = compiler /** * @param {Program} tree diff --git a/packages/mdx/lib/types.d.ts b/packages/mdx/lib/types.d.ts index dd633c2c9..5b1435d03 100644 --- a/packages/mdx/lib/types.d.ts +++ b/packages/mdx/lib/types.d.ts @@ -1,3 +1,4 @@ +import type {Program as EstreeProgram} from 'estree' import type {Data as UnistData} from 'unist' interface EsastData extends UnistData { @@ -63,3 +64,9 @@ declare module 'mdast-util-mdx-jsx' { _mdxExplicitJsx?: boolean | null | undefined } } + +declare module 'unified' { + interface CompileResultMap { + EstreeProgram: EstreeProgram + } +} diff --git a/packages/mdx/lib/util/estree-util-create.js b/packages/mdx/lib/util/estree-util-create.js index aa2c39218..7ea0016e0 100644 --- a/packages/mdx/lib/util/estree-util-create.js +++ b/packages/mdx/lib/util/estree-util-create.js @@ -15,8 +15,7 @@ */ export function create(from, to) { /** @type {Array} */ - // @ts-expect-error: `start`, `end`, `comments` are custom Acorn fields. - const fields = ['start', 'end', 'loc', 'range', 'comments'] + const fields = ['start', 'end', 'loc', 'range'] let index = -1 while (++index < fields.length) { diff --git a/packages/preact/test/index.jsx b/packages/preact/test/index.jsx index c7e7760e4..cdb305975 100644 --- a/packages/preact/test/index.jsx +++ b/packages/preact/test/index.jsx @@ -37,9 +37,9 @@ test('@mdx-js/preact', async function (t) { render( } }} @@ -68,7 +68,7 @@ test('@mdx-js/preact', async function (t) { * @returns * Element. */ - // @ts-expect-error TODO configure TypeScript project references + // @ts-expect-error: TODO configure TypeScript project references wrapper(properties) { return
} @@ -93,23 +93,23 @@ test('@mdx-js/preact', async function (t) { render( }, - // @ts-expect-error TODO configure TypeScript project references + // @ts-expect-error: TODO configure TypeScript project references h2(properties) { - // @ts-expect-error TODO configure TypeScript project references + // @ts-expect-error: TODO configure TypeScript project references return

} }} > } }} @@ -133,24 +133,24 @@ test('@mdx-js/preact', async function (t) { render( }, - // @ts-expect-error TODO configure TypeScript project references + // @ts-expect-error: TODO configure TypeScript project references h2(properties) { - // @ts-expect-error TODO configure TypeScript project references + // @ts-expect-error: TODO configure TypeScript project references return

} }} > } } @@ -176,9 +176,9 @@ test('@mdx-js/preact', async function (t) { render( } }} @@ -205,20 +205,20 @@ test('@mdx-js/preact', async function (t) { render( } }} > } } diff --git a/packages/remark-mdx/lib/index.js b/packages/remark-mdx/lib/index.js index 6121ef55c..d44e92db3 100644 --- a/packages/remark-mdx/lib/index.js +++ b/packages/remark-mdx/lib/index.js @@ -19,15 +19,15 @@ const emptyOptions = {} * Add support for MDX (JSX: `