Skip to content

Commit

Permalink
Refactor tsconfig.jsons, add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 14, 2023
1 parent cbc2822 commit ac01ca2
Show file tree
Hide file tree
Showing 44 changed files with 586 additions and 492 deletions.
20 changes: 2 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
.DS_Store
coverage/
node_modules/
/packages/esbuild/**/*.d.ts
/packages/loader/lib/**/*.d.ts
/packages/loader/test/**/*.d.ts
/packages/mdx/**/*.d.ts
/packages/node-loader/**/*.d.ts
/packages/preact/lib/**/*.d.ts
/packages/preact/test/**/*.d.ts
/packages/preact/index.d.ts
/packages/react/lib/**/*.d.ts
/packages/react/test/**/*.d.ts
/packages/react/index.d.ts
/packages/register/**/*.d.ts
/packages/remark-mdx/test/**/*.d.ts
/packages/remark-mdx/*.d.ts
/packages/rollup/**/*.d.ts
/packages/vue/lib/**/*.d.ts
/packages/vue/test/**/*.d.ts
/packages/vue/index.d.ts
*.d.cts
*.d.ts
/public/
120 changes: 66 additions & 54 deletions docs/_asset/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
* @typedef {import('mdx/types.js').MDXModule} MDXModule
* @typedef {import('react-error-boundary').FallbackProps} FallbackProps
* @typedef {import('unified').PluggableList} PluggableList
* @typedef {import('estree').Program} Program
* @typedef {import('estree').Node} EstreeNode
* @typedef {import('hast').Root} HastRoot
* @typedef {import('hast').Nodes} HastNodes
* @typedef {import('mdast').Root} MdastRoot
* @typedef {import('mdast').Nodes} MdastNodes
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('unist').Node} UnistNode
*/

Expand Down Expand Up @@ -155,9 +164,9 @@ function Playground() {
value
})

if (show === 'esast') recmaPlugins.push([capture, {name: 'esast'}])
if (show === 'hast') rehypePlugins.push([capture, {name: 'hast'}])
if (show === 'mdast') remarkPlugins.push([capture, {name: 'mdast'}])
if (show === 'esast') recmaPlugins.push([captureEsast])
if (show === 'hast') rehypePlugins.push([captureHast])
if (show === 'mdast') remarkPlugins.push([captureMdast])
/** @type {UnistNode | undefined} */
let tree

Expand Down Expand Up @@ -213,24 +222,35 @@ function Playground() {
</pre>
)

/**
* @param {{name: string}} options
*/
function capture(options) {
function captureMdast() {
/**
* @param {UnistNode} node
* @param {MdastRoot} tree
*/
return function (node) {
const clone = structuredClone(node)
return function (tree) {
const clone = structuredClone(tree)
if (!positions) cleanUnistTree(clone)
tree = clone
}
}

if (!positions) {
if (options.name === 'esast') {
cleanEstree(clone)
} else {
cleanUnistTree(clone)
}
}
function captureHast() {
/**
* @param {HastRoot} tree
*/
return function (tree) {
const clone = structuredClone(tree)
if (!positions) cleanUnistTree(clone)
tree = clone
}
}

function captureEsast() {
/**
* @param {Program} tree
*/
return function (tree) {
const clone = structuredClone(tree)
if (!positions) visitEstree(clone, removeFromEstree)
tree = clone
}
}
Expand Down Expand Up @@ -542,57 +562,49 @@ function DisplayError(props) {
}

/**
* @param {UnistNode} node
* @param {HastRoot | MdastRoot} node
*/
function cleanUnistTree(node) {
removePosition(node, {force: true})

visit(node, function (node) {
if (
node.type === 'mdxJsxAttribute' &&
'value' in node &&
node.value &&
typeof node.value === 'object'
) {
// @ts-expect-error: unist.
cleanUnistTree(node.value)
}

if (
'attributes' in node &&
node.attributes &&
Array.isArray(node.attributes)
) {
for (const attr of node.attributes) {
cleanUnistTree(attr)
}
}

if (node.data && node.data.estree) {
// @ts-expect-error: estree.
visitEstree(node.data.estree, removeFromEstree)
}
})
visit(node, cleanUnistNode)
}

/**
* @param {UnistNode} node
* @param {HastNodes | MdastNodes | MdxJsxAttribute | MdxJsxExpressionAttribute | MdxJsxAttributeValueExpression} node
*/
function cleanEstree(node) {
// @ts-expect-error: fine.
visitEstree(node, removeFromEstree)
function cleanUnistNode(node) {
if (
node.type === 'mdxJsxAttribute' &&
'value' in node &&
node.value &&
typeof node.value === 'object'
) {
cleanUnistNode(node.value)
}

if (
'attributes' in node &&
node.attributes &&
Array.isArray(node.attributes)
) {
for (const attr of node.attributes) {
cleanUnistNode(attr)
}
}

if (node.data && 'estree' in node.data && node.data.estree) {
visitEstree(node.data.estree, removeFromEstree)
}
}

/**
* @param {UnistNode} node
* @param {EstreeNode} node
*/
function removeFromEstree(node) {
// @ts-expect-error: untyped.
delete node.loc
// @ts-expect-error: untyped.
// @ts-expect-error: acorn.
delete node.start
// @ts-expect-error: untyped.
// @ts-expect-error: acorn.
delete node.end
// @ts-expect-error: untyped.
delete node.range
}
28 changes: 27 additions & 1 deletion docs/_component/blog.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/**
* @typedef {import('./sort.js').Item} Item
*/

/**
* @typedef EntryProps
* @property {Item} item
*
* @typedef GroupProps
* @property {string | undefined} [className]
* @property {Array<Item>} items
* @property {string | undefined} [sort]
*/

import React from 'react'
// @ts-expect-error: untyped.
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
Expand All @@ -9,6 +23,12 @@ const runtime = {Fragment, jsx, jsxs}

const dateTimeFormat = new Intl.DateTimeFormat('en', {dateStyle: 'long'})

/**
* @param {EntryProps} props
* Props.
* @returns {JSX.Element}
* Element.
*/
export function BlogEntry(props) {
const {item} = props
const {name, data = {}} = item
Expand Down Expand Up @@ -61,7 +81,7 @@ export function BlogEntry(props) {
) : undefined}
<small>Reading time: {timeLabel}</small>
</div>
{meta.published ? (
{meta.published && typeof meta.published === 'object' ? (
<div style={{marginLeft: 'auto', textAlign: 'right'}}>
<small>
Published on{' '}
Expand All @@ -76,6 +96,12 @@ export function BlogEntry(props) {
)
}

/**
* @param {GroupProps} props
* Props.
* @returns {JSX.Element}
* Element.
*/
export function BlogGroup(props) {
const {items, className, sort = 'navSortSelf,meta.title', ...rest} = props
const sorted = sortItems(items, sort)
Expand Down
33 changes: 27 additions & 6 deletions docs/_component/home.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
/**
* @typedef {import('react').ReactNode} ReactNode
* @typedef {Exclude<import('vfile').Data['meta'], undefined>} Meta
* @typedef {import('./sort.js').Item} Item
*/

/**
* @typedef Props
* @property {string} name
* @property {ReactNode} children
* @property {Item} navTree
* @property {Meta} meta
*/

import React from 'react'
import {NavSite, NavSiteSkip} from './nav-site.jsx'
import {FootSite} from './foot-site.jsx'

/**
* @param {Props} props
* Props.
* @returns {JSX.Element}
* Element.
*/
export function Home(props) {
const {name, navTree, children} = props
const {name, meta, navTree, children} = props
/** @type {unknown} */
// @ts-expect-error: to do: type.
const schema = meta.schemaOrg

return (
<div className="page home">
<NavSiteSkip />
<main>
{props.meta.schemaOrg && (
<script type="application/ld+json">
{JSON.stringify(props.meta.schemaOrg)}
</script>
)}
{schema ? (
<script type="application/ld+json">{JSON.stringify(schema)}</script>
) : undefined}
<article>
<div className="content body">{children}</div>
</article>
Expand Down
Loading

1 comment on commit ac01ca2

@vercel
Copy link

@vercel vercel bot commented on ac01ca2 Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mdx – ./

mdx-mdx.vercel.app
mdxjs.com
mdx-git-main-mdx.vercel.app
v2.mdxjs.com

Please sign in to comment.