From 0ff4aaeeb9fd843546302f5660e5209b26288f75 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Wed, 25 Aug 2021 17:19:47 -0700 Subject: [PATCH 1/3] types: avoid inlining intrinsics at build time leverage ComponentType to allow for class components as well. --- .gitignore | 2 +- lib/custom-types.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/index.js | 20 +------------------- 3 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 lib/custom-types.ts diff --git a/.gitignore b/.gitignore index fa43271..886e8ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ coverage/ node_modules/ .DS_Store -lib/index.d.ts +lib/*.d.ts test.d.ts *.log yarn.lock diff --git a/lib/custom-types.ts b/lib/custom-types.ts new file mode 100644 index 0000000..6614576 --- /dev/null +++ b/lib/custom-types.ts @@ -0,0 +1,43 @@ +import type {ComponentType} from 'react' +import type {Element} from 'hast' + +export interface WithNode { + node: Element +} + +export interface ComponentsWithNodeOptions { + /** + * Expose hast elements as a `node` field in components + */ + passNode: true + /** + * Override default elements (such as ``, `

`, etcetera) by passing an + * object mapping tag names to components. + */ + components?: Partial< + { + [TagName in keyof JSX.IntrinsicElements]: + | string + | ComponentType + } + > +} + +export interface ComponentsWithoutNodeOptions { + /** + * Expose hast elements as a `node` field in components. + */ + passNode?: false | undefined + + /** + * Override default elements (such as ``, `

`, etcetera) by passing an + * object mapping tag names to components. + */ + components?: Partial< + { + [TagName in keyof JSX.IntrinsicElements]: + | string + | ComponentType + } + > +} diff --git a/lib/index.js b/lib/index.js index a23baf4..337b9f9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,25 +20,7 @@ * @property {string|undefined} [prefix='h-'] * React key prefix * - * @typedef WithNode - * @property {Element} node - * Current hast element. - * - * @typedef ComponentsWithNodeOptions - * @property {true} passNode - * Expose hast elements as a `node` field in components. - * @property {Partial<{[TagName in keyof JSX.IntrinsicElements]: string|((props: WithNode & JSX.IntrinsicElements[TagName]) => ReactNode)}>} [components] - * Override default elements (such as ``, `

`, etcetera) by passing an - * object mapping tag names to components. - * - * @typedef ComponentsWithoutNodeOptions - * @property {false|undefined} [passNode] - * Expose hast elements as a `node` field in components. - * @property {Partial<{[TagName in keyof JSX.IntrinsicElements]: string|((props: JSX.IntrinsicElements[TagName]) => ReactNode)}>} [components] - * Override default elements (such as ``, `

`, etcetera) by passing an - * object mapping tag names to components. - * - * @typedef {SharedOptions & (ComponentsWithNodeOptions|ComponentsWithoutNodeOptions)} Options + * @typedef {SharedOptions & (import("./custom-types").ComponentsWithNodeOptions|import("./custom-types").ComponentsWithoutNodeOptions)} Options */ import {toH} from 'hast-to-hyperscript' From eb6f6fc038c779bf32196369741a0e8720ae776f Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Thu, 26 Aug 2021 06:19:13 -0700 Subject: [PATCH 2/3] types: use complex-types as filename, don't export WithNode type --- lib/{custom-types.ts => complex-types.ts} | 2 +- lib/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename lib/{custom-types.ts => complex-types.ts} (97%) diff --git a/lib/custom-types.ts b/lib/complex-types.ts similarity index 97% rename from lib/custom-types.ts rename to lib/complex-types.ts index 6614576..e265b7b 100644 --- a/lib/custom-types.ts +++ b/lib/complex-types.ts @@ -1,7 +1,7 @@ import type {ComponentType} from 'react' import type {Element} from 'hast' -export interface WithNode { +interface WithNode { node: Element } diff --git a/lib/index.js b/lib/index.js index 337b9f9..d8e0603 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,7 +20,7 @@ * @property {string|undefined} [prefix='h-'] * React key prefix * - * @typedef {SharedOptions & (import("./custom-types").ComponentsWithNodeOptions|import("./custom-types").ComponentsWithoutNodeOptions)} Options + * @typedef {SharedOptions & (import("./complex-types").ComponentsWithNodeOptions|import("./complex-types").ComponentsWithoutNodeOptions)} Options */ import {toH} from 'hast-to-hyperscript' From 3a85939ea6fc31f929f0707f8157d11f5401a689 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Thu, 26 Aug 2021 06:44:11 -0700 Subject: [PATCH 3/3] types: narrow supported tag names to intrinsics --- lib/complex-types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/complex-types.ts b/lib/complex-types.ts index e265b7b..86ec5a8 100644 --- a/lib/complex-types.ts +++ b/lib/complex-types.ts @@ -17,7 +17,7 @@ export interface ComponentsWithNodeOptions { components?: Partial< { [TagName in keyof JSX.IntrinsicElements]: - | string + | keyof JSX.IntrinsicElements | ComponentType } > @@ -36,7 +36,7 @@ export interface ComponentsWithoutNodeOptions { components?: Partial< { [TagName in keyof JSX.IntrinsicElements]: - | string + | keyof JSX.IntrinsicElements | ComponentType } >