Skip to content

Commit 5b3f031

Browse files
committed
handle undefined and null used as Text
1 parent eca4ad7 commit 5b3f031

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

jsx-runtime.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function jsxs(tagName, attributesWithChildren, key) {
1313
if (Array.isArray(children)) {
1414
childrenArray = children.flat();
1515
}
16-
else if (children === undefined) {
16+
else if (children === undefined || children === null) {
1717
childrenArray = [];
1818
}
1919
else if (typeof children === 'string') {
@@ -22,7 +22,9 @@ export function jsxs(tagName, attributesWithChildren, key) {
2222
else {
2323
childrenArray = [children];
2424
}
25-
childrenArray = (childrenArray).map(child => {
25+
childrenArray = childrenArray
26+
.filter(child => !(child === undefined || child === null))
27+
.map(child => {
2628
if (typeof child === 'object' && child !== null) {
2729
if (Array.isArray(child)) {
2830
throw new Error(`child is array`);

jsx-runtime.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const listenerRegexAt = /^@(.+)$/;
1414
export
1515
function jsxs(
1616
tagName: string|(new(...args: unknown[]) => Component),
17-
attributesWithChildren: {[attributeName: string]: unknown, children?: undefined|string|Component|ComponentChildren},
17+
attributesWithChildren: {[attributeName: string]: unknown, children?: undefined|null|string|Component|ComponentChildren},
1818
key?: number
1919
) {
2020
let { children, ...attributes } = attributesWithChildren;
@@ -26,15 +26,17 @@ function jsxs(
2626

2727
if (Array.isArray(children)) {
2828
childrenArray = children.flat();
29-
} else if (children === undefined) {
29+
} else if (children === undefined || children === null) {
3030
childrenArray = [];
3131
} else if (typeof children === 'string') {
3232
childrenArray = [new TextComponent(children)];
3333
} else {
3434
childrenArray = [children];
3535
}
3636

37-
childrenArray = (childrenArray).map(child => {
37+
childrenArray = childrenArray
38+
.filter(child => !(child === undefined || child === null))
39+
.map(child => {
3840
if (typeof child === 'object' && child !== null) {
3941
if (Array.isArray(child)) {
4042
throw new Error(`child is array`);
@@ -87,7 +89,7 @@ function jsxs(
8789
export
8890
function jsx(
8991
_tagName: string|(new(...args: unknown[])=>Component),
90-
_attributesWithChildren: {[attributeName: string]: unknown, children?: undefined|string|Component|ComponentChildren},
92+
_attributesWithChildren: {[attributeName: string]: unknown, children?: undefined|null|string|Component|ComponentChildren},
9193
_key?: number
9294
) {
9395
return jsxs.apply(null, arguments as unknown as Parameters<typeof jsx>);

jsx.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export namespace JSX {
4343
>,
4444
'<>'?: Subscriber<HTMLElement>|Subscriber<SVGElement>|Subscriber<MathMLElement>,
4545
children?:
46-
| string|number|bigint|Supplier<string|number|bigint>
46+
| string|number|bigint|null|undefined|Supplier<string|number|bigint|null|undefined>
4747
| ComponentChild
4848
| Array<
49-
| string|number|bigint|Supplier<string|number|bigint>
49+
| string|number|bigint|null|undefined|Supplier<string|number|bigint|null|undefined>
5050
| ComponentChild | ComponentChildren
5151
>
5252
,
@@ -80,10 +80,10 @@ export namespace JSX {
8080
| Subscriber<HTMLElement>|Subscriber<SVGElement>|Subscriber<MathMLElement>
8181

8282
// children
83-
| string|number|bigint|Supplier<string|number|bigint>
83+
| string|number|bigint|null|undefined|Supplier<string|number|bigint|null|undefined>
8484
| ComponentChild
8585
| Array<
86-
| string|number|bigint|Supplier<string|number|bigint>
86+
| string|number|bigint|null|undefined|Supplier<string|number|bigint|null|undefined>
8787
| ComponentChild | ComponentChildren
8888
>
8989
}

0 commit comments

Comments
 (0)