Skip to content

Commit e4654eb

Browse files
committed
Refactor code-style
* Refactor a bunch of code * Add more JSDoc
1 parent 30b0679 commit e4654eb

File tree

8 files changed

+107
-56
lines changed

8 files changed

+107
-56
lines changed

index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
2-
* @typedef {import('./lib/index.js').XChild} Child
3-
* Acceptable child value
4-
* @typedef {import('./lib/index.js').XAttributes} Attributes
5-
* Acceptable attributes value.
6-
* @typedef {import('./lib/index.js').XResult} Result
2+
* @typedef {import('./lib/index.js').Child} Child
3+
* @typedef {import('./lib/index.js').Attributes} Attributes
4+
* @typedef {import('./lib/index.js').Result} Result
75
*/
86

97
export {x} from './lib/index.js'

jsx-dev-runtime.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
/**
22
* @typedef {import('xast').Element} Element
33
* @typedef {import('xast').Root} Root
4-
* @typedef {import('./lib/index.js').XChild} XChild
4+
* @typedef {import('./lib/index.js').Child} Child
55
* @typedef {import('./lib/runtime.js').JSXProps} JSXProps
66
*/
77

8-
import {jsx} from './jsx-runtime.js'
8+
import {jsx} from './lib/runtime.js'
9+
10+
// Export `JSX` as a global for TypeScript.
11+
export * from './lib/jsx-automatic.js'
912

1013
export {Fragment} from './jsx-runtime.js'
1114

1215
// eslint-disable-next-line unicorn/prefer-export-from
1316
export const jsxDEV =
1417
/**
1518
* @type {{
16-
* (name: null | undefined, props: {children?: XChild}, ...unused: unknown[]): Root
17-
* (name: string, props: JSXProps, ...unused: unknown[]): Element
19+
* (name: null | undefined, props: {children?: Child}, key?: string, ...unused: Array<unknown>): Root
20+
* (name: string, props: JSXProps, key?: string, ...unused: Array<unknown>): Element
1821
* }}
1922
*/
2023
(jsx)

jsx-runtime.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
* @typedef {import('./lib/runtime.js').JSXProps}} JSXProps
33
*/
44

5-
export * from './lib/runtime.js'
5+
// Export `JSX` as a global for TypeScript.
6+
export * from './lib/jsx-automatic.js'
7+
8+
export {jsx, jsxs, Fragment} from './lib/runtime.js'

lib/index.js

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@
44
*/
55

66
/**
7-
* @typedef {Root['children'][number]} Child
8-
* @typedef {Child | Root} Node
9-
* @typedef {Root | Element} XResult
10-
* @typedef {string | number | boolean | null | undefined} XValue
11-
* @typedef {{[attribute: string]: XValue}} XAttributes Attributes to support JS primitive types
7+
* @typedef {Root['children'][number]} Content
8+
* @typedef {Content | Root} Node
9+
* Any concrete `xast` node.
10+
*
11+
* @typedef {Root | Element} Result
12+
* Result from a `x` call.
13+
*
14+
* @typedef {string | number | boolean | null | undefined} Value
15+
* Attribute value
16+
* @typedef {{[attribute: string]: Value}} Attributes
17+
* Acceptable value for element properties.
18+
*
19+
* @typedef {string | number | null | undefined} PrimitiveChild
20+
* Primitive children, either ignored (nullish), or turned into text nodes.
21+
* @typedef {Array<Node | PrimitiveChild>} ArrayChild
22+
* List of children.
23+
* @typedef {Node | PrimitiveChild | ArrayChild} Child
24+
* Acceptable child value.
1225
*
13-
* @typedef {string | number | null | undefined} XPrimitiveChild
14-
* @typedef {Array<Node | XPrimitiveChild>} XArrayChild
15-
* @typedef {Node | XPrimitiveChild | XArrayChild} XChild
1626
* @typedef {import('./jsx-classic.js').Element} x.JSX.Element
1727
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} x.JSX.IntrinsicAttributes
1828
* @typedef {import('./jsx-classic.js').IntrinsicElements} x.JSX.IntrinsicElements
@@ -22,31 +32,48 @@
2232
/**
2333
* Create XML trees in xast.
2434
*
25-
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as `rdf:RDF`). Pass `null | undefined` to build a root.
26-
* @param attributes Map of attributes. Nullish (null or undefined) or NaN values are ignored, other values (strings, booleans) are cast to strings.
27-
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
35+
* @param name
36+
* Qualified name.
37+
*
38+
* Case sensitive and can contain a namespace prefix (such as `rdf:RDF`).
39+
* When string, an `Element` is built.
40+
* When nullish, a `Root` is built instead.
41+
* @param attributes
42+
* Map of attributes.
43+
*
44+
* Nullish (`null` or `undefined`) or `NaN` values are ignored, other values
45+
* are turned to strings.
46+
*
47+
* Cannot be given if building a `Root`.
48+
* Cannot be omitted when building an `Element` if the first child is a
49+
* `Node`.
50+
* @param children
51+
* (Lists of) children.
52+
*
53+
* When strings or numbers are encountered, they are mapped to `Text` nodes.
54+
* If a `Root` node is encountered, its children are used instead.
55+
* @returns
56+
* `Element` or `Root`.
2857
*/
2958
export const x =
3059
/**
3160
* @type {{
3261
* (): Root
33-
* (name: null | undefined, ...children: Array<XChild>): Root
34-
* (name: string, attributes: XAttributes, ...children: Array<XChild>): Element
35-
* (name: string, ...children: Array<XChild>): Element
62+
* (name: null | undefined, ...children: Array<Child>): Root
63+
* (name: string, attributes?: Attributes, ...children: Array<Child>): Element
64+
* (name: string, ...children: Array<Child>): Element
3665
* }}
3766
*/
3867
(
3968
/**
40-
* Hyperscript compatible DSL for creating virtual xast trees.
41-
*
42-
* @param {string | null} [name]
43-
* @param {XAttributes | XChild} [attributes]
44-
* @param {Array<XChild>} children
45-
* @returns {XResult}
69+
* @param {string | null | undefined} [name]
70+
* @param {Attributes | Child | null | undefined} [attributes]
71+
* @param {Array<Child>} children
72+
* @returns {Result}
4673
*/
4774
function (name, attributes, ...children) {
4875
let index = -1
49-
/** @type {XResult} */
76+
/** @type {Result} */
5077
let node
5178

5279
if (name === undefined || name === null) {
@@ -89,8 +116,14 @@ export const x =
89116
)
90117

91118
/**
119+
* Add children.
120+
*
92121
* @param {Array<Child>} nodes
93-
* @param {XChild} value
122+
* List of nodes.
123+
* @param {Child} value
124+
* Child.
125+
* @returns {void}
126+
* Nothing.
94127
*/
95128
function addChild(nodes, value) {
96129
let index = -1
@@ -115,8 +148,12 @@ function addChild(nodes, value) {
115148
}
116149

117150
/**
118-
* @param {XAttributes | XChild} value
119-
* @returns {value is XAttributes}
151+
* Check if `value` is `Attributes`.
152+
*
153+
* @param {Attributes | Child} value
154+
* Value.
155+
* @returns {value is Attributes}
156+
* Whether `value` is `Attributes`.
120157
*/
121158
function isAttributes(value) {
122159
if (

lib/jsx-automatic.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* eslint-disable-next-line @typescript-eslint/consistent-type-imports -- fix in major */
2-
import {XAttributes, XChild, XResult} from './index.js'
2+
import {Attributes, Child, Result} from './index.js'
33

44
export namespace JSX {
55
/**
66
* This defines the return value of JSX syntax.
77
*/
8-
type Element = XResult
8+
type Element = Result
99

1010
/**
1111
* This disallows the use of functional components.
@@ -24,12 +24,12 @@ export namespace JSX {
2424
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
2525
interface IntrinsicElements {
2626
[name: string]:
27-
| XAttributes
27+
| Attributes
2828
| {
2929
/**
3030
* The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type.
3131
*/
32-
children?: XChild
32+
children?: Child
3333
}
3434
}
3535

lib/jsx-classic.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable-next-line @typescript-eslint/consistent-type-imports -- fix in major */
2-
import {XAttributes, XChild, XResult} from './index.js'
2+
import {Attributes, Child, Result} from './index.js'
33

44
/**
55
* This unique symbol is declared to specify the key on which JSX children are passed, without conflicting
@@ -10,7 +10,7 @@ declare const children: unique symbol
1010
/**
1111
* This defines the return value of JSX syntax.
1212
*/
13-
export type Element = XResult
13+
export type Element = Result
1414

1515
/**
1616
* This disallows the use of functional components.
@@ -29,12 +29,12 @@ export type IntrinsicAttributes = never
2929
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
3030
export interface IntrinsicElements {
3131
[name: string]:
32-
| XAttributes
32+
| Attributes
3333
| {
3434
/**
3535
* The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type.
3636
*/
37-
[children]?: XChild
37+
[children]?: Child
3838
}
3939
}
4040

lib/runtime.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
11
/**
22
* @typedef {import('./index.js').Element} Element
33
* @typedef {import('./index.js').Root} Root
4-
* @typedef {import('./index.js').XResult} XResult
5-
* @typedef {import('./index.js').XChild} XChild
6-
* @typedef {import('./index.js').XAttributes} XAttributes
7-
* @typedef {import('./index.js').XValue} XValue
4+
* @typedef {import('./index.js').Result} Result
5+
* @typedef {import('./index.js').Child} Child
6+
* @typedef {import('./index.js').Attributes} Attributes
7+
* @typedef {import('./index.js').Value} Value
88
*
9-
* @typedef {{[x: string]: XValue | XChild}} JSXProps
9+
* @typedef {{[x: string]: Value | Child}} JSXProps
1010
*/
1111

1212
import {x} from './index.js'
1313

14-
// Export `JSX` as a global for TypeScript.
15-
export * from './jsx-automatic.js'
16-
1714
/**
1815
* Create XML trees in xast through JSX.
1916
*
20-
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as `rdf:RDF`). Pass `null | undefined` to build a root.
21-
* @param props Map of attributes. Nullish (null or undefined) or NaN values are ignored, other values (strings, booleans) are cast to strings. `children` can contain one child or a list of children. When strings are encountered, they are mapped to text nodes.
17+
* @param name
18+
* Qualified name.
19+
*
20+
* Case sensitive and can contain a namespace prefix (such as `rdf:RDF`).
21+
* When string, an `Element` is built.
22+
* When nullish, a `Root` is built instead.
23+
* @param props
24+
* Map of attributes.
25+
*
26+
* Nullish (`null` or `undefined`) or `NaN` values are ignored, other values
27+
* are turned to strings.
28+
*
29+
* Cannot be given if building a `Root`.
30+
* Cannot be omitted when building an `Element` if the first child is a
31+
* `Node`.
2232
*/
2333
export const jsx =
2434
/**
2535
* @type {{
26-
* (name: null | undefined, props: {children?: XChild}, key?: string): Root
36+
* (name: null | undefined, props: {children?: Child}, key?: string): Root
2737
* (name: string, props: JSXProps, key?: string): Element
2838
* }}
2939
*/
3040
(
3141
/**
3242
* @param {string | null} name
33-
* @param {XAttributes & {children?: XChild}} props
34-
* @returns {XResult}
43+
* @param {Attributes & {children?: Child}} props
44+
* @returns {Result}
3545
*/
3646
function (name, props) {
3747
const {children, ...properties} = props
@@ -41,5 +51,4 @@ export const jsx =
4151

4252
export const jsxs = jsx
4353

44-
/** @type {null} */
4554
export const Fragment = null

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Create [xast][] trees.
209209
###### `name`
210210

211211
Qualified name (`string`, optional).
212+
212213
Case sensitive and can contain a namespace prefix (such as `rdf:RDF`).
213214
When string, an [`Element`][element] is built.
214215
When nullish, a [`Root`][root] is built instead.

0 commit comments

Comments
 (0)