Skip to content

Commit

Permalink
Remove some unneeded ts-expect-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 15, 2024
1 parent 6819e55 commit 6ccc003
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 53 deletions.
2 changes: 0 additions & 2 deletions docs/_asset/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions docs/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/mdx/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
*/

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
15 changes: 6 additions & 9 deletions packages/mdx/lib/plugin/recma-stringify.js
Original file line number Diff line number Diff line change
@@ -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'
*/
Expand All @@ -10,18 +10,15 @@ import {jsx, toJs} from 'estree-util-to-js'
/**
* Serialize an esast (estree) program to JavaScript.
*
* @type {Plugin<[Readonly<ProcessorOptions>], Program, string>}
* @this {Processor<undefined, undefined, undefined, Program, string>}
* Processor.
* @param {Readonly<ProcessorOptions>} 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<undefined, undefined, undefined, Program, string>} */ (
this
)
const {SourceMapGenerator} = options

self.compiler = compiler
this.compiler = compiler

/**
* @param {Program} tree
Expand Down
7 changes: 7 additions & 0 deletions packages/mdx/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {Program as EstreeProgram} from 'estree'
import type {Data as UnistData} from 'unist'

interface EsastData extends UnistData {
Expand Down Expand Up @@ -63,3 +64,9 @@ declare module 'mdast-util-mdx-jsx' {
_mdxExplicitJsx?: boolean | null | undefined
}
}

declare module 'unified' {
interface CompileResultMap {
EstreeProgram: EstreeProgram
}
}
3 changes: 1 addition & 2 deletions packages/mdx/lib/util/estree-util-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
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']
const fields = ['start', 'end', 'loc', 'range']
let index = -1

while (++index < fields.length) {
Expand Down
42 changes: 21 additions & 21 deletions packages/preact/test/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ test('@mdx-js/preact', async function (t) {
render(
<MDXProvider
components={{
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
h1(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h1 style={{color: 'tomato'}} {...properties} />
}
}}
Expand Down Expand Up @@ -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 <div id="layout" {...properties} />
}
Expand All @@ -93,23 +93,23 @@ test('@mdx-js/preact', async function (t) {
render(
<MDXProvider
components={{
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
h1(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h1 style={{color: 'tomato'}} {...properties} />
},
// @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 <h2 style={{color: 'rebeccapurple'}} {...properties} />
}
}}
>
<MDXProvider
components={{
// @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 <h2 style={{color: 'papayawhip'}} {...properties} />
}
}}
Expand All @@ -133,24 +133,24 @@ test('@mdx-js/preact', async function (t) {
render(
<MDXProvider
components={{
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
h1(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h1 style={{color: 'tomato'}} {...properties} />
},
// @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 <h2 style={{color: 'rebeccapurple'}} {...properties} />
}
}}
>
<MDXProvider
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
components={function () {
return {
h2(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h2 style={{color: 'papayawhip'}} {...properties} />
}
}
Expand All @@ -176,9 +176,9 @@ test('@mdx-js/preact', async function (t) {
render(
<MDXProvider
components={{
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
h1(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h1 style={{color: 'tomato'}} {...properties} />
}
}}
Expand All @@ -205,20 +205,20 @@ test('@mdx-js/preact', async function (t) {
render(
<MDXProvider
components={{
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
h1(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h1 style={{color: 'tomato'}} {...properties} />
}
}}
>
<MDXProvider
disableParentContext
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
components={function () {
return {
h2(properties) {
// @ts-expect-error TODO configure TypeScript project references
// @ts-expect-error: TODO configure TypeScript project references
return <h2 style={{color: 'papayawhip'}} {...properties} />
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/remark-mdx/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const emptyOptions = {}
* Add support for MDX (JSX: `<Video id={123} />`, export/imports: `export {x}
* from 'y'`; and expressions: `{1 + 1}`).
*
* @this {Processor}
* Processor.
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function remarkMdx(options) {
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
const self = this
const settings = options || emptyOptions
const data = self.data()

Expand Down
3 changes: 2 additions & 1 deletion packages/remark-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"prettier": true,
"rules": {
"logical-assignment-operators": "off",
"n/file-extension-in-import": "off"
"n/file-extension-in-import": "off",
"unicorn/no-this-assignment": "off"
}
}
}
43 changes: 31 additions & 12 deletions website/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import type {Author} from './generate.js'

// Register data on `estree`.
declare module 'estree' {
interface BaseNode {
/**
* Field patched by `acorn`.
*
* Registered by types for the MDX website for the playground.
*/
end?: number | undefined

/**
* Field patched by `acorn`.
*
* Registered by types for the MDX website for the playground.
*/
start?: number | undefined
}
}

// Register data on hast.
declare module 'hast' {
interface ElementData {
/**
* `meta` field available on `<code>` elements; added by `mdast-util-to-hast`.
*
* Registered by `website/types.d.ts` for the MDX website.
*/
meta?: string | null | undefined
}
}

// Add custom data supported when `rehype-document` is added.
declare module 'vfile' {
interface DataMapMeta {
Expand All @@ -22,15 +53,3 @@ declare module 'vfile' {
meta: DataMapMeta
}
}

// Register data on hast.
declare module 'hast' {
interface ElementData {
/**
* `meta` field available on `<code>` elements; added by `mdast-util-to-hast`.
*
* Registered by `website/types.d.ts` for the MDX website.
*/
meta?: string | null | undefined
}
}

0 comments on commit 6ccc003

Please sign in to comment.