Skip to content

Commit

Permalink
Fix type to include each element w/ its properties
Browse files Browse the repository at this point in the history
Closes GH-623.
Closes GH-624.
  • Loading branch information
ChristianMurphy committed Jul 29, 2021
1 parent 456bb3b commit 13367ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 3 additions & 7 deletions src/ast-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ exports.hastChildrenToReact = childrenToReact
* @property {number} [index] Passed when `options.includeElementIndex` is given
* @property {number} [siblingCount] Passed when `options.includeElementIndex` is given
*
* @callback NormalComponent
* @param {ReactBaseProps & ReactMarkdownProps} props
* @returns {ReactNode}
*
* @callback CodeComponent
* @param {ReactBaseProps & ReactMarkdownProps & {inline?: boolean}} props
* @returns {ReactNode}
Expand Down Expand Up @@ -136,8 +132,8 @@ exports.hastChildrenToReact = childrenToReact
* @property {TableRowComponent|ReactMarkdownNames} tr
* @property {UnorderedListComponent|ReactMarkdownNames} ul
*
* @typedef {Record<Exclude<ReactMarkdownNames, keyof SpecialComponents>, NormalComponent|ReactMarkdownNames>} NormalComponents
* @typedef {Partial<NormalComponents & SpecialComponents>} Components
* @typedef {{[TagName in keyof IntrinsicElements]: TagName | ((props: IntrinsicElements[TagName] & ReactMarkdownProps) => ReactNode)}} NormalComponents
* @typedef {Partial<Omit<NormalComponents, keyof SpecialComponents> & SpecialComponents>} Components
*/

/**
Expand Down Expand Up @@ -245,7 +241,7 @@ function toReact(context, node, index, parent) {
start: {line: null, column: null, offset: null},
end: {line: null, column: null, offset: null}
}
/** @type {NormalComponent|SpecialComponents[keyof SpecialComponents]|ReactMarkdownNames} */
/** @type {NormalComponents[keyof NormalComponents]|SpecialComponents[keyof SpecialComponents]|ReactMarkdownNames} */
const component =
options.components && own.call(options.components, name)
? options.components[name]
Expand Down
15 changes: 12 additions & 3 deletions test/react-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const toc = require('remark-toc')
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Text} Text
* @typedef {import('react').ReactNode} ReactNode
* @typedef {import('../src/ast-to-react').Components} Components
*/

/**
Expand Down Expand Up @@ -989,14 +990,22 @@ test('should pass index of a node under its parent to components if `includeElem

test('should be able to render components with forwardRef in HOC', () => {
/**
* @param {Function} Component
* @param {(params: Parameters<Exclude<Components['a'], 'a'>>[0]) => JSX.Element} Component
*/
const wrapper = (Component) => {
return React.forwardRef((props, ref) => <Component ref={ref} {...props} />)
return React.forwardRef(
/**
*
* @param {Parameters<Components['a']>[0]} props
* @param {import('react').Ref<HTMLAnchorElement>} ref
* @returns
*/
(props, ref) => <Component ref={ref} {...props} />
)
}

/**
* @param {Object<string, unknown>} props
* @param {Parameters<Exclude<Components['a'], 'a'>>[0]} props
*/
// eslint-disable-next-line react/jsx-no-target-blank
const wrapped = (props) => <a {...props} />
Expand Down

0 comments on commit 13367ed

Please sign in to comment.