Skip to content

Commit

Permalink
Refactor to reorganize files
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 1, 2023
1 parent 7e27d65 commit 0227f1a
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 91 deletions.
7 changes: 7 additions & 0 deletions lib/automatic-runtime-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {createAutomaticRuntime} from './create-automatic-runtime.js'
import {h} from './index.js'

// Export `JSX` as a global for TypeScript.
export * from './jsx-automatic.js'

export const {Fragment, jsx, jsxDEV, jsxs} = createAutomaticRuntime(h)
7 changes: 7 additions & 0 deletions lib/automatic-runtime-svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {createAutomaticRuntime} from './create-automatic-runtime.js'
import {s} from './index.js'

// Export `JSX` as a global for TypeScript.
export * from './jsx-automatic.js'

export const {Fragment, jsx, jsxDEV, jsxs} = createAutomaticRuntime(s)
32 changes: 18 additions & 14 deletions lib/runtime.js → lib/create-automatic-runtime.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
/**
* @typedef {import('./core.js').core} Core
* @typedef {import('./core.js').Element} Element
* @typedef {import('./core.js').HChild} HChild
* @typedef {import('./core.js').HProperties} HProperties
* @typedef {import('./core.js').HPropertyValue} HPropertyValue
* @typedef {import('./core.js').HResult} HResult
* @typedef {import('./core.js').HStyle} HStyle
* @typedef {import('./core.js').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Root} Root
*
* @typedef {Record<string, HPropertyValue | HStyle | HChild>} JSXProps
* @typedef {import('./create-h.js').Child} Child
* @typedef {import('./create-h.js').Properties} Properties
* @typedef {import('./create-h.js').PropertyValue} PropertyValue
* @typedef {import('./create-h.js').Result} Result
* @typedef {import('./create-h.js').Style} Style
* @typedef {import('./create-h.js').createH} CreateH
*
* @typedef {Record<string, Child | PropertyValue | Style>} JSXProps
*/

// Make VS code see references to above symbols.
''

/**
* Create an automatic runtime.
*
* @param {ReturnType<Core>} f
* @param {ReturnType<CreateH>} f
* `h` function.
* @returns
* Automatic JSX runtime.
*/
export function runtime(f) {
export function createAutomaticRuntime(f) {
/**
* @overload
* @param {null} type
* @param {{children?: HChild}} props
* @param {{children?: Child}} props
* @param {string} [key]
* @returns {Root}
*
Expand All @@ -35,9 +39,9 @@ export function runtime(f) {
*
* @param {string | null} type
* Element name or `null` to get a root.
* @param {HProperties & {children?: HChild}} props
* @param {Properties & {children?: Child}} props
* Properties.
* @returns {HResult}
* @returns {Result}
* Result.
*/
function jsx(type, props) {
Expand Down
61 changes: 30 additions & 31 deletions lib/core.js → lib/create-h.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Properties} Properties
* @typedef {import('hast').Root} Root
* @typedef {import('hast').RootContent} RootContent
*
Expand All @@ -10,29 +9,29 @@
*/

/**
* @typedef {Element | Root} HResult
* @typedef {Element | Root} Result
* Result from a `h` (or `s`) call.
*
* @typedef {number | string} HStyleValue
* @typedef {number | string} StyleValue
* Value for a CSS style field.
* @typedef {Record<string, HStyleValue>} HStyle
* @typedef {Record<string, StyleValue>} Style
* Supported value of a `style` prop.
* @typedef {boolean | number | string | null | undefined} HPrimitiveValue
* @typedef {boolean | number | string | null | undefined} PrimitiveValue
* Primitive property value.
* @typedef {Array<number | string>} HArrayValue
* @typedef {Array<number | string>} ArrayValue
* List of property values for space- or comma separated values (such as `className`).
* @typedef {HArrayValue | HPrimitiveValue} HPropertyValue
* @typedef {ArrayValue | PrimitiveValue} PropertyValue
* Primitive value or list value.
* @typedef {{[property: string]: HPropertyValue | HStyle}} HProperties
* @typedef {{[property: string]: PropertyValue | Style}} Properties
* Acceptable value for element properties.
*
* @typedef {number | string | null | undefined} HPrimitiveChild
* @typedef {number | string | null | undefined} PrimitiveChild
* Primitive children, either ignored (nullish), or turned into text nodes.
* @typedef {Array<HPrimitiveChild | Nodes>} HArrayChildNested
* @typedef {Array<ArrayChildNested | Nodes | PrimitiveChild>} ArrayChild
* List of children.
* @typedef {Array<HArrayChildNested | HPrimitiveChild | Nodes>} HArrayChild
* List of children.
* @typedef {HArrayChild | HPrimitiveChild | Nodes} HChild
* @typedef {Array<Nodes | PrimitiveChild>} ArrayChildNested
* List of children (deep).
* @typedef {ArrayChild | Nodes | PrimitiveChild} Child
* Acceptable child value.
*/

Expand All @@ -55,46 +54,46 @@ const own = {}.hasOwnProperty
* @returns
* `h`.
*/
export function core(schema, defaultTagName, caseSensitive) {
export function createH(schema, defaultTagName, caseSensitive) {
const adjust = caseSensitive && createAdjustMap(caseSensitive)

/**
* Hyperscript compatible DSL for creating virtual hast trees.
*
* @overload
* @param {null | undefined} [selector]
* @param {...HChild} children
* @param {...Child} children
* @returns {Root}
*
* @overload
* @param {string} selector
* @param {HProperties} properties
* @param {...HChild} children
* @param {Properties} properties
* @param {...Child} children
* @returns {Element}
*
* @overload
* @param {string} selector
* @param {...HChild} children
* @param {...Child} children
* @returns {Element}
*
* @param {string | null | undefined} [selector]
* Selector.
* @param {HChild | HProperties | null | undefined} [properties]
* @param {Child | Properties | null | undefined} [properties]
* Properties (or first child) (default: `undefined`).
* @param {...HChild} children
* @param {...Child} children
* Children.
* @returns {HResult}
* @returns {Result}
* Result.
*/
function h(selector, properties, ...children) {
let index = -1
/** @type {HResult} */
/** @type {Result} */
let node

if (selector === undefined || selector === null) {
node = {type: 'root', children: []}
// Properties are not supported for roots.
const child = /** @type {HChild} */ (properties)
const child = /** @type {Child} */ (properties)
children.unshift(child)
} else {
node = parseSelector(selector, defaultTagName)
Expand Down Expand Up @@ -138,11 +137,11 @@ export function core(schema, defaultTagName, caseSensitive) {
/**
* Check if something is properties or a child.
*
* @param {HChild | HProperties} value
* @param {Child | Properties} value
* Value to check.
* @param {string} name
* Tag name.
* @returns {value is HProperties}
* @returns {value is Properties}
* Whether `value` is a properties object.
*/
function isProperties(value, name) {
Expand Down Expand Up @@ -177,15 +176,15 @@ function isProperties(value, name) {
* Properties object.
* @param {string} key
* Property name.
* @param {HPropertyValue | HStyle} value
* @param {PropertyValue | Style} value
* Property value.
* @returns {undefined}
* Nothing.
*/
function addProperty(schema, properties, key, value) {
const info = find(schema, key)
let index = -1
/** @type {HPropertyValue} */
/** @type {PropertyValue} */
let result

// Ignore nullish and NaN values.
Expand Down Expand Up @@ -246,7 +245,7 @@ function addProperty(schema, properties, key, value) {
/**
* @param {Array<RootContent>} nodes
* Children.
* @param {HChild} value
* @param {Child} value
* Child.
* @returns {undefined}
* Nothing.
Expand Down Expand Up @@ -280,9 +279,9 @@ function addChild(nodes, value) {
* Property information.
* @param {string} name
* Property name.
* @param {HPrimitiveValue} value
* @param {PrimitiveValue} value
* Property value.
* @returns {HPrimitiveValue}
* @returns {PrimitiveValue}
* Property value.
*/
function parsePrimitive(info, name, value) {
Expand All @@ -305,7 +304,7 @@ function parsePrimitive(info, name, value) {
/**
* Serialize a `style` object as a string.
*
* @param {HStyle} value
* @param {Style} value
* Style object.
* @returns {string}
* CSS string.
Expand Down
23 changes: 13 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
/**
* @typedef {import('./core.js').HChild} Child
* @typedef {import('./create-h.js').Child} Child
* Acceptable child value.
* @typedef {import('./core.js').HProperties} Properties
* @typedef {import('./create-h.js').Properties} Properties
* Acceptable value for element properties.
* @typedef {import('./core.js').HResult} Result
* @typedef {import('./create-h.js').Result} Result
* Result from a `h` (or `s`) call.
*/

// Register the JSX namespace.
// Register the JSX namespace on `h`.
/**
* @typedef {import('./jsx-classic.js').Element} h.JSX.Element
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} h.JSX.ElementChildrenAttribute
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} h.JSX.IntrinsicAttributes
* @typedef {import('./jsx-classic.js').IntrinsicElements} h.JSX.IntrinsicElements
*
*/

// Register the JSX namespace on `s`.
/**
* @typedef {import('./jsx-classic.js').Element} s.JSX.Element
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} s.JSX.ElementChildrenAttribute
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} s.JSX.IntrinsicAttributes
* @typedef {import('./jsx-classic.js').IntrinsicElements} s.JSX.IntrinsicElements
*/

import {html, svg} from 'property-information'
import {core} from './core.js'
import {createH} from './create-h.js'
import {svgCaseSensitiveTagNames} from './svg-case-sensitive-tag-names.js'

// Note: this explicit type is needed, otherwise TS creates broken types.
/** @type {ReturnType<core>} */
export const h = core(html, 'div')
/** @type {ReturnType<createH>} */
export const h = createH(html, 'div')

// Note: this explicit type is needed, otherwise TS creates broken types.
/** @type {ReturnType<core>} */
export const s = core(svg, 'g', svgCaseSensitiveTagNames)
/** @type {ReturnType<createH>} */
export const s = createH(svg, 'g', svgCaseSensitiveTagNames)
8 changes: 4 additions & 4 deletions lib/jsx-automatic.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {HChild, HProperties, HResult} from './core.js'
import type {Child, Properties, Result} from './create-h.js'

export namespace JSX {
/**
* Define the return value of JSX syntax.
*/
type Element = HResult
type Element = Result

/**
* Key of this interface defines as what prop children are passed.
Expand All @@ -31,13 +31,13 @@ export namespace JSX {
*/
interface IntrinsicElements {
[name: string]:
| HProperties
| Properties
| {
/**
* The prop that matches `ElementChildrenAttribute` key defines the
* type of JSX children, defines the children type.
*/
children?: HChild
children?: Child
}
}
}
8 changes: 4 additions & 4 deletions lib/jsx-classic.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {HProperties, HChild, HResult} from './core.js'
import type {Child, Properties, Result} from './create-h.js'

/**
* This unique symbol is declared to specify the key on which JSX children are
Expand All @@ -9,7 +9,7 @@ declare const children: unique symbol
/**
* Define the return value of JSX syntax.
*/
export type Element = HResult
export type Element = Result

/**
* Key of this interface defines as what prop children are passed.
Expand All @@ -36,12 +36,12 @@ export type IntrinsicAttributes = never
*/
export interface IntrinsicElements {
[name: string]:
| HProperties
| Properties
| {
/**
* The prop that matches `ElementChildrenAttribute` key defines the
* type of JSX children, defines the children type.
*/
[children]?: HChild
[children]?: Child
}
}
7 changes: 0 additions & 7 deletions lib/runtime-html.js

This file was deleted.

7 changes: 0 additions & 7 deletions lib/runtime-svg.js

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"types": "index.d.ts",
"exports": {
".": "./index.js",
"./jsx-runtime": "./lib/runtime-html.js",
"./jsx-dev-runtime": "./lib/runtime-html.js",
"./svg/jsx-runtime": "./lib/runtime-svg.js",
"./svg/jsx-dev-runtime": "./lib/runtime-svg.js"
"./jsx-runtime": "./lib/automatic-runtime-html.js",
"./jsx-dev-runtime": "./lib/automatic-runtime-html.js",
"./svg/jsx-runtime": "./lib/automatic-runtime-svg.js",
"./svg/jsx-dev-runtime": "./lib/automatic-runtime-svg.js"
},
"files": [
"lib/",
Expand Down
Loading

0 comments on commit 0227f1a

Please sign in to comment.