Skip to content

Commit 0227f1a

Browse files
committed
Refactor to reorganize files
1 parent 7e27d65 commit 0227f1a

12 files changed

+96
-91
lines changed

Diff for: lib/automatic-runtime-html.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {createAutomaticRuntime} from './create-automatic-runtime.js'
2+
import {h} from './index.js'
3+
4+
// Export `JSX` as a global for TypeScript.
5+
export * from './jsx-automatic.js'
6+
7+
export const {Fragment, jsx, jsxDEV, jsxs} = createAutomaticRuntime(h)

Diff for: lib/automatic-runtime-svg.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {createAutomaticRuntime} from './create-automatic-runtime.js'
2+
import {s} from './index.js'
3+
4+
// Export `JSX` as a global for TypeScript.
5+
export * from './jsx-automatic.js'
6+
7+
export const {Fragment, jsx, jsxDEV, jsxs} = createAutomaticRuntime(s)

Diff for: lib/runtime.js renamed to lib/create-automatic-runtime.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
/**
2-
* @typedef {import('./core.js').core} Core
3-
* @typedef {import('./core.js').Element} Element
4-
* @typedef {import('./core.js').HChild} HChild
5-
* @typedef {import('./core.js').HProperties} HProperties
6-
* @typedef {import('./core.js').HPropertyValue} HPropertyValue
7-
* @typedef {import('./core.js').HResult} HResult
8-
* @typedef {import('./core.js').HStyle} HStyle
9-
* @typedef {import('./core.js').Root} Root
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('hast').Root} Root
104
*
11-
* @typedef {Record<string, HPropertyValue | HStyle | HChild>} JSXProps
5+
* @typedef {import('./create-h.js').Child} Child
6+
* @typedef {import('./create-h.js').Properties} Properties
7+
* @typedef {import('./create-h.js').PropertyValue} PropertyValue
8+
* @typedef {import('./create-h.js').Result} Result
9+
* @typedef {import('./create-h.js').Style} Style
10+
* @typedef {import('./create-h.js').createH} CreateH
11+
*
12+
* @typedef {Record<string, Child | PropertyValue | Style>} JSXProps
1213
*/
1314

15+
// Make VS code see references to above symbols.
16+
''
17+
1418
/**
1519
* Create an automatic runtime.
1620
*
17-
* @param {ReturnType<Core>} f
21+
* @param {ReturnType<CreateH>} f
1822
* `h` function.
1923
* @returns
2024
* Automatic JSX runtime.
2125
*/
22-
export function runtime(f) {
26+
export function createAutomaticRuntime(f) {
2327
/**
2428
* @overload
2529
* @param {null} type
26-
* @param {{children?: HChild}} props
30+
* @param {{children?: Child}} props
2731
* @param {string} [key]
2832
* @returns {Root}
2933
*
@@ -35,9 +39,9 @@ export function runtime(f) {
3539
*
3640
* @param {string | null} type
3741
* Element name or `null` to get a root.
38-
* @param {HProperties & {children?: HChild}} props
42+
* @param {Properties & {children?: Child}} props
3943
* Properties.
40-
* @returns {HResult}
44+
* @returns {Result}
4145
* Result.
4246
*/
4347
function jsx(type, props) {

Diff for: lib/core.js renamed to lib/create-h.js

+30-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('hast').Nodes} Nodes
4-
* @typedef {import('hast').Properties} Properties
54
* @typedef {import('hast').Root} Root
65
* @typedef {import('hast').RootContent} RootContent
76
*
@@ -10,29 +9,29 @@
109
*/
1110

1211
/**
13-
* @typedef {Element | Root} HResult
12+
* @typedef {Element | Root} Result
1413
* Result from a `h` (or `s`) call.
1514
*
16-
* @typedef {number | string} HStyleValue
15+
* @typedef {number | string} StyleValue
1716
* Value for a CSS style field.
18-
* @typedef {Record<string, HStyleValue>} HStyle
17+
* @typedef {Record<string, StyleValue>} Style
1918
* Supported value of a `style` prop.
20-
* @typedef {boolean | number | string | null | undefined} HPrimitiveValue
19+
* @typedef {boolean | number | string | null | undefined} PrimitiveValue
2120
* Primitive property value.
22-
* @typedef {Array<number | string>} HArrayValue
21+
* @typedef {Array<number | string>} ArrayValue
2322
* List of property values for space- or comma separated values (such as `className`).
24-
* @typedef {HArrayValue | HPrimitiveValue} HPropertyValue
23+
* @typedef {ArrayValue | PrimitiveValue} PropertyValue
2524
* Primitive value or list value.
26-
* @typedef {{[property: string]: HPropertyValue | HStyle}} HProperties
25+
* @typedef {{[property: string]: PropertyValue | Style}} Properties
2726
* Acceptable value for element properties.
2827
*
29-
* @typedef {number | string | null | undefined} HPrimitiveChild
28+
* @typedef {number | string | null | undefined} PrimitiveChild
3029
* Primitive children, either ignored (nullish), or turned into text nodes.
31-
* @typedef {Array<HPrimitiveChild | Nodes>} HArrayChildNested
30+
* @typedef {Array<ArrayChildNested | Nodes | PrimitiveChild>} ArrayChild
3231
* List of children.
33-
* @typedef {Array<HArrayChildNested | HPrimitiveChild | Nodes>} HArrayChild
34-
* List of children.
35-
* @typedef {HArrayChild | HPrimitiveChild | Nodes} HChild
32+
* @typedef {Array<Nodes | PrimitiveChild>} ArrayChildNested
33+
* List of children (deep).
34+
* @typedef {ArrayChild | Nodes | PrimitiveChild} Child
3635
* Acceptable child value.
3736
*/
3837

@@ -55,46 +54,46 @@ const own = {}.hasOwnProperty
5554
* @returns
5655
* `h`.
5756
*/
58-
export function core(schema, defaultTagName, caseSensitive) {
57+
export function createH(schema, defaultTagName, caseSensitive) {
5958
const adjust = caseSensitive && createAdjustMap(caseSensitive)
6059

6160
/**
6261
* Hyperscript compatible DSL for creating virtual hast trees.
6362
*
6463
* @overload
6564
* @param {null | undefined} [selector]
66-
* @param {...HChild} children
65+
* @param {...Child} children
6766
* @returns {Root}
6867
*
6968
* @overload
7069
* @param {string} selector
71-
* @param {HProperties} properties
72-
* @param {...HChild} children
70+
* @param {Properties} properties
71+
* @param {...Child} children
7372
* @returns {Element}
7473
*
7574
* @overload
7675
* @param {string} selector
77-
* @param {...HChild} children
76+
* @param {...Child} children
7877
* @returns {Element}
7978
*
8079
* @param {string | null | undefined} [selector]
8180
* Selector.
82-
* @param {HChild | HProperties | null | undefined} [properties]
81+
* @param {Child | Properties | null | undefined} [properties]
8382
* Properties (or first child) (default: `undefined`).
84-
* @param {...HChild} children
83+
* @param {...Child} children
8584
* Children.
86-
* @returns {HResult}
85+
* @returns {Result}
8786
* Result.
8887
*/
8988
function h(selector, properties, ...children) {
9089
let index = -1
91-
/** @type {HResult} */
90+
/** @type {Result} */
9291
let node
9392

9493
if (selector === undefined || selector === null) {
9594
node = {type: 'root', children: []}
9695
// Properties are not supported for roots.
97-
const child = /** @type {HChild} */ (properties)
96+
const child = /** @type {Child} */ (properties)
9897
children.unshift(child)
9998
} else {
10099
node = parseSelector(selector, defaultTagName)
@@ -138,11 +137,11 @@ export function core(schema, defaultTagName, caseSensitive) {
138137
/**
139138
* Check if something is properties or a child.
140139
*
141-
* @param {HChild | HProperties} value
140+
* @param {Child | Properties} value
142141
* Value to check.
143142
* @param {string} name
144143
* Tag name.
145-
* @returns {value is HProperties}
144+
* @returns {value is Properties}
146145
* Whether `value` is a properties object.
147146
*/
148147
function isProperties(value, name) {
@@ -177,15 +176,15 @@ function isProperties(value, name) {
177176
* Properties object.
178177
* @param {string} key
179178
* Property name.
180-
* @param {HPropertyValue | HStyle} value
179+
* @param {PropertyValue | Style} value
181180
* Property value.
182181
* @returns {undefined}
183182
* Nothing.
184183
*/
185184
function addProperty(schema, properties, key, value) {
186185
const info = find(schema, key)
187186
let index = -1
188-
/** @type {HPropertyValue} */
187+
/** @type {PropertyValue} */
189188
let result
190189

191190
// Ignore nullish and NaN values.
@@ -246,7 +245,7 @@ function addProperty(schema, properties, key, value) {
246245
/**
247246
* @param {Array<RootContent>} nodes
248247
* Children.
249-
* @param {HChild} value
248+
* @param {Child} value
250249
* Child.
251250
* @returns {undefined}
252251
* Nothing.
@@ -280,9 +279,9 @@ function addChild(nodes, value) {
280279
* Property information.
281280
* @param {string} name
282281
* Property name.
283-
* @param {HPrimitiveValue} value
282+
* @param {PrimitiveValue} value
284283
* Property value.
285-
* @returns {HPrimitiveValue}
284+
* @returns {PrimitiveValue}
286285
* Property value.
287286
*/
288287
function parsePrimitive(info, name, value) {
@@ -305,7 +304,7 @@ function parsePrimitive(info, name, value) {
305304
/**
306305
* Serialize a `style` object as a string.
307306
*
308-
* @param {HStyle} value
307+
* @param {Style} value
309308
* Style object.
310309
* @returns {string}
311310
* CSS string.

Diff for: lib/index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
/**
2-
* @typedef {import('./core.js').HChild} Child
2+
* @typedef {import('./create-h.js').Child} Child
33
* Acceptable child value.
4-
* @typedef {import('./core.js').HProperties} Properties
4+
* @typedef {import('./create-h.js').Properties} Properties
55
* Acceptable value for element properties.
6-
* @typedef {import('./core.js').HResult} Result
6+
* @typedef {import('./create-h.js').Result} Result
77
* Result from a `h` (or `s`) call.
88
*/
99

10-
// Register the JSX namespace.
10+
// Register the JSX namespace on `h`.
1111
/**
1212
* @typedef {import('./jsx-classic.js').Element} h.JSX.Element
1313
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} h.JSX.ElementChildrenAttribute
1414
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} h.JSX.IntrinsicAttributes
1515
* @typedef {import('./jsx-classic.js').IntrinsicElements} h.JSX.IntrinsicElements
16-
*
16+
*/
17+
18+
// Register the JSX namespace on `s`.
19+
/**
1720
* @typedef {import('./jsx-classic.js').Element} s.JSX.Element
1821
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} s.JSX.ElementChildrenAttribute
1922
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} s.JSX.IntrinsicAttributes
2023
* @typedef {import('./jsx-classic.js').IntrinsicElements} s.JSX.IntrinsicElements
2124
*/
2225

2326
import {html, svg} from 'property-information'
24-
import {core} from './core.js'
27+
import {createH} from './create-h.js'
2528
import {svgCaseSensitiveTagNames} from './svg-case-sensitive-tag-names.js'
2629

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

3134
// Note: this explicit type is needed, otherwise TS creates broken types.
32-
/** @type {ReturnType<core>} */
33-
export const s = core(svg, 'g', svgCaseSensitiveTagNames)
35+
/** @type {ReturnType<createH>} */
36+
export const s = createH(svg, 'g', svgCaseSensitiveTagNames)

Diff for: lib/jsx-automatic.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type {HChild, HProperties, HResult} from './core.js'
1+
import type {Child, Properties, Result} from './create-h.js'
22

33
export namespace JSX {
44
/**
55
* Define the return value of JSX syntax.
66
*/
7-
type Element = HResult
7+
type Element = Result
88

99
/**
1010
* Key of this interface defines as what prop children are passed.
@@ -31,13 +31,13 @@ export namespace JSX {
3131
*/
3232
interface IntrinsicElements {
3333
[name: string]:
34-
| HProperties
34+
| Properties
3535
| {
3636
/**
3737
* The prop that matches `ElementChildrenAttribute` key defines the
3838
* type of JSX children, defines the children type.
3939
*/
40-
children?: HChild
40+
children?: Child
4141
}
4242
}
4343
}

Diff for: lib/jsx-classic.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {HProperties, HChild, HResult} from './core.js'
1+
import type {Child, Properties, Result} from './create-h.js'
22

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

1414
/**
1515
* Key of this interface defines as what prop children are passed.
@@ -36,12 +36,12 @@ export type IntrinsicAttributes = never
3636
*/
3737
export interface IntrinsicElements {
3838
[name: string]:
39-
| HProperties
39+
| Properties
4040
| {
4141
/**
4242
* The prop that matches `ElementChildrenAttribute` key defines the
4343
* type of JSX children, defines the children type.
4444
*/
45-
[children]?: HChild
45+
[children]?: Child
4646
}
4747
}

Diff for: lib/runtime-html.js

-7
This file was deleted.

Diff for: lib/runtime-svg.js

-7
This file was deleted.

Diff for: package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
"types": "index.d.ts",
3434
"exports": {
3535
".": "./index.js",
36-
"./jsx-runtime": "./lib/runtime-html.js",
37-
"./jsx-dev-runtime": "./lib/runtime-html.js",
38-
"./svg/jsx-runtime": "./lib/runtime-svg.js",
39-
"./svg/jsx-dev-runtime": "./lib/runtime-svg.js"
36+
"./jsx-runtime": "./lib/automatic-runtime-html.js",
37+
"./jsx-dev-runtime": "./lib/automatic-runtime-html.js",
38+
"./svg/jsx-runtime": "./lib/automatic-runtime-svg.js",
39+
"./svg/jsx-dev-runtime": "./lib/automatic-runtime-svg.js"
4040
},
4141
"files": [
4242
"lib/",

0 commit comments

Comments
 (0)