Skip to content

Commit de3143c

Browse files
committed
Refactor code-style
1 parent 21d232d commit de3143c

19 files changed

+1253
-1154
lines changed

Diff for: lib/core.js

+121-83
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,149 @@
11
/**
2-
* @typedef {import('hast').Root} Root
3-
* @typedef {import('hast').Content} Content
42
* @typedef {import('hast').Element} Element
3+
* @typedef {import('hast').Nodes} Nodes
54
* @typedef {import('hast').Properties} Properties
5+
* @typedef {import('hast').Root} Root
6+
* @typedef {import('hast').RootContent} RootContent
7+
*
68
* @typedef {import('property-information').Info} Info
79
* @typedef {import('property-information').Schema} Schema
810
*/
911

1012
/**
11-
* @typedef {Content | Root} Node
12-
* Any concrete `hast` node.
13-
* @typedef {Root | Element} HResult
13+
* @typedef {Element | Root} HResult
1414
* Result from a `h` (or `s`) call.
1515
*
16-
* @typedef {string | number} HStyleValue
16+
* @typedef {number | string} HStyleValue
1717
* Value for a CSS style field.
1818
* @typedef {Record<string, HStyleValue>} HStyle
1919
* Supported value of a `style` prop.
20-
* @typedef {string | number | boolean | null | undefined} HPrimitiveValue
20+
* @typedef {boolean | number | string | null | undefined} HPrimitiveValue
2121
* Primitive property value.
22-
* @typedef {Array<string | number>} HArrayValue
22+
* @typedef {Array<number | string>} HArrayValue
2323
* List of property values for space- or comma separated values (such as `className`).
24-
* @typedef {HPrimitiveValue | HArrayValue} HPropertyValue
24+
* @typedef {HArrayValue | HPrimitiveValue} HPropertyValue
2525
* Primitive value or list value.
2626
* @typedef {{[property: string]: HPropertyValue | HStyle}} HProperties
2727
* Acceptable value for element properties.
2828
*
29-
* @typedef {string | number | null | undefined} HPrimitiveChild
29+
* @typedef {number | string | null | undefined} HPrimitiveChild
3030
* Primitive children, either ignored (nullish), or turned into text nodes.
31-
* @typedef {Array<Node | HPrimitiveChild>} HArrayChild
31+
* @typedef {Array<HPrimitiveChild | Nodes>} HArrayChildNested
3232
* List of children.
33-
* @typedef {Node | HPrimitiveChild | HArrayChild} HChild
33+
* @typedef {Array<HArrayChildNested | HPrimitiveChild | Nodes>} HArrayChild
34+
* List of children.
35+
* @typedef {HArrayChild | HPrimitiveChild | Nodes} HChild
3436
* Acceptable child value.
3537
*/
3638

37-
import {find, normalize} from 'property-information'
39+
import {parse as commas} from 'comma-separated-tokens'
3840
import {parseSelector} from 'hast-util-parse-selector'
41+
import {find, normalize} from 'property-information'
3942
import {parse as spaces} from 'space-separated-tokens'
40-
import {parse as commas} from 'comma-separated-tokens'
4143

42-
const buttonTypes = new Set(['menu', 'submit', 'reset', 'button'])
44+
const buttonTypes = new Set(['button', 'menu', 'reset', 'submit'])
4345

4446
const own = {}.hasOwnProperty
4547

4648
/**
4749
* @param {Schema} schema
50+
* Schema to use.
4851
* @param {string} defaultTagName
49-
* @param {Array<string>} [caseSensitive]
52+
* Default tag name.
53+
* @param {Array<string> | undefined} [caseSensitive]
54+
* Case-sensitive tag names (default: `undefined`).
55+
* @returns
56+
* `h`.
5057
*/
5158
export function core(schema, defaultTagName, caseSensitive) {
5259
const adjust = caseSensitive && createAdjustMap(caseSensitive)
5360

54-
const h =
55-
/**
56-
* @type {{
57-
* (): Root
58-
* (selector: null | undefined, ...children: Array<HChild>): Root
59-
* (selector: string, properties: HProperties, ...children: Array<HChild>): Element
60-
* (selector: string, ...children: Array<HChild>): Element
61-
* }}
62-
*/
63-
(
64-
/**
65-
* Hyperscript compatible DSL for creating virtual hast trees.
66-
*
67-
* @param {string | null | undefined} [selector]
68-
* @param {HProperties | HChild | null | undefined} [properties]
69-
* @param {Array<HChild>} children
70-
* @returns {HResult}
71-
*/
72-
function (selector, properties, ...children) {
73-
let index = -1
74-
/** @type {HResult} */
75-
let node
76-
77-
if (selector === undefined || selector === null) {
78-
node = {type: 'root', children: []}
79-
// @ts-expect-error Properties are not supported for roots.
80-
children.unshift(properties)
81-
} else {
82-
node = parseSelector(selector, defaultTagName)
83-
// Normalize the name.
84-
node.tagName = node.tagName.toLowerCase()
85-
if (adjust && own.call(adjust, node.tagName)) {
86-
node.tagName = adjust[node.tagName]
87-
}
61+
/**
62+
* Hyperscript compatible DSL for creating virtual hast trees.
63+
*
64+
* @overload
65+
* @param {null | undefined} [selector]
66+
* @param {...HChild} children
67+
* @returns {Root}
68+
*
69+
* @overload
70+
* @param {string} selector
71+
* @param {HProperties} properties
72+
* @param {...HChild} children
73+
* @returns {Element}
74+
*
75+
* @overload
76+
* @param {string} selector
77+
* @param {...HChild} children
78+
* @returns {Element}
79+
*
80+
* @param {string | null | undefined} [selector]
81+
* Selector.
82+
* @param {HChild | HProperties | null | undefined} [properties]
83+
* Properties (or first child) (default: `undefined`).
84+
* @param {...HChild} children
85+
* Children.
86+
* @returns {HResult}
87+
* Result.
88+
*/
89+
function h(selector, properties, ...children) {
90+
let index = -1
91+
/** @type {HResult} */
92+
let node
93+
94+
if (selector === undefined || selector === null) {
95+
node = {type: 'root', children: []}
96+
// Properties are not supported for roots.
97+
const child = /** @type {HChild} */ (properties)
98+
children.unshift(child)
99+
} else {
100+
node = parseSelector(selector, defaultTagName)
101+
// Normalize the name.
102+
node.tagName = node.tagName.toLowerCase()
103+
if (adjust && own.call(adjust, node.tagName)) {
104+
node.tagName = adjust[node.tagName]
105+
}
88106

89-
// Handle props.
90-
if (isProperties(properties, node.tagName)) {
91-
/** @type {string} */
92-
let key
93-
94-
for (key in properties) {
95-
if (own.call(properties, key)) {
96-
addProperty(schema, node.properties, key, properties[key])
97-
}
98-
}
99-
} else {
100-
children.unshift(properties)
107+
// Handle props.
108+
if (isProperties(properties, node.tagName)) {
109+
/** @type {string} */
110+
let key
111+
112+
for (key in properties) {
113+
if (own.call(properties, key)) {
114+
addProperty(schema, node.properties, key, properties[key])
101115
}
102116
}
117+
} else {
118+
children.unshift(properties)
119+
}
120+
}
103121

104-
// Handle children.
105-
while (++index < children.length) {
106-
addChild(node.children, children[index])
107-
}
122+
// Handle children.
123+
while (++index < children.length) {
124+
addChild(node.children, children[index])
125+
}
108126

109-
if (node.type === 'element' && node.tagName === 'template') {
110-
node.content = {type: 'root', children: node.children}
111-
node.children = []
112-
}
127+
if (node.type === 'element' && node.tagName === 'template') {
128+
node.content = {type: 'root', children: node.children}
129+
node.children = []
130+
}
113131

114-
return node
115-
}
116-
)
132+
return node
133+
}
117134

118135
return h
119136
}
120137

121138
/**
122-
* @param {HProperties | HChild} value
139+
* Check if something is properties or a child.
140+
*
141+
* @param {HChild | HProperties} value
142+
* Value to check.
123143
* @param {string} name
144+
* Tag name.
124145
* @returns {value is HProperties}
146+
* Whether `value` is a properties object.
125147
*/
126148
function isProperties(value, name) {
127149
if (
@@ -150,10 +172,15 @@ function isProperties(value, name) {
150172

151173
/**
152174
* @param {Schema} schema
175+
* Schema.
153176
* @param {Properties} properties
177+
* Properties object.
154178
* @param {string} key
155-
* @param {HStyle | HPropertyValue} value
156-
* @returns {void}
179+
* Property name.
180+
* @param {HPropertyValue | HStyle} value
181+
* Property value.
182+
* @returns {undefined}
183+
* Nothing.
157184
*/
158185
function addProperty(schema, properties, key, value) {
159186
const info = find(schema, key)
@@ -192,30 +219,37 @@ function addProperty(schema, properties, key, value) {
192219
}
193220

194221
if (Array.isArray(result)) {
195-
/** @type {Array<string | number>} */
222+
/** @type {Array<number | string>} */
196223
const finalResult = []
197224

198225
while (++index < result.length) {
199-
// @ts-expect-error Assume no booleans in array.
200-
finalResult[index] = parsePrimitive(info, info.property, result[index])
226+
// Assume no booleans in array.
227+
const value = /** @type {number | string} */ (
228+
parsePrimitive(info, info.property, result[index])
229+
)
230+
finalResult[index] = value
201231
}
202232

203233
result = finalResult
204234
}
205235

206236
// Class names (which can be added both on the `selector` and here).
207237
if (info.property === 'className' && Array.isArray(properties.className)) {
208-
// @ts-expect-error Assume no booleans in `className`.
209-
result = properties.className.concat(result)
238+
// Assume no booleans in `className`.
239+
const value = /** @type {number | string} */ (result)
240+
result = properties.className.concat(value)
210241
}
211242

212243
properties[info.property] = result
213244
}
214245

215246
/**
216-
* @param {Array<Content>} nodes
247+
* @param {Array<RootContent>} nodes
248+
* Children.
217249
* @param {HChild} value
218-
* @returns {void}
250+
* Child.
251+
* @returns {undefined}
252+
* Nothing.
219253
*/
220254
function addChild(nodes, value) {
221255
let index = -1
@@ -243,9 +277,13 @@ function addChild(nodes, value) {
243277
* Parse a single primitives.
244278
*
245279
* @param {Info} info
280+
* Property information.
246281
* @param {string} name
282+
* Property name.
247283
* @param {HPrimitiveValue} value
284+
* Property value.
248285
* @returns {HPrimitiveValue}
286+
* Property value.
249287
*/
250288
function parsePrimitive(info, name, value) {
251289
if (typeof value === 'string') {

Diff for: lib/html.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
* Acceptable value for element properties.
66
* @typedef {import('./core.js').HResult} Result
77
* Result from a `h` (or `s`) call.
8-
*
8+
*/
9+
10+
/**
911
* @typedef {import('./jsx-classic.js').Element} h.JSX.Element
12+
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} h.JSX.ElementChildrenAttribute
1013
* @typedef {import('./jsx-classic.js').IntrinsicAttributes} h.JSX.IntrinsicAttributes
1114
* @typedef {import('./jsx-classic.js').IntrinsicElements} h.JSX.IntrinsicElements
12-
* @typedef {import('./jsx-classic.js').ElementChildrenAttribute} h.JSX.ElementChildrenAttribute
1315
*/
1416

1517
import {html} from 'property-information'
1618
import {core} from './core.js'
1719

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

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
import type {HProperties, HChild, HResult} from './core.js'
1+
import type {HChild, HProperties, HResult} from './core.js'
22

33
export namespace JSX {
44
/**
5-
* This defines the return value of JSX syntax.
5+
* Define the return value of JSX syntax.
66
*/
77
type Element = HResult
88

99
/**
10-
* This disallows the use of functional components.
10+
* Key of this interface defines as what prop children are passed.
11+
*/
12+
interface ElementChildrenAttribute {
13+
/**
14+
* Only the key matters, not the value.
15+
*/
16+
children?: never
17+
}
18+
19+
/**
20+
* Disallow the use of functional components.
1121
*/
1222
type IntrinsicAttributes = never
1323

1424
/**
15-
* This defines the prop types for known elements.
25+
* Define the prop types for known elements.
1626
*
17-
* For `hastscript` this defines any string may be used in combination with `hast` `Properties`.
27+
* For `hastscript` this defines any string may be used in combination with
28+
* `hast` `Properties`.
1829
*
1930
* This **must** be an interface.
2031
*/
21-
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style, @typescript-eslint/consistent-type-definitions
2232
interface IntrinsicElements {
2333
[name: string]:
2434
| HProperties
2535
| {
2636
/**
27-
* The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type.
37+
* The prop that matches `ElementChildrenAttribute` key defines the
38+
* type of JSX children, defines the children type.
2839
*/
2940
children?: HChild
3041
}
3142
}
32-
33-
/**
34-
* The key of this interface defines as what prop children are passed.
35-
*/
36-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
37-
interface ElementChildrenAttribute {
38-
/**
39-
* Only the key matters, not the value.
40-
*/
41-
children?: never
42-
}
4343
}

0 commit comments

Comments
 (0)