create-automatic-runtime.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @import {Element, Root} from 'hast'
  3. * @import {Child, Properties, PropertyValue, Result, Style, createH as CreateH} from './create-h.js'
  4. */
  5. /**
  6. * @typedef {Record<string, Child | PropertyValue | Style>} JSXProps
  7. */
  8. // Make VS code see references to above symbols.
  9. ''
  10. /**
  11. * Create an automatic runtime.
  12. *
  13. * @param {ReturnType<CreateH>} f
  14. * `h` function.
  15. * @returns
  16. * Automatic JSX runtime.
  17. */
  18. export function createAutomaticRuntime(f) {
  19. /**
  20. * @overload
  21. * @param {null} type
  22. * @param {{children?: Child}} properties
  23. * @param {string | null | undefined} [key]
  24. * @returns {Root}
  25. *
  26. * @overload
  27. * @param {string} type
  28. * @param {JSXProps} properties
  29. * @param {string | null | undefined} [key]
  30. * @returns {Element}
  31. *
  32. * @param {string | null} type
  33. * Element name or `null` to get a root.
  34. * @param {Properties & {children?: Child}} properties
  35. * Properties.
  36. * @returns {Result}
  37. * Result.
  38. */
  39. function jsx(type, properties) {
  40. const {children, ...properties_} = properties
  41. const result =
  42. // @ts-ignore: `children` is fine: TS has a recursion problem which
  43. // sometimes generates broken types.
  44. type === null ? f(null, children) : f(type, properties_, children)
  45. return result
  46. }
  47. return {Fragment: null, jsxDEV: jsx, jsxs: jsx, jsx}
  48. }