Skip to content

Commit 5af6bc7

Browse files
Fix to correctly compile intrinsic types
Co-authored-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Remco Haszing <remcohaszing@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com> Related to GH-622. Closes GH-638.
1 parent 02bac83 commit 5af6bc7

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

lib/ast-to-react.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
2-
* @typedef {JSX.IntrinsicElements} IntrinsicElements
32
* @typedef {import('react').ReactNode} ReactNode
43
* @typedef {import('unist').Position} Position
54
* @typedef {import('hast').Element} Element
5+
* @typedef {import('hast').ElementContent} ElementContent
66
* @typedef {import('hast').Root} Root
77
* @typedef {import('hast').Text} Text
88
* @typedef {import('hast').Comment} Comment
99
* @typedef {import('hast').DocType} Doctype
1010
* @typedef {import('property-information').Info} Info
1111
* @typedef {import('property-information').Schema} Schema
12+
* @typedef {import('./complex-types').ReactMarkdownProps} ReactMarkdownProps
1213
*
1314
* @typedef Raw
1415
* @property {'raw'} type
@@ -21,7 +22,7 @@
2122
*
2223
* @callback TransformLink
2324
* @param {string} href
24-
* @param {Array.<Comment|Element|Text>} children
25+
* @param {Array.<ElementContent>} children
2526
* @param {string?} title
2627
* @returns {string}
2728
*
@@ -33,22 +34,14 @@
3334
*
3435
* @callback TransformLinkTarget
3536
* @param {string} href
36-
* @param {Array.<Comment|Element|Text>} children
37+
* @param {Array.<ElementContent>} children
3738
* @param {string?} title
3839
* @returns {string|undefined}
3940
*
40-
* @typedef {keyof IntrinsicElements} ReactMarkdownNames
41+
* @typedef {keyof JSX.IntrinsicElements} ReactMarkdownNames
4142
*
4243
* To do: is `data-sourcepos` typeable?
4344
*
44-
* @typedef ReactMarkdownProps
45-
* @property {Element} node
46-
* @property {string} key
47-
* @property {ReactNode[]} children
48-
* @property {Position?} [sourcePosition] Passed when `options.rawSourcePos` is given
49-
* @property {number} [index] Passed when `options.includeElementIndex` is given
50-
* @property {number} [siblingCount] Passed when `options.includeElementIndex` is given
51-
*
5245
* @callback CodeComponent
5346
* @param {JSX.IntrinsicElements['code'] & ReactMarkdownProps & {inline?: boolean}} props
5447
* @returns {ReactNode}
@@ -92,8 +85,7 @@
9285
* @property {TableRowComponent|ReactMarkdownNames} tr
9386
* @property {UnorderedListComponent|ReactMarkdownNames} ul
9487
*
95-
* @typedef {{[TagName in keyof IntrinsicElements]: TagName | ((props: IntrinsicElements[TagName] & ReactMarkdownProps) => ReactNode)}} NormalComponents
96-
* @typedef {Partial<Omit<NormalComponents, keyof SpecialComponents> & SpecialComponents>} Components
88+
* @typedef {Partial<Omit<import("./complex-types").NormalComponents, keyof SpecialComponents> & SpecialComponents>} Components
9789
*
9890
* @typedef Options
9991
* @property {boolean} [sourcePos=false]
@@ -145,10 +137,8 @@ export function childrenToReact(context, node) {
145137
) {
146138
children.push(child.value)
147139
}
148-
// @ts-expect-error `raw` nodes are non-standard
149140
} else if (child.type === 'raw' && !context.options.skipHtml) {
150141
// Default behavior is to show (encoded) HTML.
151-
// @ts-expect-error `raw` nodes are non-standard
152142
children.push(child.value)
153143
}
154144
}
@@ -201,7 +191,7 @@ function toReact(context, node, index, parent) {
201191
context.schema = parentSchema
202192

203193
// Nodes created by plugins do not have positional info, in which case we use
204-
// an object that matches the positon interface.
194+
// an object that matches the position interface.
205195
const position = node.position || {
206196
start: {line: null, column: null, offset: null},
207197
end: {line: null, column: null, offset: null}

lib/complex-types.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type {ReactNode, ComponentType} from 'react'
2+
import type {Position} from 'unist'
3+
import type {Element} from 'hast'
4+
5+
/* File for types which are not handled correctly in JSDoc mode */
6+
7+
export interface ReactMarkdownProps {
8+
node: Element
9+
children: ReactNode[]
10+
/**
11+
* Passed when `options.rawSourcePos` is given
12+
*/
13+
sourcePosition?: Position
14+
/**
15+
* Passed when `options.includeElementIndex` is given
16+
*/
17+
index?: number
18+
/**
19+
* Passed when `options.includeElementIndex` is given
20+
*/
21+
siblingCount?: number
22+
}
23+
24+
export type NormalComponents = {
25+
[TagName in keyof JSX.IntrinsicElements]:
26+
| keyof JSX.IntrinsicElements
27+
| ComponentType<JSX.IntrinsicElements[TagName] & ReactMarkdownProps>
28+
}

test/test.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ test('should pass on raw source position to non-tag components if rawSourcePos o
757757
rawSourcePos
758758
rehypePlugins={[raw]}
759759
components={{
760+
// @ts-expect-error JSX types currently only handle element returns not string returns
760761
em({sourcePosition}) {
761762
assert.equal(sourcePosition, {
762763
start: {line: 1, column: 1, offset: 0},

0 commit comments

Comments
 (0)