|
3 | 3 | * @typedef {import('unist').Parent} Parent
|
4 | 4 | * @typedef {import('unist').Literal} Literal
|
5 | 5 | * @typedef {Record<string, unknown>} Props
|
6 |
| - * @typedef {Array<Node>|string} ChildrenOrValue |
7 |
| - * |
8 |
| - * @typedef {(<T extends string, P extends Record<string, unknown>, C extends Array<Node>>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps |
9 |
| - * @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps |
10 |
| - * @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps |
11 |
| - * @typedef {(<T extends string, C extends Array<Node>>(type: T, children: C) => {type: T, children: C})} BuildParent |
12 |
| - * @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral |
13 |
| - * @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid |
| 6 | + * @typedef {Array<Node> | string} ChildrenOrValue |
14 | 7 | */
|
15 | 8 |
|
16 | 9 | /**
|
17 | 10 | * Build a node.
|
18 | 11 | *
|
19 | 12 | * @param type
|
20 | 13 | * Node type.
|
21 |
| - * @param [props] |
| 14 | + * @param props |
22 | 15 | * Fields assigned to node.
|
23 |
| - * @param [value] |
| 16 | + * @param value |
24 | 17 | * Children of node or value of `node` (cast to string).
|
25 | 18 | * @returns
|
26 | 19 | * Built node.
|
27 | 20 | */
|
28 |
| -export const u = /** |
29 |
| - * @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps} |
30 |
| - */ ( |
| 21 | +export const u = |
31 | 22 | /**
|
32 |
| - * @param {string} type |
33 |
| - * @param {Props|ChildrenOrValue} [props] |
34 |
| - * @param {ChildrenOrValue} [value] |
35 |
| - * @returns {Node} |
| 23 | + * @type {( |
| 24 | + * (<T extends string>(type: T) => {type: T}) & |
| 25 | + * (<T extends string, P extends Props>(type: T, props: P) => {type: T} & P) & |
| 26 | + * (<T extends string>(type: T, value: string) => {type: T, value: string}) & |
| 27 | + * (<T extends string, P extends Props>(type: T, props: P, value: string) => {type: T, value: string} & P) & |
| 28 | + * (<T extends string, C extends Array<Node>>(type: T, children: C) => {type: T, children: C}) & |
| 29 | + * (<T extends string, P extends Props, C extends Array<Node>>(type: T, props: P, children: C) => {type: T, children: C} & P) |
| 30 | + * )} |
36 | 31 | */
|
37 |
| - function (type, props, value) { |
38 |
| - /** @type {Node} */ |
39 |
| - const node = {type: String(type)} |
| 32 | + ( |
| 33 | + /** |
| 34 | + * @param {string} type |
| 35 | + * @param {Props | ChildrenOrValue | null | undefined} [props] |
| 36 | + * @param {ChildrenOrValue | null | undefined} [value] |
| 37 | + * @returns {Node} |
| 38 | + */ |
| 39 | + function (type, props, value) { |
| 40 | + /** @type {Node} */ |
| 41 | + const node = {type: String(type)} |
40 | 42 |
|
41 |
| - if ( |
42 |
| - (value === undefined || value === null) && |
43 |
| - (typeof props === 'string' || Array.isArray(props)) |
44 |
| - ) { |
45 |
| - value = props |
46 |
| - } else { |
47 |
| - Object.assign(node, props) |
48 |
| - } |
| 43 | + if ( |
| 44 | + (value === undefined || value === null) && |
| 45 | + (typeof props === 'string' || Array.isArray(props)) |
| 46 | + ) { |
| 47 | + value = props |
| 48 | + } else { |
| 49 | + Object.assign(node, props) |
| 50 | + } |
49 | 51 |
|
50 |
| - if (Array.isArray(value)) { |
51 |
| - // @ts-expect-error: create a parent. |
52 |
| - node.children = value |
53 |
| - } else if (value !== undefined && value !== null) { |
54 |
| - // @ts-expect-error: create a literal. |
55 |
| - node.value = String(value) |
56 |
| - } |
| 52 | + if (Array.isArray(value)) { |
| 53 | + // @ts-expect-error: create a parent. |
| 54 | + node.children = value |
| 55 | + } else if (value !== undefined && value !== null) { |
| 56 | + // @ts-expect-error: create a literal. |
| 57 | + node.value = String(value) |
| 58 | + } |
57 | 59 |
|
58 |
| - return node |
59 |
| - } |
60 |
| -) |
| 60 | + return node |
| 61 | + } |
| 62 | + ) |
0 commit comments