Skip to content

Commit

Permalink
Use TypeScript @import JSDoc tags
Browse files Browse the repository at this point in the history
Previously we used `@typedef` tags to simulate type imports. The problem
is that are not really imports. They define and export a new type. We
now use `@import` introduced by TypeScript 5.5 to actually import types.

The pattern using `@typedef` is still used in index files to export
types.

This change also replaces the type casting inside MDX files with
definitions of the `Props` types.

This also replaces usage of the global `JSX` namespace with proper
types. For TypeScript<5.1 the correct return type of React components is
`ReactElement`, for Preact `VNode`. For TypeScript>=5.1, the correct
return type for React components is `ReactNode`, for Preact
`ComponentChildren`.
  • Loading branch information
remcohaszing committed Jun 25, 2024
1 parent 8ccbba8 commit 429d15f
Show file tree
Hide file tree
Showing 63 changed files with 357 additions and 319 deletions.
38 changes: 19 additions & 19 deletions docs/_asset/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
/* @jsxImportSource react */

/**
* @typedef {import('@wooorm/starry-night').Grammar} Grammar
* @typedef {import('estree').Node} EstreeNode
* @typedef {import('estree').Program} Program
* @typedef {import('hast').Nodes} HastNodes
* @typedef {import('hast').Root} HastRoot
* @typedef {import('mdast').Nodes} MdastNodes
* @typedef {import('mdast').Root} MdastRoot
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute
* @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
* @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute
* @typedef {import('mdx/types.js').MDXModule} MDXModule
* @typedef {import('react-error-boundary').FallbackProps} FallbackProperties
* @typedef {import('unified').PluggableList} PluggableList
* @typedef {import('unist').Node} UnistNode
* @import {Grammar} from '@wooorm/starry-night'
* @import {Node as EstreeNode, Program} from 'estree'
* @import {Nodes as HastNodes, Root as HastRoot} from 'hast'
* @import {Nodes as MdastNodes, Root as MdastRoot} from 'mdast'
* @import {
MdxJsxAttribute,
MdxJsxAttributeValueExpression,
MdxJsxExpressionAttribute
* } from 'mdast-util-mdx-jsx'
* @import {MDXModule} from 'mdx/types.js'
* @import {ReactNode} from 'react'
* @import {FallbackProps} from 'react-error-boundary'
* @import {PluggableList} from 'unified'
* @import {Node as UnistNode} from 'unist'
*/

/**
Expand All @@ -35,7 +35,7 @@
* OK.
* @property {true} ok
* Whether OK.
* @property {JSX.Element} value
* @property {ReactNode} value
* Result.
*
* @typedef {EvalNok | EvalOk} EvalResult
Expand Down Expand Up @@ -315,7 +315,7 @@ function Playground() {
const scope = formatMarkdown ? 'text.md' : 'source.mdx'
// Cast to actual value.
const compiledResult = /** @type {EvalResult | undefined} */ (evalResult)
/** @type {JSX.Element | undefined} */
/** @type {ReactNode | undefined} */
let display

if (compiledResult) {
Expand Down Expand Up @@ -579,9 +579,9 @@ function Playground() {

/**
*
* @param {Readonly<FallbackProperties>} properties
* @param {Readonly<FallbackProps>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
function ErrorFallback(properties) {
Expand All @@ -601,7 +601,7 @@ function ErrorFallback(properties) {
/**
* @param {DisplayProperties} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
function DisplayError(properties) {
Expand Down
7 changes: 4 additions & 3 deletions docs/_component/blog.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('./sort.js').Item} Item
* @import {ReactNode} from 'react'
* @import {Item} from './sort.js'
*/

/**
Expand Down Expand Up @@ -29,7 +30,7 @@ const dateTimeFormat = new Intl.DateTimeFormat('en', {dateStyle: 'long'})
/**
* @param {Readonly<EntryProperties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function BlogEntry(properties) {
Expand Down Expand Up @@ -106,7 +107,7 @@ export function BlogEntry(properties) {
/**
* @param {Readonly<GroupProperties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function BlogGroup(properties) {
Expand Down
10 changes: 5 additions & 5 deletions docs/_component/home.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @typedef {import('react').ReactNode} ReactNode
* @typedef {import('vfile').Data['meta']} DataMeta
* @typedef {import('./sort.js').Item} Item
* @import {ReactNode} from 'react'
* @import {Data} from 'vfile'
* @import {Item} from './sort.js'
*/

/**
* @typedef {Exclude<DataMeta, undefined>} Meta
* @typedef {Exclude<Data['meta'], undefined>} Meta
*
* @typedef Properties
* Properties.
Expand All @@ -26,7 +26,7 @@ import {NavigationSite, NavigationSiteSkip} from './nav-site.jsx'
/**
* @param {Readonly<Properties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function Home(properties) {
Expand Down
11 changes: 6 additions & 5 deletions docs/_component/layout.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @typedef {import('vfile').Data['meta']} DataMeta
* @typedef {import('./sort.js').Item} Item
* @import {ReactNode} from 'react'
* @import {Data} from 'vfile'
* @import {Item} from './sort.js'
*/

/**
Expand All @@ -10,11 +11,11 @@
* Name.
* @property {Readonly<URL>} ghUrl
* GitHub URL.
* @property {Readonly<DataMeta> | undefined} [meta]
* @property {Readonly<Data['meta']> | undefined} [meta]
* Meta.
* @property {Readonly<Item>} navigationTree
* Navigation tree.
* @property {JSX.Element} children
* @property {ReactNode} children
* Children.
*/

Expand All @@ -28,7 +29,7 @@ const dateTimeFormat = new Intl.DateTimeFormat('en', {dateStyle: 'long'})
/**
* @param {Readonly<Properties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function Layout(properties) {
Expand Down
5 changes: 3 additions & 2 deletions docs/_component/nav-site.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('./sort.js').Item} Item
* @import {ReactNode} from 'react'
* @import {Item} from './sort.js'
*/

/**
Expand Down Expand Up @@ -34,7 +35,7 @@ export function NavigationSiteSkip() {
/**
* @param {Readonly<Properties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function NavigationSite(properties) {
Expand Down
10 changes: 5 additions & 5 deletions docs/_component/nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/// <reference types="rehype-infer-description-meta" />

/**
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('react').ReactNode} ReactNode
* @typedef {import('./sort.js').Item} Item
* @import {ElementContent} from 'hast'
* @import {ReactNode} from 'react'
* @import {Item} from './sort.js'
*/

/**
Expand Down Expand Up @@ -46,7 +46,7 @@ const dateTimeFormat = new Intl.DateTimeFormat('en', {dateStyle: 'long'})
/**
* @param {Readonly<GroupProperties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function NavigationGroup(properties) {
Expand All @@ -69,7 +69,7 @@ export function NavigationGroup(properties) {
/**
* @param {Readonly<ItemProperties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function NavigationItem(properties) {
Expand Down
4 changes: 2 additions & 2 deletions docs/_component/note.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('react').ReactNode} ReactNode
* @import {ReactNode} from 'react'
*/

/**
Expand All @@ -22,7 +22,7 @@ const known = new Set(['info', 'legacy', 'important'])
/**
* @param {Readonly<Properties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function Note(properties) {
Expand Down
6 changes: 5 additions & 1 deletion docs/_component/snowfall.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @import {ReactNode} from 'react'
*/

/**
* @typedef Properties
* Properties.
Expand All @@ -14,7 +18,7 @@ const data = [6, 5, 2, 4.5, 1.5, 2.5, 2, 2.5, 1.5, 2.5, 3.5, 7]
/**
* @param {Readonly<Properties>} properties
* Properties.
* @returns {JSX.Element}
* @returns {ReactNode}
* Element.
*/
export function Chart(properties) {
Expand Down
2 changes: 1 addition & 1 deletion docs/_component/sort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('vfile').Data} Data
* @import {Data} from 'vfile'
*/

/**
Expand Down
14 changes: 9 additions & 5 deletions docs/blog/index.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{/**
* @import {Item} from '../_component/sort.js'
*/}

{/**
* @typedef Props
* @property {Item} navigationTree
*/}

import assert from 'node:assert/strict'
import {BlogGroup} from '../_component/blog.jsx'

Expand All @@ -16,11 +25,6 @@ The latest news about MDX.

{
(function () {
/**
* @typedef {import('../_component/sort.js').Item} Item
*/

/** @type {Item} */
const navigationTree = props.navigationTree
const category = navigationTree.children.find(function (item) {
return item.name === '/blog/'
Expand Down
14 changes: 9 additions & 5 deletions docs/community/index.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{/**
* @import {Item} from '../_component/sort.js'
*/}

{/**
* @typedef Props
* @property {Item} navigationTree
*/}

import assert from 'node:assert/strict'
import {NavigationGroup} from '../_component/nav.jsx'

Expand All @@ -15,11 +24,6 @@ and some background information.

{
(function () {
/**
* @typedef {import('../_component/sort.js').Item} Item
*/

/** @type {Item} */
const navigationTree = props.navigationTree
const category = navigationTree.children.find(function (item) {
return item.name === '/community/'
Expand Down
14 changes: 9 additions & 5 deletions docs/docs/index.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{/**
* @import {Item} from '../_component/sort.js'
*/}

{/**
* @typedef Props
* @property {Item} navigationTree
*/}

import assert from 'node:assert/strict'
import {NavigationGroup} from '../_component/nav.jsx'

Expand All @@ -17,11 +26,6 @@ Reading through these should give you a good understanding of MDX.

{
(function () {
/**
* @typedef {import('../_component/sort.js').Item} Item
*/

/** @type {Item} */
const navigationTree = props.navigationTree
const category = navigationTree.children.find(function (item) {
return item.name === '/docs/'
Expand Down
14 changes: 9 additions & 5 deletions docs/guides/index.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{/**
* @import {Item} from '../_component/sort.js'
*/}

{/**
* @typedef Props
* @property {Item} navigationTree
*/}

import assert from 'node:assert/strict'
import {NavigationGroup} from '../_component/nav.jsx'

Expand All @@ -15,11 +24,6 @@ around MDX.

{
(function () {
/**
* @typedef {import('../_component/sort.js').Item} Item
*/

/** @type {Item} */
const navigationTree = props.navigationTree
const category = navigationTree.children.find(function (item) {
return item.name === '/guides/'
Expand Down
14 changes: 9 additions & 5 deletions docs/packages/index.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{/**
* @import {Item} from '../_component/sort.js'
*/}

{/**
* @typedef Props
* @property {Item} navigationTree
*/}

import assert from 'node:assert/strict'
import {NavigationGroup} from '../_component/nav.jsx'

Expand All @@ -17,11 +26,6 @@ bundlers and frontend frameworks.

{
(function () {
/**
* @typedef {import('../_component/sort.js').Item} Item
*/

/** @type {Item} */
const navigationTree = props.navigationTree
const category = navigationTree.children.find(function (item) {
return item.name === '/packages/'
Expand Down
16 changes: 8 additions & 8 deletions packages/esbuild/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @typedef {import('@mdx-js/mdx').CompileOptions} CompileOptions
* @typedef {import('esbuild').Message} Message
* @typedef {import('esbuild').OnLoadArgs} OnLoadArgs
* @typedef {import('esbuild').OnLoadResult} OnLoadResult
* @typedef {import('esbuild').OnResolveArgs} OnResolveArgs
* @typedef {import('esbuild').Plugin} Plugin
* @typedef {import('esbuild').PluginBuild} PluginBuild
* @typedef {import('vfile').Value} Value
* @import {CompileOptions} from '@mdx-js/mdx'
* @import {
Message,
OnLoadArgs,
OnLoadResult,
Plugin,
PluginBuild
* } from 'esbuild'
*/

/**
Expand Down
Loading

0 comments on commit 429d15f

Please sign in to comment.